2

I have many LEDs and two servos connected to one Arduino Uno, and the servos are programmed to move randomly indefinitely. However, I would like to install a kill switch for the servos by reading a pot connected to the A0 pin, and when the input from the pot is over a certain value, the servos stop moving. Then, when the pot is "turned down" again, they resume the movement sequence.

I'm a novice, so please be gentle with me! I'm just in a time crunch and know you all probably have an elegant way of getting this done. Thanks!

#include <Servo.h>

// ----CONSTANTS (won't change)

const int led2 = 2;
const int led3 = 3;// the pin numbers for the LEDs
const int led4 = 4;
const int led5 = 5;
const int led6 = 6;
const int led7 = 7;
const int led11 = 11;
const int led12 = 12;
const int led13 = 13;
const int killpot = A0; // analog pin for kill switch pot

const int servoPin1 = 9; // the pin number for the servo signal
const int servoPin2 = 10;

const int led_3_Interval = 2000;
const int led_5_Interval = 4000;
const int led_6_Interval = 3000;
const int led_7_Interval = 10000;
const int led_11_Interval = 1000;
const int led_12_Interval = 1000;
const int led_13_Interval = 6000;
const int led_4_Interval = 3000;

const int blinkDuration = 2000; // number of millisecs that Led's are on - all three leds use this

const int servoMinDegrees = 15; // the limits to servo movement
const int servoMaxDegrees = 165;


//------- VARIABLES (will change)

byte led_2_state = LOW;           
byte led_3_state = LOW;         
byte led_4_state = LOW; 
byte led_5_state = LOW; 
byte led_6_state = LOW; 
byte led_7_state = LOW; 
byte led_11_state = LOW; 
byte led_12_state = LOW;
byte led_13_state = LOW;// used to record whether the LEDs are on or off

Servo left_servo;  // create servo object to control a servo 
Servo right_servo;

int servoPosition1 = 90;     // the current angle of the servo - starting at 90.
int servoPosition2 = 90;     // the current angle of the servo - starting at 90.
int servoSlowInterval = 2000; // millisecs between servo moves
int servoFastInterval = 1000;
int servoInterval = servoSlowInterval; // initial millisecs between servo moves
int servoDegrees = random(5, 50);       // amount servo moves at each step 
                           //    will be changed to negative value for movement in the other direction

unsigned long currentMillis = 0;    // stores the value of millis() in each iteration of loop()
unsigned long previousLed2Millis = 0;   // will store last time the LED was updated
unsigned long previousLed3Millis = 0;
unsigned long previousLed4Millis = 0;
unsigned long previousLed5Millis = 0;
unsigned long previousLed6Millis = 0;
unsigned long previousLed7Millis = 0;
unsigned long previousLed11Millis = 0;
unsigned long previousLed12Millis = 0;
unsigned long previousLed13Millis = 0;

unsigned long previousServoLeftMillis = 0; // the time when the servo was last moved
unsigned long previousServoRightMillis = 0;

//========

void setup() {

 Serial.begin(9600);
 Serial.println("Starting SeveralThingsAtTheSameTimeRev1.ino");  // so we know what sketch is running

     // set the Led pins as output:
 pinMode(led2, OUTPUT);
 pinMode(led3, OUTPUT);
 pinMode(led4, OUTPUT);
 pinMode(led5, OUTPUT);
 pinMode(led6, OUTPUT);
 pinMode(led7, OUTPUT);
 pinMode(led11, OUTPUT);
 pinMode(led12, OUTPUT);
 pinMode(led13, OUTPUT);

     // set the button pin as input with a pullup resistor to ensure it defaults to HIGH

 left_servo.write(servoPosition1); // sets the initial position
 left_servo.attach(servoPin1);
 right_servo.write(servoPosition2); // sets the initial position
 right_servo.attach(servoPin2);
}

//=======

void loop() {

     // Notice that none of the action happens in loop() apart from reading millis()
     //   it just calls the functions that have the action code

 currentMillis = millis();   // capture the latest value of millis()
                             //   this is equivalent to noting the time from a clock
                             //   use the same time for all LED flashes to keep them synchronized
 analogRead(resswitch);
 updateLed_2_State();
 updateLed_3_State();
 updateLed_4_State();
 updateLed_5_State();
 updateLed_6_State();
 updateLed_7_State();
 updateLed_11_State();
 updateLed_12_State();
 updateLed_13_State();
 switchLeds();
 servoLeft_Sweep();
 servoRight_Sweep();

}

//========

void updateLed_2_State() {

 digitalWrite(led2, HIGH); // turn eye LEDs on
}

//=======

void updateLed_3_State() {

 if (led_3_state == LOW) {
   if (currentMillis - previousLed3Millis >= led_3_Interval) {
      led_3_state = HIGH;
      previousLed3Millis += led_3_Interval;
   }
 }
 else {
   if (currentMillis - previousLed3Millis >= blinkDuration) {
      led_3_state = LOW;
      previousLed3Millis += blinkDuration;
   } 
 }    
}

//=======

void updateLed_4_State() {

 if (led_4_state == LOW) {
   if (currentMillis - previousLed4Millis >= led_4_Interval) {
      led_4_state = HIGH;
      previousLed4Millis += led_4_Interval;
   }
 }
 else {
   if (currentMillis - previousLed4Millis >= blinkDuration) {
      led_4_state = LOW;
      previousLed4Millis += blinkDuration;
   }
 }    
}
//=======

void updateLed_5_State() {

 if (led_5_state == LOW) {
   if (currentMillis - previousLed5Millis >= led_5_Interval) {
      led_5_state = HIGH;
      previousLed5Millis += led_5_Interval;
   }
 }
 else {
   if (currentMillis - previousLed5Millis >= blinkDuration) {
      led_5_state = LOW;
      previousLed5Millis += blinkDuration;
   }
 }    
}
//=======

void updateLed_6_State() {

 if (led_6_state == LOW) {
   if (currentMillis - previousLed6Millis >= led_6_Interval) {
      led_6_state = HIGH;
      previousLed6Millis += led_6_Interval;
   }
 }
 else {
   if (currentMillis - previousLed6Millis >= blinkDuration) {
      led_6_state = LOW;
      previousLed6Millis += blinkDuration;
   }
 }    
}
//=======

void updateLed_7_State() {

 if (led_7_state == LOW) {
   if (currentMillis - previousLed7Millis >= led_7_Interval) {
      led_7_state = HIGH;
      previousLed7Millis += led_7_Interval;
   }
 }
 else {
   if (currentMillis - previousLed7Millis >= blinkDuration) {
      led_7_state = LOW;
      previousLed7Millis += blinkDuration;
   }
 }    
}
//=======

void updateLed_11_State() {

 if (led_11_state == LOW) {
   if (currentMillis - previousLed11Millis >= led_11_Interval) {
      led_11_state = HIGH;
      previousLed11Millis += led_11_Interval;
   }
 }
 else {
   if (currentMillis - previousLed11Millis >= blinkDuration) {
      led_11_state = LOW;
      previousLed11Millis += blinkDuration;
   }
 }    
}
//=======

void updateLed_12_State() {

 if (led_12_state == LOW) {
   if (currentMillis - previousLed12Millis >= led_12_Interval) {
      led_12_state = HIGH;
      previousLed12Millis += led_12_Interval;
   }
 }
 else {
   if (currentMillis - previousLed12Millis >= blinkDuration) {
      led_12_state = LOW;
      previousLed12Millis += blinkDuration;
   }
 }    
}

//=======

void updateLed_13_State() {

 if (led_13_state == LOW) {
   if (currentMillis - previousLed13Millis >= led_13_Interval) {
      led_13_state = HIGH;
      previousLed13Millis += led_13_Interval;
   }
 }
 else {
   if (currentMillis - previousLed13Millis >= blinkDuration) {
      led_13_state = LOW;
      previousLed13Millis += blinkDuration;
   }
 }    
}

