Advertisement
safwan092

Motor Control Using 3 Buttons

Dec 14th, 2017
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.02 KB | None | 0 0
  1.  
  2. int button1Pin = 5;
  3. int button2Pin = 6;
  4. int button3Pin = 7;
  5.  
  6. int motorStatus = 0;
  7.  
  8. int motor1pin = 3;
  9. int motor2pin = 4;
  10.  
  11. void setup() {
  12.   pinMode(button1Pin, INPUT);
  13.   pinMode(button2Pin, INPUT);
  14.   pinMode(button3Pin, INPUT);
  15.   pinMode(motor1pin, OUTPUT);
  16.   pinMode(motor2pin, OUTPUT);
  17.   Stop();
  18. }
  19.  
  20. void loop() {
  21.  
  22.   if (digitalRead(button1Pin) == HIGH) {
  23.     motorStatus = 0;
  24.   }
  25.  
  26.   else if (digitalRead(button2Pin) == HIGH) {
  27.     motorStatus = 1;
  28.   }
  29.  
  30.   else if (digitalRead(button3Pin) == HIGH) {
  31.     motorStatus = 2;
  32.   }
  33.  
  34.   switch (motorStatus) {
  35.     case 0:    // Stop
  36.       Stop();
  37.       break;
  38.     case 1:    // Forward
  39.       Forward();
  40.       break;
  41.     case 2:    // Backward
  42.       Backward();
  43.       break;
  44.  
  45.   }
  46.   delay(1);
  47. }
  48. void Stop() {
  49.   digitalWrite(motor1pin, LOW);
  50.   digitalWrite(motor2pin, LOW);
  51. }
  52.  
  53. void Forward() {
  54.   digitalWrite(motor1pin, LOW);
  55.   digitalWrite(motor2pin, HIGH);
  56. }
  57.  
  58. void Backward() {
  59.   digitalWrite(motor1pin, HIGH);
  60.   digitalWrite(motor2pin, LOW);
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement