Advertisement
gabbyshimoni

control DC motor speed via 2 PB

Feb 14th, 2019
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. #define motorPin1 7
  2. #define motorPin2 8
  3. #define motorEnaPin 9
  4. #define pbPin1 3
  5. #define pbPin2 4
  6. int pbVal1 = 0, pbVal2 = 0;
  7. int Speed = 0;
  8.  
  9. void setup() {
  10.   pinMode(motorPin1, OUTPUT);
  11.   pinMode(motorPin2, OUTPUT);
  12.   pinMode(motorEnaPin, OUTPUT);
  13.  
  14.   pinMode(pbPin1, INPUT);
  15.   pinMode(pbPin2, INPUT);
  16.  
  17.   digitalWrite(motorEnaPin, LOW);
  18.   digitalWrite(motorPin1, LOW);
  19.   digitalWrite(motorPin2, LOW);
  20. }
  21.  
  22. void loop() {
  23.  
  24.   pbVal1 = digitalRead(pbPin1);
  25.   pbVal2 = digitalRead(pbPin2);
  26.  
  27.   if (pbVal1 == HIGH && pbVal2 == LOW) {
  28.     Speed += 10;
  29.   }
  30.  
  31.   if (pbVal2 == HIGH && pbVal1 == LOW) {
  32.     Speed -= 10;
  33.     if (Speed < 0) Speed = 0;
  34.   }
  35.  
  36.   if (pbVal2 == HIGH && pbVal1 == HIGH) {
  37.     Speed = 0;
  38.   }
  39.  
  40.   analogWrite(motorEnaPin, Speed);
  41.   if (Speed > 0) {
  42.     digitalWrite(motorPin1, HIGH);
  43.   }
  44.   else {
  45.     digitalWrite(motorPin1, LOW);
  46.   }
  47.   digitalWrite(motorPin2, LOW);
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement