deicool
September 25, 2022, 12:18pm
1
Hello
I am a newbie and am trying to implement a delay in the circuit. I tried 2 ways and both are not working.
delay(5000)
For loop
Please help.
Thanks.
Deepak
---------Code--------
unsigned long value = 0UL;
unsigned long maxValue = 32000000UL;
int relayPin = 2;
void setup() {
Serial.begin(9600);
pinMode(relayPin, OUTPUT);
}
void loop() {
if(value++ >= maxValue)
{
/* The Arduino executes this code approx. once every second
* (assuming clock speed = 16MHz)
*/
Serial.println("LIGHTS ON");
digitalWrite(relayPin, HIGH);
Serial.println("LIGHTS OFF");
digitalWrite(relayPin, LOW);
// Don't forget to reset the counter
value = 0UL;
}
}
-------Code---------
------Article-------
(I removed the IR Receiver from my circuit)
------Article-------
deicool:
delay(5000)
Please show us that one, it has a better chance of working. Also you posted in the wrong sub forum. This one is for IDE questions.
deicool
September 25, 2022, 12:29pm
3
int relayPin = 2;
void setup() {
Serial.begin(9600);
pinMode(relayPin, OUTPUT);
}
void loop() {
Serial.println("LIGHTS ON");
digitalWrite(relayPin, HIGH);
Serial.println("LIGHTS OFF");
digitalWrite(relayPin, LOW);
delay(5000);
}
Thanks for your reply.
1000 milli seconds is one second
unsigned long lasttimevalue = millis();
unsigned long maxValue = 1000;
int relayPin = 2;
bool tick = true;
void setup() {
Serial.begin(9600);
pinMode(relayPin, OUTPUT);
}
void loop() {
if ( (millis() - lasttimevalue) >= maxValue )
{
/* The Arduino executes this code approx. once every second
(assuming clock speed = 16MHz)
*/
if (tick)
{
Serial.println("LIGHTS ON");
digitalWrite(relayPin, HIGH);
tick = !tick;
// Don't forget to reset the counter
lasttimevalue = millis();
} else {
Serial.println("LIGHTS OFF");
digitalWrite(relayPin, LOW);
tick = !tick;
lasttimevalue = millis();
}
}
}
perhaps
deicool:
Danged if i can see either in code above....
You turned the lights on and then turn them off a few microseconds later. What do you expect to see?
deicool
September 25, 2022, 12:39pm
7
Even the code below does not work:
int relayPin = 2;
void setup() {
Serial.begin(9600);
pinMode(relayPin, OUTPUT);
}
void loop() {
Serial.println("LIGHTS ON");
digitalWrite(relayPin, HIGH);
delay(5000);
Serial.println("LIGHTS OFF");
digitalWrite(relayPin, LOW);
}
int relayPin = 2;
void setup() {
Serial.begin(9600);
pinMode(relayPin, OUTPUT);
}
void loop() {
Serial.println("LIGHTS ON");
digitalWrite(relayPin, HIGH);
delay(1000);
Serial.println("LIGHTS OFF");
digitalWrite(relayPin, LOW);
delay(1000);
}
perhaps
You can see something blink about 7.5 times a second. Faster than that and you cannot see it.
Does not work is not a very helpful issue description.
How long do you think it takes for the lights to go off, loop to return to the start and turn the lights on again? Microseconds.
See?
Your first one was
repeat
{
on
off
wait
}
now you expect
repeat
{
on
wait
off
}
to work.
deicool
September 25, 2022, 12:54pm
10
anon57585045:
now you expect
repeat
{
on
wait
off
}
to work.
I tried to create a wait for 5 seconds?
delay(5000)
Walk through your programs step by step. It's obvious.
Also, the complete solution was posted for you.
deicool
September 25, 2022, 12:57pm
13
unsigned long value = 0UL;
unsigned long maxValue = 16000000UL;
int relayPin = 2;
void setup() {
Serial.begin(9600);
pinMode(relayPin, OUTPUT);
}
void loop() {
if(value++ >= maxValue)
{
/* The Arduino executes this code approx. once every second
* (assuming clock speed = 16MHz)
*/
Serial.println("LIGHTS ON");
digitalWrite(relayPin, HIGH);
Serial.println("LIGHTS OFF");
digitalWrite(relayPin, LOW);
// Don't forget to reset the counter
value = 0UL;
}
long x = 0;
do {
x = x+1;
} while (x < 10000000000);
}
You are not thinking about the solutions that you have been given. Spend some time with it.
1 Like
What sensors? How does this check them?
deicool
September 25, 2022, 1:02pm
16
Please ignore the comment.
Please edit and delete it.
1 Like
Thank you for "liking" my posts. Does it mean you figured out the error?
deicool
September 25, 2022, 1:07pm
19
Well, I got an alternative solution.
1000 milli seconds is one second
unsigned long lasttimevalue = millis();
unsigned long maxValue = 1000;
int relayPin = 2;
bool tick = true;
void setup() {
Serial.begin(9600);
pinMode(relayPin, OUTPUT);
}
void loop() {
if ( (millis() - lasttimevalue) >= maxValue )
{
/* The Arduino executes this code approx. once every second
(assuming clock speed = 16MHz)
*/
if (tick)
{
Serial.println("LIGHTS ON");
digitalWrite(relayPin, HIGH);
tick = !tick;
…
But still can't figure out why "delay(5000)" is not working in my relay-circuit. It works in case of LED perfectly.
You were told several times. Repeating it won't help.
Also a complete working solution was already posted.