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);
}
}
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
turn on LED
execute for() loop from 0..180
turn off first LED, turn on second LED
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.
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
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()
//********************************************************************************
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);
}
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.