Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- July 18, 2019
- Twice-A-Day Automatic Arduino Uno Activator (for a RC-style servo, for a pet feeder) ----- version 1.0
- This sketch will allow an Uno to operate [something] at two time periods over a 24-hour time.
- It has four stages that you must set the length of in seconds.
- Stages 1 and 3 are the lengths of time that the servo will be opened.
- Stages 2 and 4 are the lengths of time that the servo will be closed.
- It also has two buttons. They must be held down for at least one second for them to work.
- Button #1 will reset the program timer to the beginning of stage-1, where the servo will open.
- Button #2 will just open the servo for a short period of time that you set, without resetting the timer
- (in case you would want to dispense a small amount of food at any time).
- 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.
- 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
- and has not stopped working for some reason.
- This sketch uses serial messages for button troubleshooting, so pins zero and 1 must not be used.
- */
- int stage_indicator = 1;
- float stage_1__seconds_to_operate_motor = 30; // This is the amount of seconds to operate the motor for this stage.
- // This is the first length of time that you would want the servo to stay open for.
- 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.
- float stage_3__seconds_to_operate_motor = 30; // This is the amount of seconds to operate the motor for this stage.
- // This is the second length of time that you would want the servo to stay open for.
- 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.
- int button_1_pin = 2; // Button 1 will reset the timer to the beginning of stage 1.
- int button_2_pin = 3; // Button 2 will run the motor for a short time, without resetting the timer.
- int button_2_open_time = 10; // This is a time in seconds to open the servo when button 2 is pressedd.
- int pwm_output_pin = 5; // This is the motor (servo) control pin.
- int pwm_servo_open_value = 180; // ???????? you'd have to set this for whatever servo you wanted to use...
- int pwm_servo_closed_value = 0; // ???????? set this also to whatever it needs to be
- float seconds_counter = 0; // This gets set automatically.
- 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).
- int led_blinker_counter = 0; // This gets set automatically.
- void setup() {
- Serial.begin(9600);
- pinMode(button_1_pin, INPUT_PULLUP); // Buttons are connected using input_pullup, so low = active
- pinMode(button_2_pin, INPUT_PULLUP); // Buttons are connected using input_pullup, so low = active
- // analogWrite doesn't need pinMode set
- pinMode(led_pin, OUTPUT);
- digitalWrite(led_pin, 0);
- Serial.println("Exiting setup()");
- }
- void loop() {
- switch (stage_indicator) {
- case 1:
- check_stage_1_timer();
- break;
- case 2:
- check_stage_2_timer();
- break;
- case 3:
- check_stage_3_timer();
- break;
- case 4:
- check_stage_4_timer();
- break;
- }
- check_buttons();
- one_second_timer();
- check_led_blinker();
- }
- void check_stage_1_timer() {
- if (seconds_counter == stage_1__seconds_to_operate_motor) {
- analogWrite(pwm_output_pin, pwm_servo_closed_value); // Close the servo, because stage_1 is ending.
- seconds_counter = 0; // reset the seconds counter for the next stage
- stage_indicator = 2; // Switch to the next stage
- }
- }
- void check_stage_2_timer() {
- if (seconds_counter == stage_2__seconds_delay_to_wait) {
- analogWrite(pwm_output_pin, pwm_servo_open_value); // Open the servo, because stage_3 is starting.
- seconds_counter = 0; // reset the seconds counter for the next stage
- stage_indicator = 3; // Switch to the next stage
- }
- }
- void check_stage_3_timer() {
- if (seconds_counter == stage_3__seconds_to_operate_motor) {
- analogWrite(pwm_output_pin, pwm_servo_closed_value); // Close the servo, because stage_3 is ending.
- seconds_counter = 0; // reset the seconds counter for the next stage
- stage_indicator = 4; // Switch to the next stage
- }
- }
- void check_stage_4_timer() {
- if (seconds_counter == stage_1__seconds_to_operate_motor) {
- analogWrite(pwm_output_pin, pwm_servo_open_value); // Open the servo, because stage_1 is starting.
- seconds_counter = 0; // reset the seconds counter for the next stage
- stage_indicator = 1; // Switch to the next stage, which after 4 needs to go back to 1
- }
- }
- void check_buttons() {
- if (digitalRead(button_1_pin) == LOW) {
- perform_button_1_function();
- }
- if (digitalRead(button_2_pin) == LOW) {
- perform_button_2_function();
- }
- }
- void perform_button_1_function() {
- Serial.println("Button #1 pressed"); // troubleshooting message
- analogWrite(pwm_output_pin, pwm_servo_open_value); // Open the servo
- seconds_counter = 0; // reset the seconds counter for the next stage
- stage_indicator = 1; // Switch to the next stage, which after 4 needs to go back to 1
- }
- void perform_button_2_function() {
- Serial.println("Button #2 pressed"); // troubleshooting message
- analogWrite(pwm_output_pin, pwm_servo_open_value); // Open the servo
- delay(button_2_open_time);
- analogWrite(pwm_output_pin, pwm_servo_closed_value); // Close the servo
- }
- void one_second_timer() {
- delay(1000);
- seconds_counter++;
- led_blinker_counter++;
- }
- void check_led_blinker() {
- // This function is just bfor blinking the pin-13 LED on and off once every ten seconds.
- if (led_blinker_counter == 9) {
- digitalWrite(led_pin, HIGH);
- Serial.println("LED = on"); // troubleshooting message
- }
- if (led_blinker_counter == 10) {
- digitalWrite(led_pin, LOW);
- Serial.println("LED = off"); // troubleshooting message
- led_blinker_counter = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment