Hello, I was working on a project when I realized I wanted to use millis() over delay. Granted, delay worked perfectly but ran into a problem when I needed a to do a more complex statement so just trying to learn the millis() function and can not figure out how to get a specific action to occur. For testing, I made a small code that uses delays and works as I desire.
Press a push button 1 time, it blinks an LED at a rate of 100ms for 5 seconds. (Button not Held down)
//button pressed once, redLED blinks for 5 seconds using delay
int redLED = 0;
int pushButton = 0;
int counter;
void setup()
{
pinMode(2, INPUT);
pinMode(3, OUTPUT);
}
void loop()
{
pushButton = digitalRead(2);
redLED = LOW;
if (pushButton == HIGH) {
for (counter = 0; counter < 25; ++counter) {
digitalWrite(3, HIGH);
delay(100); // Wait for 100 millisecond(s)
digitalWrite(3, LOW);
delay(100); // Wait for 100 millisecond(s)
}
}
}
Now, below will be a test millis() that does not function properly. It does blink the LED when pressed and held but I'm most interested in how to push that button 1 time and making the LED blink for 5 seconds at a rate of 100ms without having to hold down the button. I have looked around but can not find an example of this. To keep things simple, not looking for debounce code or anything. Just trying to find information on how to write this code to behave the same as the delay code. Thanks for any info you may have!
int redLED = 0;
int redLEDstate = LOW;
int pushButton = 0;
unsigned long previousTime;
bool blink;
void setup()
{
pinMode(2, INPUT);
pinMode(3, OUTPUT);
}
void loop()
{
unsigned long currentTime = millis();
pushButton = digitalRead(2);
redLED = LOW;
if (pushButton == HIGH && currentTime - previousTime >= 100) {
redLEDstate = !redLEDstate;
digitalWrite(3, redLEDstate);
previousTime = currentTime;
}
}
separate the code that reads the button and the code that blinks the LED
the code that reads a button sets the unused variable 'blink' that you created, to 'true'
the code that blinks the LED only checks 'blink' to see whether it should blink or not, when it's finished blinking, set 'blink' to 'false'.
Why does your program coincidentally have exactly the right boolean variable that you need, but you say nothing about it, and don't use it? Is this a school assignment template sketch that you were given?
Thanks, I’ll look at that. Sorry, I had my larger project I was working with and didn’t remove that variable. I tried posting before about other things and I think I post too much so was trying to post the bare minimum so I could get some suggestions as I know next to nothing about c programming. And no, I’m not in school. Too old to start a new career now. Lol. Just a personal project I’m playing with in my spare time
Here was the first part of the code I was working on that I had the blink but I'm not doing it right as it does the same thing as the small example. Only real difference is I have a vibration sensor in place where the button is. Only blinks when HIGH, does not continue to blink for the 5 seconds after the INPUT goes LOW. Kind of at a standstill till I can figure out how to make the redLED stay blinking for 4 or 5 seconds after my sensor goes to the HIGH state momentarily.
#define greenLED 0
#define speaker 1
#define vibSensor 2
#define redLED 3
#define speakerSwitch 4
int VibSenseInput = 0;
int SensorCounter = 0;
int SpeakerSwitch = 0;
int counter;
int counter2;
int counter3;
unsigned long previousTime;
unsigned long elapsedTime;
int count;
bool blink;
int elapsedTimecount;
void setup()
{
pinMode(vibSensor, INPUT);
pinMode(speakerSwitch, INPUT);
pinMode(greenLED, OUTPUT);
pinMode(redLED, OUTPUT);
pinMode(speaker, OUTPUT);
}
void loop()
{
unsigned long currentTime = millis();
VibSenseInput = digitalRead(2);
SpeakerSwitch = digitalRead(4);
if (VibSenseInput == LOW && SpeakerSwitch == LOW) {
digitalWrite(greenLED, HIGH);
digitalWrite(redLED, LOW);
digitalWrite(speaker, LOW);
SensorCounter = 0;
}
if (VibSenseInput == HIGH && SpeakerSwitch == LOW && elapsedTimecount < 4000) {
digitalWrite(greenLED, LOW);
digitalWrite(speaker, LOW);
if (currentTime - previousTime >= 100) {
previousTime = currentTime;
if (count < 10) {
blink = !blink;
digitalWrite(redLED, blink ? HIGH : LOW);
}
count++;
if (count > 11);
count = 0;
if (currentTime - elapsedTime <= 4000) {
elapsedTime = currentTime;
}
elapsedTimecount++;
if (elapsedTimecount > 4001);
elapsedTimecount = 0;
}
}
#define ENABLED true
#define DISABLED false
#define LEDon HIGH
#define LEDoff LOW
#define switchClosed LOW
#define switchOpen HIGH
const byte mySwitch = 2; // +5V---[50k]---PIN---[Switch]---GND
const byte LED = 3; // +5V---[A->|-K]---[220R]---[Pin3]
const byte heartbeatLED = 13; // +5V---[A->|-K]---[220R]---[Pin13]
byte switchState = switchOpen;
bool timerFlag = DISABLED;
//timing stuff
unsigned long switchTime;
unsigned long heartbeatTime;
unsigned long fiveSecondTime;
unsigned long toggleTime;
//*************************************************************************
void setup()
{
pinMode(mySwitch, INPUT_PULLUP);
pinMode(LED, OUTPUT);
pinMode(heartbeatLED, OUTPUT);
} //END of setup()
//*************************************************************************
void loop()
{
//************************************** heartbeat T I M E R
//to see if the sketch has blocking code,
//time to toggle the heartbeat LED ?
if (millis() - heartbeatTime >= 500ul)
{
//restart the TIMER
heartbeatTime = millis();
//toggle the LED
digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
}
//**************************************
//are these TIMERs enabled ?
if (timerFlag == ENABLED)
{
//******************** toggle T I M E R
//is it time to toggle the LED ?
if (millis() - toggleTime >= 100ul)
{
//restart the TIMER
toggleTime = millis();
//toggle the LED
digitalWrite(LED, !digitalRead(LED));
}
//******************** 5 second T I M E R
//has the 5 second TIMER expired ?
if (millis() - fiveSecondTime >= 5000ul)
{
//disable these TIMERs
timerFlag = DISABLED;
//make sure the LED is OFF
digitalWrite(LED, LEDoff);
}
}
//************************************** switches T I M E R
//is it time to check the switches ?
if (millis() - switchTime >= 50ul)
{
//restart the TIMER
switchTime = millis();
checkSwitches();
}
} //END of loop()
//*************************************************************************
void checkSwitches()
{
byte currentState;
//************************************** m y S w i t c h
//mySwitch
currentState = digitalRead(mySwitch);
//did the switch change state ?
if (switchState != currentState)
{
//update to the new state
switchState = currentState;
//if we are not timing, was the switch close ?
if (timerFlag == DISABLED && currentState == switchClosed)
{
timerFlag = ENABLED;
//restart the TIMERs
toggleTime = millis();
fiveSecondTime = millis();
}
} //END of this switch
//**************************************
} //END of checkSwitches()
//*************************************************************************