Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #include <Arduino.h>
  2.  
  3. int ENA=5;//connected to Arduino's port 5(output pwm)
  4. int IN1=2;//connected to Arduino's port 2
  5. int IN2=3;//connected to Arduino's port 3
  6. int ENB=6;//connected to Arduino's port 6(output pwm)
  7. int IN3=4;//connected to Arduino's port 4
  8. int IN4=7;//connected to Arduino's port 7
  9.  
  10. void stop()
  11. {
  12.     digitalWrite(ENA,LOW);
  13.     digitalWrite(ENB,LOW);//stop driving
  14.  
  15. }
  16.  
  17. void setup()
  18. {
  19.  pinMode(ENA,OUTPUT);//output
  20.  pinMode(ENB,OUTPUT);
  21.  pinMode(IN1,OUTPUT);
  22.  pinMode(IN2,OUTPUT);
  23.  pinMode(IN3,OUTPUT);
  24.  pinMode(IN4,OUTPUT);
  25.  stop();
  26.  
  27. }
  28.  
  29. void setTurnToSide(int side)
  30. {
  31.     if (side > 0)
  32.     {
  33.         digitalWrite(IN1,LOW);
  34.         digitalWrite(IN2,HIGH);//setting motorA's directon
  35.         digitalWrite(IN3,HIGH);
  36.         digitalWrite(IN4,LOW);//setting motorB's directon
  37.      }
  38.      else
  39.      {
  40.          digitalWrite(IN1,HIGH);
  41.         digitalWrite(IN2,LOW);//setting motorA's directon
  42.         digitalWrite(IN3,LOW);
  43.         digitalWrite(IN4,HIGH);//setting motorB's directon
  44.      }
  45. }
  46.  
  47. void start (int speed)
  48. {
  49.     analogWrite(ENA,speed);//start driving motorA
  50.     analogWrite(ENB,speed);//start driving motorB
  51.  
  52. }
  53.  
  54.  
  55. void loop()
  56. {
  57.  for (int i = 255; i > 0; i-=5)
  58.  {
  59.     setTurnToSide (1);
  60.     start (i);
  61.     delay(5000);
  62.     stop();
  63.     delay(100);
  64.     setTurnToSide(0);
  65.     start(i);
  66.     delay(5000);
  67.     stop();
  68.     delay(100);
  69.  }
  70.  
  71. }