skizziks_53

Reddit servo + LED v1.0

Jul 2nd, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.07 KB | None | 0 0
  1. /*
  2.    Reddit servo example --- v 1.0
  3.    Just moves a servo and turns a LED on and off, without using delay()
  4.  
  5.    Board: any
  6.    Other hardware: an RC servo and an LED
  7. */
  8.  
  9. #include <Servo.h> // Standard Arduino IDE servo library
  10.  
  11. Servo servo;
  12. const int LED_pin = 3;
  13. const int servo_pin = 9;
  14. int position_to_use = 0;
  15.  
  16. int servo_positions[] = {0, 50, 180, 90};
  17. int delay_times[] = {500, 200, 1000, 1000}; // These are millisecond time delay values
  18. int led_light_up_value = 180;
  19.  
  20. bool servo_waiting = false; // This is used to change between moving servos/setting the LED, and waiting for a time delay.
  21. int current_delay = 0; // Whatever current value time delay is stored in here.
  22. unsigned long delay_begin_time = 0;
  23. unsigned long delay_current_time = 0;
  24.  
  25.  
  26. void setup() {
  27.   pinMode(LED_pin, OUTPUT);
  28.   servo.attach(servo_pin);
  29. }
  30.  
  31.  
  32. void loop() {
  33.   if (servo_waiting == false) {
  34.     change_servo_position(servo_positions[position_to_use]);
  35.     check_LED_state(servo_positions[position_to_use]);
  36.     start_waiting_period(delay_times[position_to_use]);
  37.   }
  38.   else { // if servo_waiting == true
  39.     check_to_enable_servo();
  40.   }
  41. }
  42.  
  43.  
  44.  
  45. void change_servo_position(int servo_position) {
  46.   // This function just writes the servo position to the servo-
  47.   // which might seem kind of silly to bother with making another function just for one line, but really isn't.
  48.   // If there was anything that you decided you wanted to do every time you moved a servo, then you could include that part in here.
  49.   servo.write(servo_position);
  50. }
  51.  
  52.  
  53.  
  54. void start_waiting_period(int time_to_wait) {
  55.   // This function just begins a waiting time properly.
  56.   // time_to_wait is the value of milliseconds that you want the waiting period to be.
  57.   current_delay = time_to_wait;
  58.   delay_begin_time = millis();
  59.   servo_waiting = true;
  60. }
  61.  
  62.  
  63.  
  64. void check_to_enable_servo() {
  65.   // This changes servo_waiting back to false after the time stored in current_delay has passed.
  66.   delay_current_time = millis();
  67.   if (delay_current_time >= delay_begin_time) {
  68.     if (delay_current_time >= (delay_begin_time + current_delay)) {
  69.       servo_waiting = false;
  70.       advance_position(); // This changes to the next value to use, after the waiting period is over.
  71.     }
  72.   }
  73.   else {
  74.     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.
  75.     // 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.
  76.   }
  77. }
  78.  
  79.  
  80.  
  81. void check_LED_state(int current_servo_position) {
  82.   // This just turns the LED on and off when the servo position matches led_light_up_value.
  83.   if (current_servo_position == led_light_up_value) {
  84.     digitalWrite(LED_pin, HIGH);
  85.   }
  86.   else {
  87.     digitalWrite(LED_pin, LOW);
  88.   }
  89. }
  90.  
  91.  
  92.  
  93. void advance_position() {
  94.   // This just advances the position by 1 each time it is called, and resets it to zero when needed.
  95.   position_to_use += 1;
  96.   if (position_to_use == 4) {
  97.     position_to_use = 0; // Reset this when it gets too high
  98.   }
  99. }
Add Comment
Please, Sign In to add comment