I got this code to blink for a duration of time after the button is pressed, I can change the duration ( how long the LED blinks) and the interval (how long the LED stays on), I need to add in a 30 minute long int between each time the led turns on. I am looking to blink the LED every 30 minutes for 8 hours. Ive been trying to crack this for 3 days now I need help
const int trigger = 2;
const int ledPin = 10;
boolean ledState = LOW;
boolean previousButtonState = HIGH;
boolean buttonState = LOW;
boolean serialCheck = LOW;
long duration = 0;
unsigned long previousMillis = 0;
// constants won't change : how long LED is on
const long interval = 1000;
void setup()
{
Serial.begin(9600);
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
pinMode(trigger,INPUT);
}
void loop()
{
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= interval && buttonState == HIGH )
{
previousMillis = currentMillis;
// Serial.println(buttonState);
// if(buttonState == HIGH)
// {
if (ledState == LOW)
{
ledState = HIGH;
Serial.println("led set high");
}
else
{
ledState = LOW;
}
// }
if (millis() >= duration)
{
Serial.println("duration completed");
previousButtonState = buttonState;
buttonState = LOW;
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
if(digitalRead(trigger) == HIGH)
{
if(previousButtonState == HIGH)
{
Serial.println("pressed");
// Serial.println(currentMillis);
buttonState = HIGH;
}
else
{
buttonState = LOW;
}
delay(150); //debouncing
duration = millis() + 14000; // How long the loop lasts
}
}
Assuming that code works as intended, just add another timer, similar to how you currently do it, that executes your current code every 30 mins worth of milliseconds.
Though personally I'd set it up as a state machine, ie
static char state = 0;
switch( state )
{
case 0:
{
// do blink once code here
// ...
state = 1;
}
break;
case 1:
{
if(/*check 30 mins passed here*/ )
{
state = 0;
}
}
break;
}
please excuse my lack of knowledge, I had no idea state machine was a thing are there any good tutorials on it? Do I put the code you have mentioned in the setup or loop section of the sketch?
There's probably thousands of tutorials on it, been used since before Arduinos were around.
In your situation, you'd put it in your loop.
If you look at the snippet I posted, you'll see how it works, each case is a state, where that code will be executed until you specify a different state to run by changing the 'state' variable.
So you will have 2 states, one that waits for 30 mins, and one that blinks the led. In your situation these states will alternate.
At the time of writing this you have started 4 Threads including this one although your total number of Posts is only 6.
As far as I can see all your questions relate to the same project.
So please stop creating new Threads about the same project. Keep all the stuff in one Thread where someone trying to help you can easily see all of the information and all of the replies. There is nothing more frustrating (and time wasting) than writing an answer that someone else has already provided.
Although this is not what you want, it should give you some ideas.
//Blink without Delay skeleton
//4 examples demonstrated
//
//LED wiring options
//=============================================
//Depending which way your LEDs are wired, uncomment the next line.
//#define PlusEqualsON
#ifdef PlusEqualsON
//wired so +5V turns LED ON
#define ledON HIGH
#define ledOFF LOW
//=========================
#else
//wired so +5V turns LED OFF
#define ledON LOW
#define ledOFF HIGH
//=========================
#endif
//switch wiring options
//=============================================
//Depending which way your switches are wired, uncomment the next line.
#define PushEqualsLOW
#ifdef PushEqualsLOW
//pushing the switch makes pin LOW
#define Pushed LOW
#define Released HIGH
//=========================
#else
//pushing the switch makes pin HIGH
#define Pushed HIGH
#define Released LOW
//=========================
#endif
//=============================================
unsigned long currentMillis;
unsigned long pin13Millis;
unsigned long pin12Millis;
unsigned long pin11Millis;
unsigned long SwitchMillis;
//if these are not changed in the sketch, they can be const
unsigned long debounceMillis = 100UL; //100ms
unsigned long ledOnTime = 5*1000UL; //5 seconds
byte laststartSwitchState = HIGH;
byte buttonState = HIGH;
byte counter = 0;
//the following are enable/disable flags
//some of these might not be used in this sketch
boolean flag13 = true;
boolean flag12 = true;
boolean flag11 = true;
boolean flag10 = true;
const byte startSwitch = 2; //pushed = LOW
const byte testSwitch = 3; //pushed = LOW
//**********************************************************************
void setup()
{
Serial.begin(9600);
digitalWrite(13,ledOFF);
pinMode(13, OUTPUT);
digitalWrite(12,ledOFF);
pinMode(12, OUTPUT);
digitalWrite(11,ledOFF);
pinMode(11, OUTPUT);
digitalWrite(10,ledOFF);
pinMode(10, OUTPUT);
pinMode(startSwitch, INPUT_PULLUP); //pushed = LOW
pinMode(testSwitch, INPUT_PULLUP); //pushed = LOW
} // >>>>>>>>>>>>>> E N D O F s e t u p ( ) <<<<<<<<<<<<<<<<<
void loop()
{
//save the current time
currentMillis = millis();
//************************************* E x a m p l e 1
//toggle pin 13 every 200mS
//has 200ms or more gone by?
if (currentMillis - pin13Millis >= 200UL)
{
//code here runs every 200ms
//get ready for next iteration
pin13Millis = pin13Millis + 200UL;
//toggle pin 13
digitalWrite(13,!digitalRead(13));
}
//************************************* E x a m p l e 2
//at power up, pin 12 LED goes ON, after 3 seconds goes OFF and stays OFF
//could be used as a powerup reset signal
if (flag12 == true && currentMillis - pin12Millis <= 3000UL)
{
//code here runs for 3 seconds after power up, then stops
digitalWrite(12,ledON);
}
else
{
digitalWrite(12,ledOFF);
//disable further pin 12 control
flag12 = false;
}
//************************************* E x a m p l e 3
//if testSwitch is pushed and released
//pin 11 LED goes ON for 5 seconds, then goes OFF
buttonState = digitalRead(testSwitch);
//are we are allowed to check the switch and is it pressed?
if(flag11 == true && buttonState == Pushed)
{
//enable timing of LED on pin 11
flag11 = false; //false --> timing is enabled
//turn LED ON
digitalWrite(11,ledON);
//record the time LED turned ON
pin11Millis = currentMillis;
}
//are we allowed and is it time to control pin 11
if (flag11 == false && currentMillis - pin11Millis >= ledOnTime)
{
//if enabled, code here runs after ledOnTime ms goes by
digitalWrite(11,ledOFF);
//allow switch press detection again
flag11 = true; //true --> switch monitoring is enabled
}
//************************************* E x a m p l e 4
//is it time to check the switches?
//in particular, pushing startSwitch will turn ON/OFF (toggle) an output pin 10
//is it time to check the switches
if (currentMillis - SwitchMillis >= debounceMillis)
{
//code here runs every debounceMillis ms
//get ready for the next iteration
SwitchMillis += debounceMillis;
//go and check the switches
checkSwitches();
}
//*********************************
//put other non-blocking stuff here
//*********************************
} // >>>>>>>>>>>>>> E N D O F l o o p ( ) <<<<<<<<<<<<<<<<<
//======================================================================
// F U N C T I O N S
//======================================================================
//****************** c h e c k S w i t c h e s ( ) *********************
//switches are checked every debounceValue milli seconds
//no minimum switch press time is validated with this code (i.e. No glitch filter)
void checkSwitches()
{
//re-usable for all the switches
boolean thisState;
//************************************* E x a m p l e Push ON push OFF (toggle)
//check if this switch has changed state
thisState = digitalRead(startSwitch);
if (thisState != laststartSwitchState)
{
//update the switch state
laststartSwitchState = thisState;
//this switch position has changed so do some stuff
//"HIGH condition code"
//switch went from LOW to HIGH
if(thisState == HIGH)
{
//Do some HIGH switch stuff here
}
//"LOW condition code"
//switch went from HIGH to LOW
else
{
//Do some LOW switch stuff here
digitalWrite(10, !digitalRead(10));
//print number of pushes
counter++;
Serial.println(counter);
}
} //END of startSwitch code
//*****************************************
//similar code for other switches goes here
//*****************************************
} //END of checkSwitches()
//**********************************************************************
//======================================================================
// E N D O F C O D E
//======================================================================
Moiraredneck:
I had no idea state machine was a thing are there any good tutorials on it?
I wrote a short tutorial on simple state machines, especially for use with millis(). I also have a couple of millis() examples for common tasks people have asked me for help. I don't have the exact code you are asking about, but I do have one on different flashing times and how to handle events after a push button.
Thanks so much everybody for your help, James well done with the millis() examples by far the easiest to understand code, especially the nice plain English comments are brilliant for someone at my beginner level. Larry and Tammy thanks for your help and taking the time to reply, but right now I cant understand how to implement those types of code,
Think i'm somewhere closer to my goal having difficulty with lines 45-48. Is it possible to put a "blink" into the code this way?
// On and Off Times (as int, max=32secs)
const unsigned int onTime = 500; //LED on for half second
const unsigned int offTime = 7000; // LED off fo 7 seconds
const unsigned long waitTime = 10000; //will waitTime (LED off 30 mins) work as a command?
// Tracks the last time event fired
unsigned long previousMillis=0;
// Interval is how long we wait
int interval = onTime;
int switchState = 0;
// Used to track if LED should be on or off
boolean LED13state = true;
// Usual Setup Stuff
void setup() {
Serial.begin(9600);
pinMode(13,OUTPUT);
pinMode(2,INPUT);
}
void loop() {
switchState = digitalRead(2);//is the push button pressed?
if (switchState == LOW){
//the button is not pressed
digitalWrite(2, LOW);
}
else{
// Set Pin 13 to state of LED13state each timethrough loop()
// If LED13State hasn't changed, neither will the pin
digitalWrite(13, LED13state);
// 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 ((unsigned long)(currentMillis - previousMillis) >= interval) {
// Change wait interval, based on current LED state
if (LED13state) {
// LED is currently on, set time to stay off
interval = offTime; //why won't this wait 7 seconds, blink pin13 then wait 10 seconds?
digitalWrite(13,HIGH);
delay(250);
digitalWrite(13,LOW);
interval = waitTime;
} else {
// LED is currently off, set time to stay on
interval = onTime;
}
// Toggle the LED's state, Fancy, eh!?
LED13state = !(LED13state);
// Save the current time to compare "later"
previousMillis = currentMillis;
}
}
}