Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Reddit servo example --- v 1.0
- Just moves a servo and turns a LED on and off, without using delay()
- Board: any
- Other hardware: an RC servo and an LED
- */
- #include <Servo.h> // Standard Arduino IDE servo library
- Servo servo;
- const int LED_pin = 3;
- const int servo_pin = 9;
- int position_to_use = 0;
- int servo_positions[] = {0, 50, 180, 90};
- int delay_times[] = {500, 200, 1000, 1000}; // These are millisecond time delay values
- int led_light_up_value = 180;
- bool servo_waiting = false; // This is used to change between moving servos/setting the LED, and waiting for a time delay.
- int current_delay = 0; // Whatever current value time delay is stored in here.
- unsigned long delay_begin_time = 0;
- unsigned long delay_current_time = 0;
- void setup() {
- pinMode(LED_pin, OUTPUT);
- servo.attach(servo_pin);
- }
- void loop() {
- if (servo_waiting == false) {
- change_servo_position(servo_positions[position_to_use]);
- check_LED_state(servo_positions[position_to_use]);
- start_waiting_period(delay_times[position_to_use]);
- }
- else { // if servo_waiting == true
- check_to_enable_servo();
- }
- }
- void change_servo_position(int servo_position) {
- // This function just writes the servo position to the servo-
- // which might seem kind of silly to bother with making another function just for one line, but really isn't.
- // 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.
- servo.write(servo_position);
- }
- void start_waiting_period(int time_to_wait) {
- // This function just begins a waiting time properly.
- // time_to_wait is the value of milliseconds that you want the waiting period to be.
- current_delay = time_to_wait;
- delay_begin_time = millis();
- servo_waiting = true;
- }
- void check_to_enable_servo() {
- // This changes servo_waiting back to false after the time stored in current_delay has passed.
- delay_current_time = millis();
- if (delay_current_time >= delay_begin_time) {
- if (delay_current_time >= (delay_begin_time + current_delay)) {
- servo_waiting = false;
- advance_position(); // This changes to the next value to use, after the waiting period is over.
- }
- }
- else {
- 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.
- // 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.
- }
- }
- void check_LED_state(int current_servo_position) {
- // This just turns the LED on and off when the servo position matches led_light_up_value.
- if (current_servo_position == led_light_up_value) {
- digitalWrite(LED_pin, HIGH);
- }
- else {
- digitalWrite(LED_pin, LOW);
- }
- }
- void advance_position() {
- // This just advances the position by 1 each time it is called, and resets it to zero when needed.
- position_to_use += 1;
- if (position_to_use == 4) {
- position_to_use = 0; // Reset this when it gets too high
- }
- }
Add Comment
Please, Sign In to add comment