#include <Arduino.h>
int ENA=5;//connected to Arduino's port 5(output pwm)
int IN1=2;//connected to Arduino's port 2
int IN2=3;//connected to Arduino's port 3
int ENB=6;//connected to Arduino's port 6(output pwm)
int IN3=4;//connected to Arduino's port 4
int IN4=7;//connected to Arduino's port 7
void stop()
{
digitalWrite(ENA,LOW);
digitalWrite(ENB,LOW);//stop driving
}
void setup()
{
pinMode(ENA,OUTPUT);//output
pinMode(ENB,OUTPUT);
pinMode(IN1,OUTPUT);
pinMode(IN2,OUTPUT);
pinMode(IN3,OUTPUT);
pinMode(IN4,OUTPUT);
stop();
}
void setTurnToSide(int side)
{
if (side > 0)
{
digitalWrite(IN1,LOW);
digitalWrite(IN2,HIGH);//setting motorA's directon
digitalWrite(IN3,HIGH);
digitalWrite(IN4,LOW);//setting motorB's directon
}
else
{
digitalWrite(IN1,HIGH);
digitalWrite(IN2,LOW);//setting motorA's directon
digitalWrite(IN3,LOW);
digitalWrite(IN4,HIGH);//setting motorB's directon
}
}
void start (int speed)
{
analogWrite(ENA,speed);//start driving motorA
analogWrite(ENB,speed);//start driving motorB
}
void loop()
{
for (int i = 255; i > 0; i-=5)
{
setTurnToSide (1);
start (i);
delay(5000);
stop();
delay(100);
setTurnToSide(0);
start(i);
delay(5000);
stop();
delay(100);
}
}