question, delay or not , how to do

Hey i have to make a project and i can't to do :frowning: .
I have a relay , when is on , he give me 5V . This 5v entering in the analog pin A0 . I need when i have a impuls ( in A0 with 3v or more ) to wait 3 second , after he turn on me a LED , from a digital pin . And i want to set time ( how long is on the LED ) , and how long to wait LED after implus to go ON ( this 3 second it was a example )

Thank you!

I have a relay , when is on , he give me 5V . This 5v entering in the analog pin A0 . I need when i have a impuls ( in A0 with 3v or more ) to wait 3 second , after he turn on me a LED , from a digital pin . And i want to set time ( how long is on the LED ) , and how long to wait LED after implus to go ON ( this 3 second it was a example )

When pin A0 goes HIGH
--- wait for 3 seconds and then turn on a led
--- wait for X seconds and then turn off the led

No need to see if pin A0 changes, if the relay turns OFF or ON again?

When do you want to set those two times? While the sketch is running?

This sketch will not grow bigger and need to do more?
I think that so far, more is implied.

GoForSmoke:
When pin A0 goes HIGH
--- wait for 3 seconds and then turn on a led
--- wait for X seconds and then turn off the led

No need to see if pin A0 changes, if the relay turns OFF or ON again?

When do you want to set those two times? While the sketch is running?

This sketch will not grow bigger and need to do more?
I think that so far, more is implied.

i don't need to see anything on serial . I want to set in sketch the times . The relay is actioned manual by me . So i need only led to get on in 3 second after in A0 will come 3,5V or more . and i want to set and how long il will be on ( the led ) .

Do you want to be able to use an input device to tell the arduino how long to keep the LED on? A potentiometer read on an analog pin would be one way. Or are you saying that you want to be able to specify the time as a constant in the sketch?

As long as you have complete control of circumstances and that's ALL you want it to do.....
if you want more later, don't ask me to modify this.

This crappy example does not read A0. It uses a jumper stuck in pin 2.
Ground the jumper on the metal USB connector box to make this work.

For you the relay sense code line would be

while ( analoglRead( A0 ) < (int)( 35L * 1024L / 50L )); // not tested. 1023 is one step < 5V so I put 1024

instead of

while ( digitalRead( buttonPin ) == HIGH ); // tested

// this is dirty dead end code, use or expand at your own risk

byte ledPin = 13; // Arduino board led
byte buttonPin = 2;

word turnLedOnAfterTime = 3000;
word turnLedOffAfterTime = 10000;

void setup()
{
  pinMode( ledPin, OUTPUT );
  pinMode( buttonPin, INPUT_PULLUP );
  
  while ( digitalRead( buttonPin ) == HIGH );
  
  delay( turnLedOnAfterTime );
  digitalWrite( ledPin, HIGH );

  delay( turnLedOffAfterTime );
  digitalWrite( ledPin, LOW );
}

void loop()
{
}

wildbill:
Do you want to be able to use an input device to tell the arduino how long to keep the LED on? A potentiometer read on an analog pin would be one way. Or are you saying that you want to be able to specify the time as a constant in the sketch?

No , but i think is easy , because i generate 1 impuls ( the current i send from relay will stand
a moment , a flash ) , and when arduino detect this flash current , will stay 3 second , after give to go the led on . The led stay on 1 second . This is all i want :frowning:

GoForSmoke:
As long as you have complete control of circumstances and that's ALL you want it to do.....
if you want more later, don't ask me to modify this.

This crappy example does not read A0. It uses a jumper stuck in pin 2.
Ground the jumper on the metal USB connector box to make this work.

For you the relay sense code line would be

while ( analoglRead( A0 ) < (int)( 35L * 1024L / 50L )); // not tested. 1023 is one step < 5V so I put 1024

instead of

while ( digitalRead( buttonPin ) == HIGH ); // tested

// this is dirty dead end code, use or expand at your own risk

byte ledPin = 13; // Arduino board led
byte buttonPin = 2;

word turnLedOnAfterTime = 3000;
word turnLedOffAfterTime = 10000;

