Millis issue

Hello, I am currently stuck on my program as what I am intending is basically what delay(3000) would do but instead I wanna use millis instead.

int switchState1 = 0;
int switchState2 = 0;
const unsigned long period = 3000;
unsigned long currentMillis;
unsigned long startMillis;

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);
currentMillis = millis();

if (switchState1 == LOW) {
  digitalWrite(3, HIGH);
  digitalWrite(4, LOW);
}
else { 
 if (currentMillis - startMillis >= period)
    {
  digitalWrite(3, LOW);
  digitalWrite(4, HIGH);
 startMillis = currentMillis;
  }
}
if (switchState2 == LOW) {
    digitalWrite(6, LOW);
  digitalWrite(7, HIGH);
}
else {
    if (currentMillis - startMillis >= period)
    {
  digitalWrite(6, HIGH);
  digitalWrite(7, LOW);
  startMillis = currentMillis;
    }
}
}

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.

I am unsure how to solve this

Please explain what you want to do

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 ?

How are the inputs wired ?

You have two different time tests that both use the same startMillis value. Perhaps they should each have their own value in separate variables.

If that does not help then a more extensive description of what you want to happen would be useful.

Separately your program would be much easier to understand if, instead of this style

digitalWrite(3, HIGH);

you used names for the pins - perhaps like this

digitalWrite(redLedPin, HIGH);

...R

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

Did you note the need to use different timing variables for each of the LEDs ?

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.

...R

@UKHelibob

How do I separate the two? And yes I do have resistors connected

@Robin2

I want two different buttons controlling two different red and green LEDs.

I push button 1 and Red LED 1 turns on for 3 seconds before turning off and Green LED 1 goes back on

I push button 2 and Red LED 2 turns on for 3 seconds before turning off and Green LED 2 goes back on

And yes I changed the names to meaningful ones, how do I use the AutoFormat Tool?

How do I separate the two?

Read the second paragraph of post #7

supmus:
, how do I use the AutoFormat Tool?

In the Arduino IDE select Tools/AutoFormat or CTRL-T

...R

Robin2:
In the Arduino IDE select Tools/AutoFormat or CTRL-T

For best results (imo), make sure the { and } are on their own lines. So instead of this:

if (switchState1 == LOW) {
  digitalWrite(3, HIGH); // Green LED ON
  digitalWrite(4, LOW); // Red LED OFF
}
else {

.... do this:

if (switchState1 == LOW) 
{
  digitalWrite(3, HIGH); // Green LED ON
  digitalWrite(4, LOW); // Red LED OFF
}
else 
{

For best results (imo), make sure the { and } are on their own lines.

I agree. You can even configure the Auto Format tool to do it for you if you want

UKHeliBob:
I agree. You can even configure the Auto Format tool to do it for you if you want

I didn't know that. Where? I don't see it in preferences (1.8.5)

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 :slight_smile:

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

Enjoy !

UKHeliBob:
Enjoy !

Thanks, will check later

@UKHeliBob, does such a change persist across IDE upgrades or does an upgrade bring in a new formatter.conf?

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

ah no sorry I didnt read it yet, had a hectic few days,but meantime I was wondering