搜索代码

Lesson 52-2: Advanced Code: Controlling a DC Motor with a Relay Using Arduino for High and Low-Level Triggering

Lesson 52-2: Advanced Code: Controlling a DC Motor with a Relay Using Arduino for High and Low-Level Triggering

493-Lesson 52: Control a DC motor with a relay using Arduino (two projects)
语言: C++
/*
* Lesson 51: Controlling 2 DC motors using L293D and Arduino | Arduino Step By Step Course
 * Arduino code (Advanced): change the direction of rotation 
 
 * of a DC motor with 2 relays.
 * This code can be used with High-Level Trigger relays and
 * Low-Level Trigger relays. Watch the video for instructions.
 * This is basic code. I have advanced code which can be used in both 
 
 * for Low-level trigger and High-level trigger relays with clean code.
Please watch the video explaining this code: https://youtu.be/2n0vUa0cZOI

 * 
 * Written by Ahmad Shamshiri for Robojax.com on 
 * Sunday, August 18, 2019 
 * at 20:22 in Ajax, Ontario, Canada


*** Purchase this 2-channel relay from affiliated stores ***
Purchase 2-channel 5V relays from:
eBay: https://ebay.us/dkldii
Amazon USA: https://amzn.to/31WMCU2
Amazon All European Countries: https://amzn.to/34F6qxd
Amazon Canada:  https://amzn.to/37UtSsm
Banggood: https://www.banggood.com/custlink/vmGY6mi9gD

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

 
This code is available at http://robojax.com/course1/?vid=lecture51
 
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/>.
 */


int relay1 = 2;
int relay2 = 3;

const int triggerType = LOW;// your relay type

int relayOFF, relayON;// relay ON and OFF values for LOW and HIGH Trigger relays



void setup() {

    pinMode(relay1, OUTPUT);// set pin as output for relay 1
    pinMode(relay2, OUTPUT);// set pin as output for relay 2


    if(triggerType ==LOW){
      relayON = LOW;
      relayOFF = HIGH;
      digitalWrite(relay1, relayOFF);//keep both relay OFF so motor is stopped
      digitalWrite(relay2, relayOFF);//keep both relay OFF so motor is stopped
    }else{
      relayON = HIGH;
      relayOFF = LOW; 
      digitalWrite(relay1, relayOFF);//keep both relay OFF so motor is stopped
      digitalWrite(relay2, relayOFF);//keep both relay OFF so motor is stopped
    }
 
  
  Serial.begin(9600);// initialize serial monitor with 9600 baud
  Serial.println("Robojax Motor Direction of Rotation");
  Serial.println("Using 2 Relays");  
  delay(2000);
}

void loop() {

 // Rotate in CCW direction
  motorCCW(); 
  delay(3000);// wait for 3 seconds

  motorStop();
  delay(2000);
  
 // Rotate in CW direction
  motorCW();
  delay(3000);// wait for 3 seconds

 motorStop();
 delay(2000);
  
 Serial.println("===============");
          
}// loop end

/*
 * motorCCW() 
 * controls the relay so the motor rotates in CCW
 */
void motorCCW()
{
  digitalWrite(relay1, relayON);// turn relay 1 ON
  digitalWrite(relay2, relayOFF);// turn relay 2 OFF  
  Serial.println("Rotating in CCW");    
}// motorCCW()


/*
 * motorCW() 
 * controls the relay so the motor rotates in CW
 */
void motorCW()
{
  digitalWrite(relay1, relayOFF);// turn relay 1 OFF
  digitalWrite(relay2, relayON);// turn relay 2 ON 
  Serial.println("Rotating in CW");  
}//motorCW()


/*
 * motorStop()
 * controls the relay so the motor is stopped
 */
void motorStop()
{
  digitalWrite(relay1, relayOFF);// turn relay 1 OFF
  digitalWrite(relay2, relayOFF);// turn relay 2 OFF 
  Serial.println("Stopped");    
}

|||您可能需要的东西

文件📁

没有可用的文件。