Servo with LEDs

Here is my code below for LEDs which works fine on the board.
Theres a photoresistor with a laser pointer pointed at it. When i move my hand across the laser, it switches to two other LEDs with a delay between LED 2&3 (outputs 5&6)

I have a second code below where i introduced a servo to the circuit. The servo works fine, now when I go to swipe my hand across the laser the second and 3rd LEDs dont go off and LED1 just stays on…? When I remove the servo wires and keep same code, the LED system still doesnt work so I am assuming its not a hardware issue

Original Code below

=========================================

int lightPin = 0;  //pin for Photoresistor
int TranslateAnalog = 0;

void setup()
{
   Serial.begin(9600);  //Start serial communication
   pinMode (7, OUTPUT);
   pinMode (6, OUTPUT);
   pinMode (5, OUTPUT);
 }

void loop() {
 
   Serial.println(analogRead(lightPin)); //Write the value of the photoresistor to the serial monitor.
   TranslateAnalog = analogRead(lightPin); //Read the Photoresistor value
  
   //If Photoresistor Value is above 355 it will disable LED
   if(TranslateAnalog>355) {
     digitalWrite(5, LOW);
     digitalWrite(6, LOW);
     digitalWrite(7, HIGH);
   }
   //If Photoreistor Value is below 355 it will turn LED ON
   if(TranslateAnalog<355) {
     digitalWrite(6, HIGH);
     digitalWrite(7, LOW);
     delay(500);
     digitalWrite(5, HIGH);
     delay(490);
   }
  delay(10); //10 miliseconds delay
}

=========================================

Code with Servo below

=========================================

#include<Servo.h>
int pos = 0;
Servo myservo;

int lightPin = 0;  //pin for Photoresistor
int TranslateAnalog = 0;

void setup()
{
   Serial.begin(9600);  //Start serial communication
   pinMode (7, OUTPUT);
   pinMode (6, OUTPUT);
   pinMode (5, OUTPUT);
   myservo.attach(8);
 }


void loop() {

   lights1(); 
   motor();

}

void lights1() {
   Serial.println(analogRead(lightPin)); //Write the value of the photoresistor to the serial monitor.
   TranslateAnalog = analogRead(lightPin); //Read the Photoresistor value
  
   //If Photoresistor Value is above 355 it will disable LED
   if(TranslateAnalog>355) {
     digitalWrite(5, LOW);
     digitalWrite(6, LOW);
     digitalWrite(7, HIGH);
   }
   //If Photoreistor Value is below 355 it will turn LED ON
   if(TranslateAnalog<355) {
     digitalWrite(6, HIGH);
     digitalWrite(7, LOW);
     delay(500);
     digitalWrite(5, HIGH);
     delay(490);
   }

}


void motor() {
 for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
   // in steps of 1 degree
   myservo.write(pos);              // tell servo to go to position in variable 'pos'
   delay(15);                       // waits 15ms for the servo to reach the position
 }
 for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
   myservo.write(pos);              // tell servo to go to position in variable 'pos'
   delay(15);                       // waits 15ms for the servo to reach the position
 }
}

=========================================

Hi,

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html . Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks… Tom… :slight_smile:

all done thanks

In your motor() function you are taking 360 steps with 15mS between them. The processor spends 5.4-seconds in this function doing those steps because you're using delay().

If you hold your hand in the beam for >5.5 seconds I suspect you'll see the switch you want. This is because once the processor finishes motor() it speeds through lights1() very quickly; your chances of picking the fraction of a second it's looking at the analog input every 5.4-secs is pretty small.

You need to use millis() timing to allow the processor to do more than one thing at a time.

okay thanks. ill teach myself millis

Here’s a revised motor() function that’ll get you started (not tested…)

void motor() 
{
    static bool
        bDirection = true;
    static unsigned byte
        pos = 0;
    static unsigned long
        timeServo = 0;

    if( (millis() - timeServo) < 15 )
        return;        
    timeServo = millis();
    
    myservo.write(pos);

    if( bDirection == true )
    {
        pos++;
        if( pos == 180 )
            bDirection = false;
    }//if
    else
    {
        pos--;
        if( pos == 0 )
            bDirection = true;
                
    }//else
    
}//motor

You’ll probably want to do something about the delay() calls in your lights1() function too; when they’re called, the processor won’t be able to update the motor either so you’ll see a stutter when the analog input is < 355…