firedancer

Motor controlled by serial

Jul 4th, 2013
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.60 KB | None | 0 0
  1. //Arduino PWM Speed Control:
  2. int E1 = 5;  
  3. int M1 = 4;
  4.  
  5. void setup() {
  6.     pinMode(M1, OUTPUT);
  7.     Serial.begin(9600);
  8. }
  9.  
  10. void loop() {
  11.   if (Serial.available()) {
  12.     int value = Serial.parseInt();
  13.     if (value != 0) {
  14.       if (value > 0) {
  15.           digitalWrite(M1, HIGH);
  16.       } else {
  17.           digitalWrite(M1, LOW); // Go backwards
  18.       }
  19.       value = abs(value);
  20.      
  21.       if (value < 5) {
  22.               analogWrite(E1, 0);
  23.       } else {
  24.         value = map(value, 0, 90, 50, 255);
  25.         analogWrite(E1, value);      
  26.       }
  27.       delay(2);
  28.     }
  29.   }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment