Hi. I'm new in programming Arduino and i have a problem. I try use function millis(), if and else if. I try blink len on Pin 13 when time is between 1-5sec and this is ok but second code is problem. I want to blink my led again between 6-8sec and efect is led is only on but why i don't know. Can you help me explain this problem? Thanks
unsigned long actualTime =0;
unsigned long rememberTime = 0;
unsigned long diferentTime = 0;
void setup(){
pinMode(13,OUTPUT);
Serial.begin(9600);
}
void loop(){
actualTime = millis();
diferentTime = actualTime - rememberTime;
if (diferentTime >= 1000UL) {
digitalWrite(13, HIGH);
rememberTime = actualTime;}
else if (actualTime >=5000){
digitalWrite(13,LOW);
}
if (actualTime >= 6000UL) {
digitalWrite(13, HIGH);
rememberTime = actualTime;}
else if (actualTime >=8000)
digitalWrite(13,LOW);
Serial.println(actualTime);
}
gregory099:
Hi. I'm new in programming Arduino and i have a problem. I try use function millis(), if and else if. I try blink len on Pin 13 when time is between 1-5sec and this is ok but second code is problem. I want to blink my led again between 6-8sec and efect is led is only on but why i don't know. Can you help me explain this problem? Thanks
Your on-for-a-second/off-for-a-seccond blinking is missing the off-for-a-second part. The code will be easier to understand if you make the blinking part separate from the timing part.
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
}
void blink()
{
static unsigned long rememberTime = 0;
if (millis() - rememberTime >= 100UL)
{
rememberTime = millis();
// Toggle the LED:
if (digitalRead(LED_BUILTIN) == LOW)
digitalWrite(LED_BUILTIN, HIGH);
else
digitalWrite(LED_BUILTIN, LOW);
}
}
void loop()
{
unsigned long actualTime = millis();
// It has not been at least a second since the timer was last started
if (actualTime < 5000 || (actualTime >= 6000 && actualTime < 8000))
blink();
else
digitalWrite(LED_BUILTIN, LOW);
Serial.println(actualTime);
}
Note: I sped up the blinking since you can fit only one 2-second cycle between 6 and 8 seconds.
Hello. What i need. I need in my sketch turn on/off fun and motor dc but i try do this on led. When millis() start i wont turn led on (HIGH) when time is 1-5 sec next turn of led (LOW). Next step when time in millis() is 6 sec turn led on(HIGH) and when millis() is 9sec turn ledn off (LOW). I need use millis() because delay stop my program. I write what i need witch function delay.
// When millis() start i wont turn led on (HIGH)
// when time is 1-5 sec next turn of led (LOW).
// Next step when time in millis() is 6 sec turn led on(HIGH)
// and when millis() is 9sec turn ledn off (LOW).
// So: HIGH until 1000, LOW until 6000, HIGH until 9000, then LOW forever
const byte Motor_DOPin = LED_BUILTIN; // Use LED to represent motor
void setup()
{
pinMode(Motor_DOPin, OUTPUT);
}
void loop()
{
if (millis() < 1000)
digitalWrite(Motor_DOPin, HIGH); // HIGH until 1000
else if (millis() < 6000)
digitalWrite(Motor_DOPin, LOW); // LOW until 6000
else if (millis() < 9000)
digitalWrite(Motor_DOPin, HIGH); // HIGH until 9000
else
digitalWrite(Motor_DOPin, LOW); // Then LOW forever
}
So, from reply #6 I understand that you understand delay() stops the Arduino. But since you haven't said what else you want to do, maybe it is ok to stop?