My set up is just the way I have my uno and bread board set up and only blinks an led 5 times. It's a timed sequence of events using millis for timing although I'm only using one led pin and the led could just be a substitute, could be trigging different relays or air solenoids. I was just trying to get it to blink when i wanted. Thanks for all your advice.
If you're a beginner, that project is a good exercise to start with!
Just some advice to help you improve your programming skills.
constants should always be defined with full upper case names, it makes easier to distinghuish them from "variables"
pin numbers have a small range, better use "const byte" type instead of "const int"
Even if it's almost correct, keep an eye to indentations, use Ctrl-T from the IDe to automatically format the code and see the difference
when dealing with buttons, you used INPUT_PULLUP and it's good, but you must take "bouncing" into considerations, either adding a delay (a "delay(100);" is enough, if you need quicker responses, you could use lower values) or using a "debounce" external circuit (Google "arduino debounce" and you'll get tons of instructions)
if you need to define a number of identical variables, you better start using arrays, instead of a long list of "time1", "time2" and so on.
when chiecking a timeout with millis() always compare millis()-starttime with the desired interval, to make sure the millis() overlap won't give problems; I mean instead of: (currentMillis >= time1 + 500
you should use: currentMillis - time1 >= 500
Said that, I could post here later a slightly modified version of your code but to start, I now have not made any considerations relating to the code behaviour and/or something not clear to me, but only to the programming style.
Just to start with the code logic/implementation, I see your code turns the LED on when you press the button, then using "time1" after 500 ms you turn it off then keeps cycling there until 1 second is elapsed, then after another 500 ms (aka 1500 ms after button has been pressed) you turn the LED back ON for another similar cycle using "time2" and so on, up to "time9" when you turn the LED off and nothing more happens.
So, I don't know if I really got what your goal is, but it looks to me like it's just a way to blink a LED a certain number of times, with a specific "on-off" timings.
Why don't you try to better describe in details what it should do, instead of telling us what that code does?
So when you press the button it blinks the LED five times. If you press it once again after it has started its 5 blinks, then you get 6 blinks. Was that your intention?
Some searching will find several simpler (less code) methods, based on incrementing a variable (say 'numberOfBlinks') and testing when that reaches 5.
Im planning to change the millis timing like you suggested, i knew that is the suggested way of doing it, just got the code working that way and went with it. Ive looked into arrays only way ive found is a for loop. How do i change the array to the next number without a for loop? ...or should i use the for loop. Idk thanks for help ive been working overtime and haven't had time to do much code learning.
Ive got an older post "to make a led blink five times after button press using millis" that code uses a counter for the blinks but that code has a bug i cant seem to fix. A quick button press usually results wth five blink a slow button press usually results with one long blink and three short.
This is caused because you only check for button == LOW without any precautions against "double"-initiating the blinking
Here is a demo-code in WOKWI that demonstrates how to to do it
code
const int ledPin = 8;
const int button1 = 5;
int ledState = LOW;
const unsigned long interval = 1000;
unsigned long myBlinkTimer;
int NoOfBlinks = 0;
void setup() {
Serial.begin(115200);
Serial.println("Setup-Start");
Serial.println("press button to start blinking");
pinMode(ledPin, OUTPUT);
pinMode(button1, INPUT_PULLUP);
}
void loop() {
if (NoOfBlinks == 0) { // if no blinking is active variable "NoOfBlinks" contains value 0
if (digitalRead(button1) == LOW) {
Serial.println("Button pressed start blinking");
myBlinkTimer = millis();
NoOfBlinks = 1; // assign value 1 to indicate blinking shall start
}
}
// check if blinking shall be active
if (NoOfBlinks > 0 ) {
// check if it is time to change LED-state
if ( TimePeriodIsOver(myBlinkTimer, interval) ) {
Serial.print("NoOfBlinks:");
Serial.print(NoOfBlinks);
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
Serial.println(" LED off switching on");
ledState = HIGH;
}
else {
ledState = LOW;
Serial.println(" LED on switching off");
NoOfBlinks++; // if blink has finished by switching off increment by 1
}
digitalWrite(ledPin, ledState);
}
if (NoOfBlinks > 5) {
Serial.print("NoOfBlinks:");
Serial.print(NoOfBlinks);
Serial.println(" => stop blinking");
Serial.println("press button to start blinking new");
digitalWrite(ledPin, LOW);
NoOfBlinks = 0; // assign value -1 to stop the blinking
}
}
}
// easy to use helper-function for non-blocking timing
boolean TimePeriodIsOver (unsigned long &startOfPeriod, unsigned long TimePeriod) {
unsigned long currentMillis = millis();
if ( currentMillis - startOfPeriod >= TimePeriod ) {
// more time than TimePeriod has elapsed since last time if-condition was true
startOfPeriod = currentMillis; // a new period starts right here so set new starttime
return true;
}
else return false; // actual TimePeriod is NOT yet over
}
not sure what your goal is here. is it to simply blink an LED some # of times, trying to better understand how to use millis(), trying to better understand conditional statements, ???
looks like the base piece of code is just copy & pasted, but even that might have simpler.
something to consider is that directly comparing any of your "time" variables to the value from millis() will have issues when millis() wraps around and goes from a value close to 2^32, 4,294,967,296 back to something close to zero, the values for all the time variables will be greater than the value near zero
this is why the difference between a time value and the value from millis() is compared to some period
if (currentMillis - time1 >= 500)
another thing to consider is that if you wanted only one of the conditional if statements to be valid, "else if" statements could be used
hopefully you realize there's a benefit to having as little code as necessary, not to save memory, but to minimize the amount of code to read and debug.
look this over
const int ledPin = 8;
const int button1 = 5;
const unsigned long Period = 500;
unsigned long time;
boolean run;
int ledState = LOW;
int cnt;
void loop() {
unsigned long currentMillis = millis();
if (digitalRead (button1) == LOW) {
time = currentMillis;
run = true;
cnt = 5;
}
if (run && currentMillis - time >= Period) {
time = currentMillis;
if (LOW == ledState)
ledState = HIGH;
else {
ledState = LOW;
if (0 >= --cnt)
run = false;
}
digitalWrite (ledPin, ledState);
}
}
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(button1, INPUT_PULLUP);
}