Код для поиска

Lesson 50: Controlling the Direction of Motor Rotation Using Arduino and L293D

Lesson 50: Controlling the Direction of Motor Rotation Using Arduino and L293D

495-Lesson 50: Controlling the Direction of Motor Rotation Using an Arduino and L293D
Язык: C++
++
/*
* Lesson 50: Controlling Direction of Rotation of Motor | Arduino Step By Step Course

 * This is the Arduino code L293 DC motor driver.
Using this code you can control a motor to rotate in both directions.
Please watch the video for full details of this code and project:
https://youtu.be/-Ss67lqrtL8

 *  * 
 // Written by Ahmad Shamshiri for Robojax.com on 
// January 26, 2019 at 08:01 in Ajax, Ontario, Canada

This code is available at http://robojax.com/course1/?vid=lecture50
 
with over 100 lectures free on YouTube. Watch it here: http://robojax.com/L/?id=338
Get the code for the course: http://robojax.com/L/?id=339  
 * on Saturday, November 24, 2018 
 * in Ajax, Ontario, Canada
 * 
 
 * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.
 * This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.

*/

#define Pin1A 2 // define DRIVE 1A pin
#define Pin2A 4 // define DRIVE 1Y pin
#define EN12 3 // define pin 3 for EN1, 2 (must be PWM pin)


void setup() {
  // L293 Motor Control Code by Robojax.com 2018025
  Serial.begin(9600);// setup Serial Monitor to display information
  pinMode(Pin1A, OUTPUT);// define pin as OUTPUT for Pin1A
  pinMode(Pin2A, OUTPUT);// define pin as OUTPUT for Pin2A 
  pinMode(EN12, OUTPUT);// define pin as OUTPUT for EN12
  

  // L293 Motor Control Code by Robojax.com 2018025   
}

void loop() {
  // L293 Motor Control Code by Robojax.com 2018025

  // DRIVE motor at maximum speed in CCW direction
    digitalWrite(Pin1A,HIGH);// for CCW
    digitalWrite(Pin2A,LOW);// for CCW     
    analogWrite(EN12, 255);// run motor at (255 PWM value) maximum speed 
    Serial.println(" CCW at 255");    
    delay(4000);//for 4 seconds

   // stop motor  
    analogWrite(EN12, 0);// stop motor 
    Serial.println(" Stop motor 1");    
    delay(3000);//for 3 seconds  
    
  // DRIVE motor at half speed in CW direction
    digitalWrite(Pin1A,LOW);// for CW
    digitalWrite(Pin2A,HIGH);// for CW     
    analogWrite(EN12, 125);// run motor at (125 PWM value) speed 
    Serial.println(" CW at 125");    
    delay(4000);//for 4 seconds

   // stop motor
    analogWrite(EN12, 0);// stop motor 
    Serial.println(" Stop motor 1");    
    delay(3000);//for 3 seconds  
    
  // DRIVE motor  at half speed in CW direction
    digitalWrite(Pin1A,HIGH);// for CW
    digitalWrite(Pin2A,LOW);// for CW     
    analogWrite(EN12, 125);// run motor at (125 PWM value) speed 
    Serial.println(" CCW at 125");    
    delay(4000);//for 4 seconds

  // stop motor  
    analogWrite(EN12, 0);// stop motor
    Serial.println(" Stop motor 1");    
    delay(4000);//for 4 seconds

   for(int i=125; i <=255; i +=20)
   {
     digitalWrite(Pin1A,HIGH);// for CCW
     digitalWrite(Pin2A,LOW);// for CCW     
     analogWrite(EN12, i);// send i value of PWM to motor
     Serial.print(" CCW at ");   
     Serial.println(i);  
     delay(500);//for 4 seconds   
   }
   delay(4000);// wait for 4 seconds
   
   // stop motor 
    analogWrite(EN12, 0);// stop motor
    Serial.println(" Stop motor 1");    
    delay(4000);//for 4 seconds  

   for(int i=255; i >100; i -=20)
   {
     digitalWrite(Pin1A,LOW);// 
     digitalWrite(Pin2A,HIGH);//      
     analogWrite(EN12, i);// send i value of PWM to motor
     Serial.print(" CW at ");   
     Serial.println(i);  
     delay(500);//for 4 seconds   
   }  
   delay(4000);// wait for 4 seconds
         
  Serial.println("====== Loop done"); 

  // L293 Motor Control Code by Robojax.com 2018025 
}// loop end

Файлы📁

Нет доступных файлов.