void setup()
{
  pinMode( ledPin, OUTPUT );
  pinMode( buttonPin, INPUT_PULLUP );
 
  while ( digitalRead( buttonPin ) == HIGH );
 
  delay( turnLedOnAfterTime );
  digitalWrite( ledPin, HIGH );

delay( turnLedOffAfterTime );
  digitalWrite( ledPin, LOW );
}

void loop()
{
}

and this program is not work :frowning: i don't have button , so i put just 5V ; and i put " while ( analoglRead( A0 ) < (int)( 35L * 1024L / 50L )); " and don't work , i have a error with analoglRead :frowning:

Should be analogRead, not analoglRead.
My typo error!

The tested sketch uses a jumper wire for the button. A piece of wire with bare ends is enough.
One end goes in pin 2 and when I ground the other end, that is 'button pushed'.
The metal box that the USB plugs into is grounded, I touch the wire to that.

if i put while ( analoglRead( A0 ) < (int)( 35L * 1023L / 50L ));; , the error is ' analoglRead ' was not declared in this scope .
i was try to declare analoglRead ( int analoglRead ; const analoglRead ; )... but is not work .

Because analoglRead is spelled wrong.

It should be analogRead without the extra l.

GoForSmoke:
Should be analogRead, not analoglRead.
My typo error!

The tested sketch uses a jumper wire for the button. A piece of wire with bare ends is enough.
One end goes in pin 2 and when I ground the other end, that is 'button pushed'.
The metal box that the USB plugs into is grounded, I touch the wire to that.

now is work with analog pin ( A0 ) but is work only 1 time ; after is work only i reset the Arduino . Why ? I think we need to put the program in void loop , no ?

Thank you very very much ! It's WORK . I put in void loop the sequence of program .

I will post the program

byte ledPin = 13; // Arduino board led

word turnLedOnAfterTime = 3000;
word turnLedOffAfterTime = 1000;

void setup()
{
  pinMode( ledPin, OUTPUT );
    
}

void loop()
{
 while ( analogRead( A0 ) < (int)( 35L * 1023L / 50L ));;
{
delay( turnLedOnAfterTime );
  digitalWrite( ledPin, HIGH );

  delay( turnLedOffAfterTime );
  digitalWrite( ledPin, LOW );
}
}

Thank you a lot !

edgaryo:
now is work with analog pin ( A0 ) but is work only 1 time ; after is work only i reset the Arduino . Why ? I think we need to put the program in void loop , no ?

Only once because when I asked you did not say more than once.

"We" don't put it in loop(). You do.

I have better ways of doing that that allow for much more, different things all work together those ways.

The comment at the top;
// this is dirty dead end code, use or expand at your own risk
It is not a joke.

and is one more problem , is very very sensible . If i put the arduino in hand the led will be flash :expressionless: . Why ? You can do a better program ? :frowning: Please

edgaryo:
and is one more problem , is very very sensible . If i put the arduino in hand the led will be flash :expressionless: . Why ? You can do a better program ? :frowning: Please

Something is shorted?
The power to the relay, is the ground of that connected to the ground of the Arduino before either is powered up?
Except where isolators are used, all grounds for all power should be connected.

Better program will take that you learn some ideas, like how to make things happen on time without stopping everything else.

This is a cleaned-up version of the Arduino Example Sketch, BlinkWithoutDelay.
I left the original comments in because it is Public Domain.

/* Blink without Delay -- with minor fixes by GFS

  Turns on and off a light emitting diode(LED) connected to a digital  
 pin, without using the delay() function.  This means that other code
 can run at the same time without being interrupted by the LED code.
  
  The circuit:
 * LED attached from pin 13 to ground. 
 * Note: on most Arduinos, there is already an LED on the board
 that's attached to pin 13, so no hardware is needed for this example.


 created 2005
 by David A. Mellis
 modified 8 Feb 2010
 by Paul Stoffregen
 
 This example code is in the public domain.
 
 http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
 
 GFS fixes and modifications -- May 5 2013:
 . changed time variables to be ALL unsigned longs, as they should be.
 . added UL to numbers being assigned to unsigned longs as should be.
 . changed the variable name 'interval' to 'blinkTime' as interval is now a
 word used by the IDE, it shows up red (like the word 'if') instead of black.
 . changed the if-else logic to change the ledState variable to 1 line XOR logic.
 . added comments about adding more tasks to the sketch.
 
 */

// constants won't change. Used here to 
// set pin numbers:
const byte ledPin =  13;      // the number of the LED pin

// Variables will change:
byte ledState = LOW;             // ledState used to set the LED

unsigned long previousMillis = 0UL;  // will store last time LED was updated

unsigned long blinkTime = 1000UL;  // interval at which to blink (milliseconds)

void setup() 
{
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);      
}

void loop()
{
  unsigned long currentMillis = millis();
 
  // here is where you'd put code that needs to be running all the time.
  
  // GFS adds -- you see the if() { } block below? You can add more blocks
  // whether if() or switch-case or whatever to do other tasks and as long
  // as they run quick without delays or prolonged loops, your sketch will
  // be responsive as if everything runs at the same time.
  // just as the blink runs on time, another task can run when a button or 
  // sensor or serial data comes in or changes. 
  // simple commands run in less than a millionth of a second so you can pack
  // a good bit of process into a block and still run quick. analog read takes
  // longer, about 9 per millisecond so it's best not to do a bunch of those 
  // in a row but instead 1 analog read per time through loop() so other tasks
  // can get a chance in between analog reads. 
  // it's also good to avoid using floating-point as that is slooowww and avoid
  // using C++ Strings as they mess with your RAM and suck up CPU cycles doing it.

  // Back to the original program:
  // check to see if it's time to blink the LED; that is, if the 
  // difference between the current time and last time you blinked 
  // the LED is bigger than the interval at which you want to 
  // blink the LED.
  if(currentMillis - previousMillis >= blinkTime) // is it time to change the led? 
  {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    ledState = ledState ^ 1; // ^ is logical XOR, true if the values are different
    // using logic operations can save a lot of tedious, pain to debug if's

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

GoForSmoke:

edgaryo:
and is one more problem , is very very sensible . If i put the arduino in hand the led will be flash :expressionless: . Why ? You can do a better program ? :frowning: Please

Something is shorted?
The power to the relay, is the ground of that connected to the ground of the Arduino before either is powered up?
Except where isolators are used, all grounds for all power should be connected.

Better program will take that you learn some ideas, like how to make things happen on time without stopping everything else.

This is a cleaned-up version of the Arduino Example Sketch, BlinkWithoutDelay.
I left the original comments in because it is Public Domain.

/* Blink without Delay -- with minor fixes by GFS

Turns on and off a light emitting diode(LED) connected to a digital 
pin, without using the delay() function.  This means that other code
can run at the same time without being interrupted by the LED code.
 
  The circuit:

  • LED attached from pin 13 to ground.
  • Note: on most Arduinos, there is already an LED on the board
    that's attached to pin 13, so no hardware is needed for this example.

created 2005
by David A. Mellis
modified 8 Feb 2010
by Paul Stoffregen

This example code is in the public domain.

http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay

GFS fixes and modifications -- May 5 2013:
. changed time variables to be ALL unsigned longs, as they should be.
. added UL to numbers being assigned to unsigned longs as should be.
. changed the variable name 'interval' to 'blinkTime' as interval is now a
word used by the IDE, it shows up red (like the word 'if') instead of black.
. changed the if-else logic to change the ledState variable to 1 line XOR logic.
. added comments about adding more tasks to the sketch.

*/

// constants won't change. Used here to
// set pin numbers:
const byte ledPin =  13;      // the number of the LED pin

// Variables will change:
byte ledState = LOW;             // ledState used to set the LED

unsigned long previousMillis = 0UL;  // will store last time LED was updated

unsigned long blinkTime = 1000UL;  // interval at which to blink (milliseconds)

void setup()
{
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);     
}

void loop()
{
  unsigned long currentMillis = millis();

// here is where you'd put code that needs to be running all the time.
 
  // GFS adds -- you see the if() { } block below? You can add more blocks
  // whether if() or switch-case or whatever to do other tasks and as long
  // as they run quick without delays or prolonged loops, your sketch will
  // be responsive as if everything runs at the same time.
  // just as the blink runs on time, another task can run when a button or
  // sensor or serial data comes in or changes.
  // simple commands run in less than a millionth of a second so you can pack
  // a good bit of process into a block and still run quick. analog read takes
  // longer, about 9 per millisecond so it's best not to do a bunch of those
  // in a row but instead 1 analog read per time through loop() so other tasks
  // can get a chance in between analog reads.
  // it's also good to avoid using floating-point as that is slooowww and avoid
  // using C++ Strings as they mess with your RAM and suck up CPU cycles doing it.

// Back to the original program:
  // check to see if it's time to blink the LED; that is, if the
  // difference between the current time and last time you blinked
  // the LED is bigger than the interval at which you want to
  // blink the LED.
  if(currentMillis - previousMillis >= blinkTime) // is it time to change the led?
  {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

// if the LED is off turn it on and vice-versa:
    ledState = ledState ^ 1; // ^ is logical XOR, true if the values are different
    // using logic operations can save a lot of tedious, pain to debug if's

// set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

no, nothing is shorted , all is connected at ground .... i don't know why is so sensible ....
I will try to make a better program .
Thank you ! You helped me a lot !!!

i have try to integrete analog sensor in your last program ( blink without delay ) but i can't :expressionless: . is not working ... i don't know where to put :
" sensorValue = analogRead(analogInPin);
if (sensorValue > 600 );
{

}
"

It is because the lesson of that program is making things happen on time. Not sensor input.

I wrote this

Better program will take that you learn some ideas, like how to make things happen on time without stopping everything else.

Some of the ideas are in that BlinkWithoutDelay.

using millis() to get milliseconds time since Arduino started
millis() returns an unsigned long value
unsigned long constants should have letters UL after the value --- 3000UL is a constant unsigned long value.
when unsigned numbers are subtracted, the difference will be correct even through rollover
--------- the rollover is like when a clock hour hand goes from 11 to 2 by adding 3
--------- with unsigned long the 11 o'clock is a much bigger number but the principle is the same
--------- I have a sketch that shows this and allows playing with the numbers, do you want that?

the time now minus a time before equals time passed between.

we set a start time and the time to wait then watch every time that loop() goes around.
--------- loop() goes around very fast but with every pass through loop() a little time goes by
--------- when the time actually passed is the same or more than the time to wait, then the action is done.

These ideas are how to make the led turn on and off when you want without using delay().
Delay() stops the code until delay is finished. That keeps tasks from running together.
By not using delay() to time the led, and instead using the millis() and time values, other tasks can keep running even while the led is turning on and off to blink. Hence the sketch is Blink Without Delay.

When I wrote:

I have better ways of doing that that allow for much more, different things all work together those ways.

this Blink Without Delay is a major part of that.

Here is a blog that puts things together, making multiple things happen together but still NOT reading A0.
It uses a common sense example, cooking breakfast so all is done at the same time.

The reading A0 for certain values is an EVENT just as time elapsed reaching a certain value is an EVENT.
To make the led change on time, the if checks the time passed --- event.
To make something happen when a pin changes, the if checks the pin --- event.

Loop() goes around and around and sections of code inside act on different events makes Event Driven Code.
When you use a GUI window, like editing a post, with mouse and keyboard actions, that is Event Driven.

ok :smiley: i got it :smiley: , and i was read the site " Gammon Forum : Electronics : Microprocessors : How to do multiple things at once ... like cook bacon and eggs " . Thank you ! :smiley:

edgaryo:
ok :smiley: i got it :smiley: , and i was read the site " Gammon Forum : Electronics : Microprocessors : How to do multiple things at once ... like cook bacon and eggs " . Thank you ! :smiley:

I am glad!

It is hard for me to gauge how you've been doing so this is good news.
But don't just read. Play with the code, run it and change it to test your understanding.