Using the delay vs millis to sequence relays
need code
i'm new @ arduino , trying write sketch when button pressed, after amount of time, relays begin turning on. each in sequence after delay. stay on while button pressed (5v pin 2). after button released, relays turn off in reverse order after delay. currently, i've written program using delay()...can done better using millis command? if so, how?. there better way? written, after pin 2 goes low, there long delay before relays start turning off. prolly because going through delays of loop. i'll triggering photocell, there appreciated too. again, i'm new go easy on me. see below. thanx! scott
/* written scott robinson - november 2017. bible opens , several lights trigger in sequence */
/* bible light trigger photo resistor or switch. turns on relays when pr goes low. 2 sec delay between each. turns off relays when pr goes high.
uno pin setup
pin 2 - switch
pin 6 - relay 1 (in1)
pin 7 - relay 2 (in2)
pin 8 - relay 3 (in3)
pin 9 - relay 4 (in4)
pin 10 - relay 5 (in5)
pin 13 - power on/off led
*/
#define relay1 6
#define relay2 7
#define relay3 8
#define relay4 9
#define relay5 10
int ledpin = 13 ; // setup pin 13 power on led
void setup()
{
pinmode(ledpin, output) ; //turn on led pin 13
digitalwrite (ledpin, high) ;
pinmode(2, input); //setup pin 2 input switch
digitalwrite (2, low); // default low or 0
// initialise arduino data pins output
pinmode(relay1, output);
pinmode(relay2, output);
pinmode(relay3, output);
pinmode(relay4, output);
pinmode(relay5, output);
delay(500); //wait chip initialization complete
// setup - turn off relays start
digitalwrite(relay1,high); // turns relay off
digitalwrite(relay2,high); // turns relay off
digitalwrite(relay3,high); // turns relay off
digitalwrite(relay4,high); // turns relay off
digitalwrite(relay5,high); // turns relay off
}
void loop()
{
int reading2 = digitalread(2); // check pin 2 +5v if switch used
if (reading2 == 1) // read pin 2 if +5v then
{
delay(2000); // initial wait
digitalwrite(relay1,low); // turns on relays 1
delay(1000); // wait
digitalwrite(relay2,low); // turns on relay 2
delay(1000); // wait
digitalwrite(relay3,low); // turns on relay 3
delay(1000); // wait
digitalwrite(relay4,low); // turns on relay 4
delay(1000); // wait
digitalwrite(relay5,low); // turns on relay 5
}
else
{ //if pin 2 low
digitalwrite(relay5,high); // turns off relay 5
delay(1000); // wait
digitalwrite(relay4,high); // turns off relay 4
delay(1000); // wait
digitalwrite(relay3,high); // turns off relay 3
delay(1000); // wait
digitalwrite(relay2,high); // turns off relay 2
delay(1000); // wait
digitalwrite(relay1,high); // turns off relay 1
}
}
i'm new @ arduino , trying write sketch when button pressed, after amount of time, relays begin turning on. each in sequence after delay. stay on while button pressed (5v pin 2). after button released, relays turn off in reverse order after delay. currently, i've written program using delay()...can done better using millis command? if so, how?. there better way? written, after pin 2 goes low, there long delay before relays start turning off. prolly because going through delays of loop. i'll triggering photocell, there appreciated too. again, i'm new go easy on me. see below. thanx! scott
/* written scott robinson - november 2017. bible opens , several lights trigger in sequence */
/* bible light trigger photo resistor or switch. turns on relays when pr goes low. 2 sec delay between each. turns off relays when pr goes high.
uno pin setup
pin 2 - switch
pin 6 - relay 1 (in1)
pin 7 - relay 2 (in2)
pin 8 - relay 3 (in3)
pin 9 - relay 4 (in4)
pin 10 - relay 5 (in5)
pin 13 - power on/off led
*/
#define relay1 6
#define relay2 7
#define relay3 8
#define relay4 9
#define relay5 10
int ledpin = 13 ; // setup pin 13 power on led
void setup()
{
pinmode(ledpin, output) ; //turn on led pin 13
digitalwrite (ledpin, high) ;
pinmode(2, input); //setup pin 2 input switch
digitalwrite (2, low); // default low or 0
// initialise arduino data pins output
pinmode(relay1, output);
pinmode(relay2, output);
pinmode(relay3, output);
pinmode(relay4, output);
pinmode(relay5, output);
delay(500); //wait chip initialization complete
// setup - turn off relays start
digitalwrite(relay1,high); // turns relay off
digitalwrite(relay2,high); // turns relay off
digitalwrite(relay3,high); // turns relay off
digitalwrite(relay4,high); // turns relay off
digitalwrite(relay5,high); // turns relay off
}
void loop()
{
int reading2 = digitalread(2); // check pin 2 +5v if switch used
if (reading2 == 1) // read pin 2 if +5v then
{
delay(2000); // initial wait
digitalwrite(relay1,low); // turns on relays 1
delay(1000); // wait
digitalwrite(relay2,low); // turns on relay 2
delay(1000); // wait
digitalwrite(relay3,low); // turns on relay 3
delay(1000); // wait
digitalwrite(relay4,low); // turns on relay 4
delay(1000); // wait
digitalwrite(relay5,low); // turns on relay 5
}
else
{ //if pin 2 low
digitalwrite(relay5,high); // turns off relay 5
delay(1000); // wait
digitalwrite(relay4,high); // turns off relay 4
delay(1000); // wait
digitalwrite(relay3,high); // turns off relay 3
delay(1000); // wait
digitalwrite(relay2,high); // turns off relay 2
delay(1000); // wait
digitalwrite(relay1,high); // turns off relay 1
}
}
quote
can done better using millis command?absolutely. need record when switch becomes pressed. periodically, see if has been long enough time turn relay on.
you need record when switch becomes released, can see when has been released long enough need turn relay off.
the state change detection , blink without delay examples have clues need.
Arduino Forum > Using Arduino > Programming Questions > Using the delay vs millis to sequence relays
arduino
Comments
Post a Comment