skizziks_53

Reddit servo + LED v2.0

Jul 2nd, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.75 KB | None | 0 0
  1. /*
  2.   Reddit servo example --- v 2.0
  3.   Just moves a servo and turns a LED on and off, without using delay()
  4.  
  5.   This version uses a state flag to control the whole program.
  6.   The main loop as a switch-case statement that separates what happens into different steps.
  7.   This model is somewhat easier to write complex processes with, since you can directly state what happens at every stage.
  8.  
  9.   Board: any
  10.   Other hardware: an RC servo and an LED
  11. */
  12.  
  13. #include <Servo.h> // Standard Arduino IDE servo library
  14.  
  15.  
  16. int program_state = 0; // This variable controls everything that happens.
  17. //                        In this particular example, there are 16 stages of the program that are numbered from zero to 15.
  18. //                        Placing serial.print statements into each stage allows you to easily troubleshoot where a program gets stuck or skips.
  19.  
  20. Servo servo;
  21. const int LED_pin = 3;
  22. const int servo_pin = 9;
  23. int position_to_use = 0;
  24.  
  25. int servo_positions[] = {0, 50, 180, 90};
  26. int delay_times[] = {1000, 2000, 3000, 4000}; // These are millisecond time delay values
  27. int led_light_up_value = 180;
  28.  
  29. bool servo_waiting = false; // This is used to change between moving servos and waiting for a time delay.
  30. int current_delay = 0; // Whatever current value time delay is stored in here.
  31. unsigned long delay_begin_time = 0;
  32. unsigned long delay_current_time = 0;
  33.  
  34.  
  35. void setup() {
  36.   Serial.begin(9600);
  37.   pinMode(LED_pin, OUTPUT);
  38.   servo.attach(servo_pin);
  39.  
  40.   pinMode(13, OUTPUT);
  41.   digitalWrite(13, LOW); // The particular clone Uno board I used needs this or it sets it to HIGH...
  42.  
  43.   Serial.println("Exiting setup()");
  44. }
  45.  
  46.  
  47. void loop() {
  48.   switch (program_state) {
  49.  
  50.     case 0:
  51.       show_program_state();
  52.       servo.write(servo_positions[position_to_use]);
  53.       program_state = 1;
  54.       break;
  55.  
  56.     case 1:
  57.       show_program_state();
  58.       check_LED_state(servo_positions[position_to_use]);
  59.       program_state = 2;
  60.       break;
  61.  
  62.     case 2:
  63.       show_program_state();
  64.       start_waiting_period(delay_times[position_to_use]);
  65.       program_state = 3;
  66.       break;
  67.  
  68.     case 3:
  69.       // This stage gets checked a bunch of times while it is waiting to re-enable the servo,
  70.       // so it only prints the program_stage when it is completed.
  71.       check_to_enable_servo();
  72.       if (servo_waiting == false) {
  73.         show_program_state();
  74.         program_state = 4;
  75.         position_to_use = 1;
  76.       }
  77.       break;
  78.  
  79.     case 4:
  80.       show_program_state();
  81.       servo.write(servo_positions[position_to_use]);
  82.       program_state = 5;
  83.       break;
  84.  
  85.     case 5:
  86.       show_program_state();
  87.       check_LED_state(servo_positions[position_to_use]);
  88.       program_state = 6;
  89.       break;
  90.  
  91.     case 6:
  92.       show_program_state();
  93.       start_waiting_period(delay_times[position_to_use]);
  94.       program_state = 7;
  95.       break;
  96.  
  97.     case 7:
  98.       // This stage gets checked a bunch of times while it is waiting to re-enable the servo,
  99.       // so it only prints the program_stage when it is completed.
  100.       check_to_enable_servo();
  101.       if (servo_waiting == false) {
  102.         show_program_state();
  103.         program_state = 8;
  104.         position_to_use = 2;
  105.       }
  106.       break;
  107.  
  108.     case 8:
  109.       show_program_state();
  110.       servo.write(servo_positions[position_to_use]);
  111.       program_state = 9;
  112.       break;
  113.  
  114.     case 9:
  115.       show_program_state();
  116.       check_LED_state(servo_positions[position_to_use]);
  117.       program_state = 10;
  118.       break;
  119.  
  120.     case 10:
  121.       show_program_state();
  122.       start_waiting_period(delay_times[position_to_use]);
  123.       program_state = 11;
  124.       break;
  125.  
  126.     case 11:
  127.       // This stage gets checked a bunch of times while it is waiting to re-enable the servo,
  128.       // so it only prints the program_stage when it is completed.
  129.       check_to_enable_servo();
  130.       if (servo_waiting == false) {
  131.         show_program_state();
  132.         program_state = 12;
  133.         position_to_use = 3;
  134.       }
  135.       break;
  136.  
  137.     case 12:
  138.       show_program_state();
  139.       servo.write(servo_positions[position_to_use]);
  140.       program_state = 13;
  141.       break;
  142.  
  143.     case 13:
  144.       show_program_state();
  145.       check_LED_state(servo_positions[position_to_use]);
  146.       program_state = 14;
  147.       break;
  148.  
  149.     case 14:
  150.       show_program_state();
  151.       start_waiting_period(delay_times[position_to_use]);
  152.       program_state = 15;
  153.       break;
  154.  
  155.     case 15:
  156.       // This stage gets checked a bunch of times while it is waiting to re-enable the servo,
  157.       // so it only prints the program_stage when it is completed.
  158.       check_to_enable_servo();
  159.       if (servo_waiting == false) {
  160.         show_program_state();
  161.         program_state = 0; // This gets reset back to zero at the last step, for the program to repeat.
  162.         //                    If you left this value at 17, then all the steps would only be done one time.
  163.         position_to_use = 0; // This needs to be reset back to zero here also since the arrays only had 4 positions that start at zero.
  164.       }
  165.       break;
  166.  
  167.   }
  168. }
  169.  
  170.  
  171.  
  172. void change_servo_position(int servo_position) {
  173.   servo.write(servo_position);
  174. }
  175.  
  176.  
  177.  
  178. void start_waiting_period(int time_to_wait) {
  179.   // This function just begins a waiting time properly.
  180.   // time_to_wait is the value of milliseconds that you want the waiting period to be.
  181.   current_delay = time_to_wait;
  182.   delay_begin_time = millis();
  183.   servo_waiting = true;
  184. }
  185.  
  186.  
  187.  
  188. void check_to_enable_servo() {
  189.   // This changes servo_waiting back to false after the time stored in current_delay has passed.
  190.   delay_current_time = millis();
  191.   if (delay_current_time >= delay_begin_time) {
  192.     if (delay_current_time >= (delay_begin_time + current_delay)) {
  193.       servo_waiting = false;
  194.     }
  195.   }
  196.   else {
  197.     delay_begin_time = millis(); // This is a condition to reset the begin time, in case millis() had rolled over. After running for 53 days or so.
  198.     // This is more of an issue with using micros() that rolls over in only about 14 hours, but I'm just showing it here anyway.
  199.   }
  200. }
  201.  
  202.  
  203.  
  204. void check_LED_state(int current_servo_position) {
  205.   // This just turns the LED on and off when the servo position matches led_light_up_value.
  206.   if (current_servo_position == led_light_up_value) {
  207.     digitalWrite(LED_pin, HIGH);
  208.   }
  209.   else {
  210.     digitalWrite(LED_pin, LOW);
  211.   }
  212. }
  213.  
  214.  
  215. void show_program_state() {
  216.   // This prints the current program state every time it is called.
  217.   Serial.print("Executing program_state: ");
  218.   Serial.println(program_state);
  219. }
  220.  
  221.  
  222. /*
  223.   // This sketch does not use this function, as it just assigns values to position_to_use directly.
  224.   void advance_position() {
  225.   position_to_use += 1;
  226.   if (position_to_use = 4) {
  227.     position_to_use = 0; // Reset this when it gets too high
  228.   }
  229.   }
  230. */
Add Comment
Please, Sign In to add comment