Advertisement
FahmiG

RC transmitter control dc motor Arduino

Jun 11th, 2015
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.92 KB | None | 0 0
  1. /*
  2. Arduino Code: Using RC controller to control DC motor speed and direction
  3. Shield: 2A motor driver shield
  4.  
  5. */
  6. const int PulseInPin = A1;
  7.  
  8. int PulseVal = 0;
  9. int MotorSpeed = 0;
  10. int MotorDir = 0;
  11.  
  12. //Arduino PWM Speed Control:
  13. int E1 = 5;  
  14. int M1 = 4;
  15.  
  16. void setup()
  17. {
  18.     pinMode(M1, OUTPUT);  
  19.     pinMode(E1, OUTPUT);  
  20.    
  21. }
  22.  
  23. void loop()
  24. {
  25.       PulseVal = pulseIn(PulseInPin, HIGH); //1500 middle
  26.    
  27.     if(PulseVal > 1600)//Forward
  28.     {
  29.         MotorSpeed = map(PulseVal, 1600, 2000, 0, 255);
  30.        
  31.         digitalWrite(M1,HIGH);
  32.         analogWrite(E1, MotorSpeed);
  33.     }
  34.     else if(PulseVal < 1400) //Backward
  35.     {
  36.         MotorSpeed = map(PulseVal, 1400, 1000, 0, 255);
  37.        
  38.         digitalWrite(M1,LOW);
  39.         analogWrite(E1, MotorSpeed);
  40.     }
  41.     else //PulseVal: 1400-1600
  42.     {  //STOP
  43.         MotorSpeed = 0;
  44.         analogWrite(E1, MotorSpeed);
  45.     }
  46.  
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement