Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Arduino Code: Using RC controller to control DC motor speed and direction
- Shield: 2A motor driver shield
- */
- const int PulseInPin = A1;
- int PulseVal = 0;
- int MotorSpeed = 0;
- int MotorDir = 0;
- //Arduino PWM Speed Control:
- int E1 = 5;
- int M1 = 4;
- void setup()
- {
- pinMode(M1, OUTPUT);
- pinMode(E1, OUTPUT);
- }
- void loop()
- {
- PulseVal = pulseIn(PulseInPin, HIGH); //1500 middle
- if(PulseVal > 1600)//Forward
- {
- MotorSpeed = map(PulseVal, 1600, 2000, 0, 255);
- digitalWrite(M1,HIGH);
- analogWrite(E1, MotorSpeed);
- }
- else if(PulseVal < 1400) //Backward
- {
- MotorSpeed = map(PulseVal, 1400, 1000, 0, 255);
- digitalWrite(M1,LOW);
- analogWrite(E1, MotorSpeed);
- }
- else //PulseVal: 1400-1600
- { //STOP
- MotorSpeed = 0;
- analogWrite(E1, MotorSpeed);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement