Converting delay to millis.? need help !!!!
/*
* Hi Everyone.
I am just starting with Arduino uno.
I was wondering if you could help me?
I would like to replace "delay" with "millis()"
My Project Out Line.
* "LED Light" On/Off with delay using "Relay Module" and "Bluetooth Hc-05"
* working fine with "delay sketch" but I cannot schedule 2 Events at the same time.
* So remodifing sketch using "Millis"
* original sketch link: https://docs.google.com/document/d/1GpPH8rG_bR2Vp282nv7kyB1Zwa0shtpDcAj3-_txnxE/pub
what will happen is when I send a command A to H the relay will be switched on and if I send a to h the relay will be switched off.
I have programmed it so that
A is relay one on (latched)
B is relay two on (latched)
C is relay one on for 30 Minutes
D is relay two on for 1 Hr
E is relay one on for 2 Hrs
F is relay two on for 5 Hrs
G is relay one on for 1 second, then relay two on then both off
H will switch relay one on and off for 1000 times
the relays switch on when the digital pin is grounded. and the relay switches off when the digital pin goes high
here is the code
*/
//==========================================
// On and Off Times (as int, max=32secs)
const unsigned int onTime = 1000;
const unsigned int offTime = 500;
// Tracks the last time event fired
unsigned long previousMillis = 0;
// Interval is how long we wait
int interval = onTime;
// Used to track if LED should be on or off
boolean LED2state = true;
//==========================================
char val; // variable to receive data from the serial port
int ledpin = 2; // LED connected to pin 2 (on-board LED)
const int LED = 13;
// ####### Setup #######
void setup()
{
pinMode(ledpin = 2, OUTPUT); // pin 2 (on-board LED) as OUTPUT
pinMode(ledpin = 3, OUTPUT); // pin 3 (on-board LED) as OUTPUT
Serial.begin(9600); // start serial communication at 115200bps
pinMode(LED, OUTPUT);
}
// ####### loop #######
void loop()
{
if ( Serial.available() ) // if data is available to read
{
;
}
val = Serial.read(); // read it and store it in 'val'
// ##### Simple LED ON/OFF with out Delay #####
if ( val == 'a' ) // if 'a' was received led 2 is switched off
{
digitalWrite(ledpin = 2, HIGH); // turn Off pin 2
}
if ( val == 'A' ) // if 'A' was received led 2 on
{
digitalWrite(ledpin = 2, LOW); // turn ON pin 2
}
if ( val == 'b' ) // if 'b' was received led 3 is switched off
{
digitalWrite(ledpin = 3, HIGH); // turn Off pin 3
}
if ( val == 'B' ) // if 'B' was received led 3 on
{
digitalWrite(ledpin = 3, LOW); // turn ON pin 3
} //else (ledpin = 3, LOW) //set led pin 3 to low state
// Need Help
// ##### Simple LED ON/OFF with Delay using Millis #####
if ( val == 'C' ) // if 'C' was received led 2 on for 30 Minutes
{
digitalWrite(ledpin = 2, LOW); // turn ON pin 2
// Grab snapshot of current time, this keeps all timing
// consistent, regardless of how much code is inside the next if-statement
unsigned long currentMillis = millis();
// Compare to previous capture to see if enough time has passed
((unsigned long)(currentMillis - previousMillis) >= interval); {
// Change wait interval, based on current LED state
if (LED2state) {
// LED is currently on, set time to stay off
interval = offTime;
} else {
// LED is currently off, set time to stay on
interval = onTime;
}
// Toggle the LED's state, Fancy, eh!?
LED2state = !(LED2state);
// Save the current time to compare "later"
previousMillis = currentMillis;
// wait 30 Minutes
digitalWrite(ledpin, HIGH); // turn Off pin 2
}
}
}
//Thanks in advance
//Krish
Discussions
Best Answer 3 years ago
Try this:
// ==========================================
// On and Off Times (as int, max=32secs)
const unsigned int onTime = 1000;
const unsigned int offTime = 500;
// Tracks the last time event fired
unsigned long previousMillis = 0;
// Interval is how long we wait
int interval = onTime;
// Used to track if LED should be on or off
boolean LED2state = true;
//==========================================
char val; // variable to receive data from the serial port
int ledpin = 2; // LED connected to pin 2 (on-board LED)
const int LED = 13;
// ####### Setup #######
void setup()
{
pinMode(ledpin = 2, OUTPUT); // pin 2 (on-board LED) as OUTPUT
pinMode(ledpin = 3, OUTPUT); // pin 3 (on-board LED) as OUTPUT
Serial.begin(9600); // start serial communication at 115200bps
pinMode(LED, OUTPUT);
}
// ####### loop #######
void loop()
{
if ( Serial.available() ) // if data is available to read
// {
// ;
// }
val = Serial.read(); // read it and store it in 'val'
// ##### Simple LED ON/OFF with out Delay #####
if ( val == 'a' ) // if 'a' was received led 2 is switched off
{
digitalWrite(ledpin = 2, HIGH); // turn Off pin 2
}
if ( val == 'A' ) // if 'A' was received led 2 on
{
digitalWrite(ledpin = 2, LOW); // turn ON pin 2
}
if ( val == 'b' ) // if 'b' was received led 3 is switched off
{
digitalWrite(ledpin = 3, HIGH); // turn Off pin 3
}
if ( val == 'B' ) // if 'B' was received led 3 on
{
digitalWrite(ledpin = 3, LOW); // turn ON pin 3
} //else (ledpin = 3, LOW) //set led pin 3 to low state
// Need Help
// ##### Simple LED ON/OFF with Delay using Millis #####
if ( val == 'C' ) // if 'C' was received led 2 on for 30 Minutes
{
// digitalWrite(ledpin = 2, LOW); // turn ON pin 2
// Grab snapshot of current time, this keeps all timing
// consistent, regardless of how much code is inside the next if-statement
unsigned long currentMillis = millis();
// Compare to previous capture to see if enough time has passed
if((currentMillis - previousMillis) >= interval) {
// Change wait interval, based on current LED state
if (LED2state) {
// LED is currently on, set time to stay off
interval = offTime;
} else {
// LED is currently off, set time to stay on
interval = onTime;
}
// Toggle the LED's state, Fancy, eh!?
LED2state = !(LED2state);
digitalWrite(ledpin = 2, LED2state);
// Save the current time to compare "later"
previousMillis = currentMillis;
// wait 30 Minutes
// digitalWrite(ledpin, HIGH); // turn Off pin 2
}
}
}
Answer 3 years ago
its working great, thank you so much for sparing your time to helping me in my project,
but for my project I need Led to stay on for at least 1 to 2 Hrs and it should not Toggle (ex: if program light to stay on for 1 hour, after 1 hour it should stay OFF until the next command).