skizziks_53

2X Daily Motor Operator v1.0

Jul 18th, 2019
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.16 KB | None | 0 0
  1. /*
  2.    July 18, 2019
  3.  
  4.   Twice-A-Day Automatic Arduino Uno Activator (for a RC-style servo, for a pet feeder) ----- version 1.0
  5.  
  6.   This sketch will allow an Uno to operate [something] at two time periods over a 24-hour time.
  7.  
  8.   It has four stages that you must set the length of in seconds.
  9.   Stages 1 and 3 are the lengths of time that the servo will be opened.
  10.   Stages 2 and 4 are the lengths of time that the servo will be closed.
  11.  
  12.   It also has two buttons. They must be held down for at least one second for them to work.
  13.   Button #1 will reset the program timer to the beginning of stage-1, where the servo will open.
  14.   Button #2 will just open the servo for a short period of time that you set, without resetting the timer
  15.     (in case you would want to dispense a small amount of food at any time).
  16.  
  17.   This sketch also blinks the pin-13 LED on and off once every ten seconds, to help indicate if the program is still running correctly or not.
  18.   Whenever you have a program that has long waiting times, it is a good idea to have a LED that blinks to show that the program is still running properly
  19.      and has not stopped working for some reason.
  20.  
  21.   This sketch uses serial messages for button troubleshooting, so pins zero and 1 must not be used.
  22. */
  23.  
  24. int stage_indicator = 1;
  25.  
  26. float stage_1__seconds_to_operate_motor = 30; // This is the amount of seconds to operate the motor for this stage.
  27. // This is the first length of time that you would want the servo to stay open for.
  28.  
  29. float stage_2__seconds_delay_to_wait = 28800; // This is the amout of seconds to wait for this stage. It is set to 8 hours as an example.
  30.  
  31. float stage_3__seconds_to_operate_motor = 30; // This is the amount of seconds to operate the motor for this stage.
  32. // This is the second length of time that you would want the servo to stay open for.
  33.  
  34. float stage_4__seconds_delay_to_wait = 57540; // This is the number of remaining seconds in a 24-hour period, after the above three times were subtracted.
  35.  
  36.  
  37. int button_1_pin = 2; // Button 1 will reset the timer to the beginning of stage 1.
  38.  
  39. int button_2_pin = 3; // Button 2 will run the motor for a short time, without resetting the timer.
  40. int button_2_open_time = 10; // This is a time in seconds to open the servo when button 2 is pressedd.
  41.  
  42. int pwm_output_pin = 5; // This is the motor (servo) control pin.
  43. int pwm_servo_open_value = 180; // ???????? you'd have to set this for whatever servo you wanted to use...
  44. int pwm_servo_closed_value = 0; // ???????? set this also to whatever it needs to be
  45.  
  46. float seconds_counter = 0; // This gets set automatically.
  47.  
  48. int led_pin = 13; // This is just the usual LED pin, but you could use an external LED if you wanted (just use a limiting resistor with it).
  49. int led_blinker_counter = 0; // This gets set automatically.
  50.  
  51.  
  52. void setup() {
  53.   Serial.begin(9600);
  54.   pinMode(button_1_pin, INPUT_PULLUP); // Buttons are connected using input_pullup, so low = active
  55.   pinMode(button_2_pin, INPUT_PULLUP); // Buttons are connected using input_pullup, so low = active
  56.   // analogWrite doesn't need pinMode set
  57.   pinMode(led_pin, OUTPUT);
  58.   digitalWrite(led_pin, 0);
  59.   Serial.println("Exiting setup()");
  60. }
  61.  
  62. void loop() {
  63.   switch (stage_indicator) {
  64.     case 1:
  65.       check_stage_1_timer();
  66.       break;
  67.     case 2:
  68.       check_stage_2_timer();
  69.       break;
  70.     case 3:
  71.       check_stage_3_timer();
  72.       break;
  73.     case 4:
  74.       check_stage_4_timer();
  75.       break;
  76.   }
  77.   check_buttons();
  78.   one_second_timer();
  79.   check_led_blinker();
  80. }
  81.  
  82. void check_stage_1_timer() {
  83.   if (seconds_counter == stage_1__seconds_to_operate_motor) {
  84.     analogWrite(pwm_output_pin, pwm_servo_closed_value); // Close the servo, because stage_1 is ending.
  85.     seconds_counter = 0; // reset the seconds counter for the next stage
  86.     stage_indicator = 2; // Switch to the next stage
  87.   }
  88. }
  89.  
  90. void check_stage_2_timer() {
  91.   if (seconds_counter == stage_2__seconds_delay_to_wait) {
  92.     analogWrite(pwm_output_pin, pwm_servo_open_value); // Open the servo, because stage_3 is starting.
  93.     seconds_counter = 0; // reset the seconds counter for the next stage
  94.     stage_indicator = 3; // Switch to the next stage
  95.   }
  96. }
  97.  
  98. void check_stage_3_timer() {
  99.   if (seconds_counter == stage_3__seconds_to_operate_motor) {
  100.     analogWrite(pwm_output_pin, pwm_servo_closed_value); // Close the servo, because stage_3 is ending.
  101.     seconds_counter = 0; // reset the seconds counter for the next stage
  102.     stage_indicator = 4; // Switch to the next stage
  103.   }
  104. }
  105.  
  106. void check_stage_4_timer() {
  107.   if (seconds_counter == stage_1__seconds_to_operate_motor) {
  108.     analogWrite(pwm_output_pin, pwm_servo_open_value); // Open the servo, because stage_1 is starting.
  109.     seconds_counter = 0; // reset the seconds counter for the next stage
  110.     stage_indicator = 1; // Switch to the next stage, which after 4 needs to go back to 1
  111.   }
  112. }
  113.  
  114. void check_buttons() {
  115.   if (digitalRead(button_1_pin) == LOW) {
  116.     perform_button_1_function();
  117.   }
  118.   if (digitalRead(button_2_pin) == LOW) {
  119.     perform_button_2_function();
  120.   }
  121. }
  122.  
  123. void perform_button_1_function() {
  124.   Serial.println("Button #1 pressed"); // troubleshooting message
  125.   analogWrite(pwm_output_pin, pwm_servo_open_value); // Open the servo
  126.   seconds_counter = 0; // reset the seconds counter for the next stage
  127.   stage_indicator = 1; // Switch to the next stage, which after 4 needs to go back to 1
  128. }
  129.  
  130. void perform_button_2_function() {
  131.   Serial.println("Button #2 pressed"); // troubleshooting message
  132.   analogWrite(pwm_output_pin, pwm_servo_open_value); // Open the servo
  133.   delay(button_2_open_time);
  134.   analogWrite(pwm_output_pin, pwm_servo_closed_value); // Close the servo
  135. }
  136.  
  137. void one_second_timer() {
  138.   delay(1000);
  139.   seconds_counter++;
  140.   led_blinker_counter++;
  141. }
  142.  
  143. void check_led_blinker() {
  144.   // This function is just bfor blinking the pin-13 LED on and off once every ten seconds.
  145.   if (led_blinker_counter == 9) {
  146.     digitalWrite(led_pin, HIGH);
  147.     Serial.println("LED = on"); // troubleshooting message
  148.   }
  149.   if (led_blinker_counter == 10) {
  150.     digitalWrite(led_pin, LOW);
  151.     Serial.println("LED = off"); // troubleshooting message
  152.     led_blinker_counter = 0;
  153.   }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment