Advertisement
candale

AmoledMotor

Nov 9th, 2015
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.35 KB | None | 0 0
  1. // #define GENERAL_SWITCH_PIN 13 // 8
  2. // #define DIRECTION_PIN 12 // 9
  3. // #define UP_SWITCH_PIN 11 // 10
  4. // #define DOWN_SWITCH_PIN 10 // 11
  5. // #define BUTTON_UP_PIN 9 // 12
  6. // #define BUTTON_DOWN_PIN 8 // 13
  7.  
  8. #define GENERAL_SWITCH_PIN 6 // 8
  9. #define DIRECTION_PIN 5 // 9
  10. #define UP_SWITCH_PIN 3 // 10
  11. #define DOWN_SWITCH_PIN 2 // 11
  12. #define BUTTON_UP_PIN 1 // 12
  13. #define BUTTON_DOWN_PIN 0 // 13
  14.  
  15. #define TEST_PIN 6
  16.  
  17. #define TEST_PIN 6
  18.  
  19. int up_switch = 0;
  20. int down_switch = 0;
  21.  
  22. int button_up_stat = 0;
  23. int button_down_stat = 0;
  24.  
  25. int move_to_down = 0;
  26. int move_to_up = 0;
  27.  
  28. int state = 0;
  29.  
  30. void reset_state()
  31. {
  32.     // Serial.println("Reseting state...");
  33.     move_to_down = 0;
  34.     move_to_up = 0;
  35.     button_down_stat = 0;
  36.     button_up_stat = 0;
  37.     up_switch = 0;
  38.     down_switch = 0;
  39.    
  40.     delay(1000);
  41. }
  42.  
  43. void setup() {
  44.   pinMode(GENERAL_SWITCH_PIN, OUTPUT);
  45.   pinMode(DIRECTION_PIN, OUTPUT);
  46.  
  47.   pinMode(UP_SWITCH_PIN, INPUT);
  48.   pinMode(DOWN_SWITCH_PIN, INPUT);
  49.   pinMode(BUTTON_DOWN_PIN, INPUT);
  50.   pinMode(BUTTON_UP_PIN, INPUT);
  51.  
  52.   // Serial.begin(9600);
  53.  
  54.   reset_state();
  55. }
  56.  
  57. void disable_outputs()
  58. {
  59.   // Serial.println("Disabling outputs...");
  60.   digitalWrite(GENERAL_SWITCH_PIN, LOW);
  61.   delay(100);
  62.   digitalWrite(DIRECTION_PIN, LOW);
  63.   state = 0;
  64. }
  65.  
  66. void drive_up()
  67. {
  68.   state = 1;
  69.   // Serial.println("Drive up...");
  70.   digitalWrite(GENERAL_SWITCH_PIN, HIGH);
  71. }
  72.  
  73. void drive_down()
  74. {
  75.   state = 2;
  76.   // Serial.println("Drive down...");
  77.   digitalWrite(DIRECTION_PIN, HIGH);
  78.   digitalWrite(GENERAL_SWITCH_PIN, HIGH);
  79. }
  80.  
  81. void print_state()
  82. {
  83.   // Serial.print("UP_SWITCH: ");
  84.   // Serial.print(up_switch);
  85.   // Serial.print(" | DOWN_SWITCH: ");
  86.   // Serial.print(down_switch);
  87.   // Serial.print(" | BUS: ");
  88.   // Serial.print(button_up_stat);
  89.   // Serial.print(" | BDS: ");
  90.   // Serial.print(button_down_stat);
  91.   // Serial.print(" | MTU: ");
  92.   // Serial.print(move_to_up);
  93.   // Serial.print(" | MTD: ");
  94.   // Serial.print(move_to_down);
  95.   // Serial.print(" | BUT_UP: ");
  96.   // Serial.print(digitalRead(BUTTON_UP_PIN));
  97.   // Serial.print(" | BUT_DOWN: ");
  98.   // Serial.println(digitalRead(BUTTON_DOWN_PIN));
  99. }
  100.  
  101. void loop() {
  102.   // Get switches status
  103.   up_switch = digitalRead(UP_SWITCH_PIN);
  104.   down_switch = digitalRead(DOWN_SWITCH_PIN);
  105.  
  106.   // Get the read from the buttons
  107.   // If the button was pressed once, it records that value and for the
  108.   // subsequent loops, the vars will keep their values until
  109.   // one of the switches are activated
  110.   button_up_stat = digitalRead(BUTTON_UP_PIN) || button_up_stat;
  111.   button_down_stat = digitalRead(BUTTON_DOWN_PIN) || button_down_stat;
  112.  
  113.   // Determine if we should move to the down or to the up
  114.   move_to_up = button_up_stat == HIGH && up_switch == LOW;
  115.   move_to_down = button_down_stat == HIGH && down_switch == LOW;
  116.  
  117.   // If both are pressed, do nothing
  118.   if(move_to_down == move_to_up && state != 0)
  119.   {
  120.     print_state();
  121.     disable_outputs();
  122.     if(move_to_down == HIGH)
  123.     {
  124.       delay(1000);
  125.     }
  126.     reset_state();
  127.     return;
  128.   } else if(move_to_up == HIGH && state != 1)
  129.   {
  130.     print_state();
  131.     drive_up();
  132.   } else if(move_to_down == HIGH && state != 2)
  133.   {
  134.     print_state();
  135.     drive_down();
  136.   } else if(move_to_up == move_to_down && state == 0)
  137.   {
  138.     button_up_stat = 0;
  139.     button_down_stat = 0;
  140.   }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement