Turning on led while servo is moving

I just got to play around with Arduino lately so I'm really new on this stuff and sorry for the grammar error.

I got a work where I need to turn on 2 led when a servo motor move from 0 deg to 180 deg and from 180 to 0 for the second led whitê a delay of 4 hours between each cycle.. so the led should be continuously lighted up for each move

here is the code i am using


#include <Servo.h>

Servo myservo; 
int pos = 0;  
const int LED1 = 13; // leg for 0 ged
const int LED2 = 12; // leg for 180 deg

void setup() {
  myservo.attach(9);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
}

void loop() {

    
  for (pos = 0; pos <= 180; pos += 1) { /* goes from 0 degrees to 180 degrees in steps of 1 degree */
    myservo.write(pos);           
    delay(15); 
    if (pos == 0){
          digitalWrite(LED1, HIGH);
          }
  }

  for (pos = 180; pos >= 0; pos -= 1) { /* goes from 180 degrees to 0 degrees */
    myservo.write(pos);           
    delay(15);    

  } 
      if (pos == 180){
          digitalWrite(LED1, LOW);
          digitalWrite(LED2, HIGH);
          delay(500);
         
          }
  }

any one can fix this for me please

Your for() loops are not the same. In the second one, you count down from 180 to 0. When that is done, pos will be -1 so the test if if(pos==180) will NEVER be true.

Much easier to

  1. turn on LED
  2. execute for() loop from 0..180
  3. turn off first LED, turn on second LED
  4. execute for() loop from 180..0

No additional if() statements required.

As for the 4 hour delay, look at the Blink Without Delay example in the IDE (File->examples->02.Digital->Blink WIthout Delay) to learn how to track elapsed time without blocking other code from running.

1 Like
#include <Servo.h>

Servo myservo; 
int pos = 0;  
const int LED1 = 13; // leg for 0 ged
const int LED2 = 12; // leg for 180 deg

void setup() {
  myservo.attach(9);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
}

void loop() {
    digitalWrite(LED1, HIGH);
  for (pos = 0; pos <= 180; pos += 1) { /* goes from 0 degrees to 180 degrees in steps of 1 degree */
    myservo.write(pos);           
    delay(15); 
  }

  for (pos = 180; pos >= 0; pos -= 1) { /* goes from 180 degrees to 0 degrees */
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, HIGH);
    myservo.write(pos);           
    delay(15);    
    digitalWrite(LED2, LOW);
  } 
  }

Like this ?
Now i want to make a 4h delay between the 0..180 cycle and 180..0 cycle can u teach me how ?

The first for() loop looks correct, the second one does not need to digitalWrite every time through the loop.

digitalWrite(LED1, LOW);
digitalWrite(LED2, HIGH);
for (pos = 180; pos >= 0; pos -= 1) { /* goes from 180 degrees to 0 degrees */
    myservo.write(pos);           
    delay(15);    
}
digitalWrite(LED2, LOW);

Now, go learn the Blink Without Delay example

1 Like

I really didn't get it well and i still new in programing arduino would u help me to do it please ?
i am trying to apply a delay between both cycles first then work on led

No, that is not how this forum works. If you want to hire somebody, go over to the Gigs and Collabs section and expect to pay.

If you give it a try and run into trouble, post your best effort, along with what went wrong, and people will help.

consider

#include <Servo.h>
Servo myservo;

int pos = 0;
int dir = 1;

const int LED1 = 13; // leg for 0 ged
const int LED2 = 12; // leg for 180 deg

void setup() {
    myservo.attach(9);
    pinMode(LED1, OUTPUT);
    pinMode(LED2, OUTPUT);
}

void loop() {
    pos += dir;
    myservo.write(pos);

    if (180 <= pos)  {
        dir = -dir;
        digitalWrite (LED1, HIGH);
        digitalWrite (LED2, LOW);
    }
    else if (0 >= pos)  {
        dir = -dir;
        digitalWrite (LED1, LOW);
        digitalWrite (LED2, HIGH);
    }

    delay (15);
}

Here is a generic BWD TIMER example that you might find helpful with learning millis() timing.


//********************************************************************************
//   Version    YY/MM/DD    Description
//   1.00       21/06/12    Running sketch
//

#define LEDon                        HIGH
#define LEDoff                       LOW

#define CLOSED                       LOW
#define OPENED                       HIGH

#define PRESSED                      LOW
#define RELEASED                     HIGH

#define ENABLED                      true
#define DISABLED                     false

//***************************************************************
bool LEDflag                       = DISABLED;

const byte heartbeatLED            = 13;       //[PIN 13]---[220R]---[A->|-K]---GND
const byte testLED                 = 12;

const byte mySwitch                = 2;        //+5V---[50k Pullup]---Pin2---[mySwitch]---GND

byte lastMySwitchState             = OPENED;

//timing stuff
unsigned long heartbeatMillis;
unsigned long switchMillis;
unsigned long ledMillis;

const unsigned long ledInterval    = 5000ul;   //5 seconds


//***************************************************************
void setup()
{
  pinMode(heartbeatLED, OUTPUT);
  pinMode(testLED, OUTPUT);

  pinMode(mySwitch, INPUT_PULLUP);

} //END of setup()


//***************************************************************
void loop()
{
  //*************************************                          h e a r t b e a t   T I M E R
  //to see if the sketch is blocking,
  //is it time to toggle the LED ?
  if (millis() - heartbeatMillis >= 500)
  {
    //restart the TIMER
    heartbeatMillis = millis();

    //toggle the LED
    digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
  }

  //*************************************                          c h e c k S w i t c h   T I M E R
  //is it time to read the switches ?
  if (millis() - switchMillis >= 50)
  {
    //restart the TIMER
    switchMillis = millis();

    //go read the switches
    checkSwitches();
  }
  
  //*************************************                          t e s t L E D   T I M E R
  //if enabled, has the testLED TIMER expired ?
  if (LEDflag == ENABLED && millis() - ledMillis >= ledInterval)
  {
    LEDflag = DISABLED;

    digitalWrite(testLED, LEDoff);
  }


  //*************************************
  //other non blocking code goes here
  //*************************************


} //END of loop()


//********************************************************************************
void checkSwitches()
{
  //*********************************************                    m y S w i t c h
  //runSwitch code
  byte currentState = digitalRead(mySwitch);

  //**********************
  //was there a change in state ?
  if (lastMySwitchState != currentState)
  {
    //update to the new state
    lastMySwitchState = currentState;

    //**********************                                         C L O S E D
    //if LED TIMER is disabled, is this switch closed ?
    if (LEDflag == DISABLED && currentState == CLOSED)
    {
      //enable the LED TIMER
      LEDflag = ENABLED;

      //start the LED TIMER
      ledMillis = millis();

      digitalWrite(testLED, LEDon);
    }

  } //END of mySwitch code


  //*********************************************                    o t h e r S w i t c h e s
  //next switch code
  //*********************************************

} //END of   checkSwitches()


//********************************************************************************

Thank you guys.. i will do my best to learn it and apply it to my code.. then share it with u again

const unsigned long second = 1000;
const unsigned long minute = 60 * second;
const unsigned long hour = 60 * minute;
const unsigned long day = 24 * hour;

  delay(4 * hour);

i ended by writting this code, it works correctly but i want to link the linghting of leds with the status of the servo to check if it is borken or succeflly did his last cycle because i am going to put +6h delay between cycles
With this code even the servo is broken the leds continue to light on

#include <Servo.h>

Servo myservo; 
int pos = 0;  
const int LED1 = 12; // leg for 0 ged
const int LED2 = 4; // leg for 180 deg

void setup() {
  myservo.attach(9);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
 }

void loop() {    
  for (pos = 0; pos <= 180; pos += 1) { /* goes from 0 degrees to 180 degrees in steps of 1 degree */
       if (pos > 0  && pos < 180) {digitalWrite(LED1, HIGH);} else{
        digitalWrite(LED1, LOW);
        }   
 myservo.write(pos);  
      
    delay(20); 
  }

delay(200000);

for (pos = 180; pos >= 0; pos -= 1) { /* goes from 180 degrees to 0 degrees */

  if (pos < 180 && pos > 0) {digitalWrite(LED2, HIGH);} else{
        digitalWrite(LED2, LOW);
        }   
    myservo.write(pos);           
    delay(20);    
}
delay(200000);

    }

Anyone can guide me please ?

it is so much easier to read code when properly indented

#include <Servo.h>

Servo myservo;

int pos = 0;
const int LED1 = 12; // leg for 0 ged
const int LED2 = 4; // leg for 180 deg
int val = 0;     // variable for reading the pin status

void setup() {
    myservo.attach(9);
    pinMode(LED1, OUTPUT);
    pinMode(LED2, OUTPUT);
}

void loop() {
    /* goes from 0 degrees to 180 degrees in steps of 1 degree */
    for (pos = 0; pos <= 180; pos += 1) {
        if (pos > 0  && pos < 180) {
            digitalWrite(LED1, HIGH);
        }
        else {
            digitalWrite(LED1, LOW);
        }
        myservo.write(pos);
        delay(20);
    }

    delay(200000);

    /* goes from 180 degrees to 0 degrees */
    for (pos = 180; pos >= 0; pos -= 1) {
        if (pos < 180 && pos > 0) {
            digitalWrite(LED2, HIGH);
        }
        else {
            digitalWrite(LED2, LOW);
        }
        myservo.write(pos);
        delay(20);
    }

    delay(200000);
}

I don't understand why you are doing this:

  for (pos = 0; pos <= 180; pos += 1) { /* goes from 0 degrees to 180 degrees in steps of 1 degree */
    if (pos > 0  && pos < 180) {
      digitalWrite(LED1, HIGH);
    } else {
      digitalWrite(LED1, LOW);
    }
    myservo.write(pos);

    delay(20);
  }

instead of this:

  digitalWrite(LED1, HIGH);
  for (pos = 0; pos <= 180; pos += 1) { /* goes from 0 degrees to 180 degrees in steps of 1 degree */
    myservo.write(pos);
    delay(20);
  }
  digitalWrite(LED1, LOW);

There is no feedback from the servo so there is no way to know if it is broken unless you design some method (via hardware such as micro switch) to monitor whether it has moved or not.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.