//========

void switchLeds() {
     // this is the code that actually switches the LEDs on and off

 digitalWrite(led3, led_3_state);
 digitalWrite(led4, led_4_state);
 digitalWrite(led5, led_5_state);
 digitalWrite(led6, led_6_state);
 digitalWrite(led7, led_7_state);
 digitalWrite(led11, led_11_state);
 digitalWrite(led12, led_12_state);
 digitalWrite(led13, led_13_state);

}

//========

void servoLeft_Sweep() {

     // this is similar to the servo sweep example except that it uses millis() rather than delay()

     // nothing happens unless the interval has expired
     // the value of currentMillis was set in loop()

 if (currentMillis - previousServoLeftMillis >= servoInterval) {
       // its time for another move
   previousServoLeftMillis += servoInterval;

   servoPosition1 = servoPosition1 + servoDegrees; // servoDegrees might be negative

   if (servoPosition1 <= servoMinDegrees) {
         // when the servo gets to its minimum position change the interval to change the speed
      if (servoInterval == servoSlowInterval) {
        servoInterval = servoFastInterval;
      }
      else {
       servoInterval = servoSlowInterval;
      }
   }
   if ((servoPosition1 >= servoMaxDegrees) || (servoPosition1 <= servoMinDegrees))  {
         // if the servo is at either extreme change the sign of the degrees to make it move the other way
     servoDegrees = - servoDegrees; // reverse direction
         // and update the position to ensure it is within range
     servoPosition1 = servoPosition1 + servoDegrees; 
   }
       // make the servo move to the next position
   left_servo.write(servoPosition1);
       // and record the time when the move happened
 }
}

//========

void servoRight_Sweep() {

     // this is similar to the servo sweep example except that it uses millis() rather than delay()

     // nothing happens unless the interval has expired
     // the value of currentMillis was set in loop()

 if (currentMillis - previousServoRightMillis >= servoInterval) {
       // its time for another move
   previousServoRightMillis += servoInterval;

   servoPosition1 = servoPosition1 + servoDegrees; // servoDegrees might be negative

   if ((servoPosition1 >= servoMaxDegrees) || (servoPosition1 <= servoMinDegrees))  {
         // if the servo is at either extreme change the sign of the degrees to make it move the other way
     servoDegrees = - servoDegrees; // reverse direction
         // and update the position to ensure it is within range
     servoPosition1 = servoPosition1 + servoDegrees; 
   }
      else {
       servoInterval = servoSlowInterval;
      }
       if (servoPosition1 <= servoMinDegrees) {
         // when the servo gets to its minimum position change the interval to change the speed
      if (servoInterval == servoSlowInterval) {
        servoInterval = servoFastInterval;
      }


   }
       // make the servo move to the next position
   right_servo.write(servoPosition1);
       // and record the time when the move happened
 }
}
//=====END
  • while (analogRead(resswitch) > 700); – Majenko Oct 27 '16 at 22:58
  • Thank you! I've inserted that line in the servoLeft_Sweep() and servoRight_Sweep() sections, and the LEDs pause where they are and the servos halt, which is good. But, is there a way to insert the "while" argument and have the LED sequence continue to run with the servos stopping? – 2plus2is5 Oct 27 '16 at 23:28
  • Turn it into an if, invert the logic, and wrap it around the servo functions. – Majenko Oct 27 '16 at 23:30
  • Please excuse my novice ability... but like this? if (analogRead(resswitch) > 700); { if (currentMillis - previousServoLeftMillis >= servoInterval) {... – 2plus2is5 Oct 27 '16 at 23:37
  • It's rather if (analogRead(resswitch) < 700) { servoLeft_Sweep(); servoRight_Sweep(); } – Edgar Bonet Oct 28 '16 at 8:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.