Side note: 3 and 6 are Red LEDs, 4 and 7 are Green LEDs
The result is when I push the button, the red light will blink and instantly go back to Green and I am unable to change it back to Red for 3000 milliseconds.
Please help us by describing what the program is supposed to do. Giving the pins meaningful names such as greenLedPin1 would help to understand the program
I note that you have only one variable named startMillis and currentMillis. Is that deliberate ?
What I want is for me to push the button and the Red LED lights up and the green turns off for 3 seconds. After the 3 seconds I want it to go back to the Green LED.
I also want both programs to be able to run simultaneously without the problem that delay would pose
I just changed the timing variables and now they work independently
int switchState1 = 0;
int switchState2 = 0;
const unsigned long period = 1500;
unsigned long currentMillis1;
unsigned long currentMillis2;
unsigned long startMillis1;
unsigned long startMillis2;
void setup(){
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(2, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
}
void loop(){
switchState1 = digitalRead(2);
switchState2 = digitalRead(5);
currentMillis1 = millis();
currentMillis2 = millis();
if (switchState1 == LOW) {
digitalWrite(3, HIGH); // Green LED ON
digitalWrite(4, LOW); // Red LED OFF
}
else {
if (currentMillis1 - startMillis1 >= period)
{
digitalWrite(3, LOW); // Green LED OFF
digitalWrite(4, HIGH); // Red LED ON
startMillis1 = currentMillis1;
}
}
if (switchState2 == LOW) {
digitalWrite(6, LOW); // Green LED ON
digitalWrite(7, HIGH); // Red LED OFF
}
else {
if (currentMillis2 - startMillis2 >= period)
{
digitalWrite(6, HIGH); // Green LED OFF
digitalWrite(7, LOW); // Red LED ON
startMillis2 = currentMillis2;
}
}
}
But I am still unsure of how to get the Red LEDs to light up for 3 seconds
Separate the button press testing from the timing test.
One way would be to read the input and when it becomes LOW (I assume that you have pullup resistors in place) turn on/off the LEDs and save the start time. Then later in loop() check whether the timing period has elapsed and if so turn on/off the LED.
You could put the variables in arrays or an array of structs to reduce the code but that is probably better left for now.
Why are you changing the state of both LEDs in both sections of the program? Maybe you should have one section for the green LED and the other for the red LED.
Please use the AutoFormat tool to indent your code and make it easier to read.
If you use meaningful names for the pins (as I suggested in Reply #3) there would be no need for comments and no risk of the code and the comment getting out of step.
neiklot:
I didn't know that. Where? I don't see it in preferences (1.8.5)
I could tell, but I would have to kill you
Lookout for the hit squad if you do this
Find the file named formatter.conf on your system and open it in a text editor
At the top of the file you will see this
# This configuration file contains a selection of the available options provided by the formatting tool "Artistic Style"
# http://astyle.sourceforge.net/astyle.html
#
# If you wish to change them, don't edit this file.
# Instead, copy it in the same folder of file "preferences.txt" and modify the copy. This way, you won't lose your custom formatter settings when upgrading the IDE
# If you don't know where file preferences.txt is stored, open the IDE, File -> Preferences and you'll find a link
The website linked to has details of all of the formatting commands available
Here is how my formatter.conf is set up
This configuration file contains a selection of the available options provided by the formatting tool "Artistic Style"
# http://astyle.sourceforge.net/astyle.html
#
# If you wish to change them, don't edit this file.
# Instead, copy it in the same folder of file "preferences.txt" and modify the copy. This way, you won't lose your custom formatter settings when upgrading the IDE
# If you don't know where file preferences.txt is stored, open the IDE, File -> Preferences and you'll find a link
# 2 spaces indentation
indent=spaces=2
# also indent macros
indent-preprocessor
# indent classes, switches (and cases), comments starting at column 1
indent-classes
indent-switches
indent-cases
indent-col1-comments
# put a space around operators
pad-oper
# put a space after if/for/while
pad-header
# Move opening brackets onto new line
--style=allman --style=bsd --style=break -A1
# delete empty lines in functions
--delete-empty-lines
# Insert space padding around operators.
--pad-oper
neiklot: @UKHeliBob, does such a change persist across IDE upgrades or does an upgrade bring in a new formatter.conf?
Did you read formatter.conf ?
# This configuration file contains a selection of the available options provided by the formatting tool "Artistic Style"
# http://astyle.sourceforge.net/astyle.html
#
# If you wish to change them, don't edit this file.
# Instead, copy it in the same folder of file "preferences.txt" and modify the copy. This way, you won't lose your custom formatter settings when upgrading the IDE
# If you don't know where file preferences.txt is stored, open the IDE, File -> Preferences and you'll find a link
This text is in the copy of formatter.conf installed with each version of the IDE, but as it says, if you save a modified version to the same folder as preferences.txt then the IDE will use that version instead of the new one