AwesomeApollo:
In the loop() I want to be able to check if a button has been pressed to "jump" to that piece of code but with the delay() function then it just stops arduino from reading the inputs, I know that millis allows the inputs to still be read but i don't know how to replace all of the delays with millis
I have wished to model your idea in terms of the following diagram.
1. Your current program codes are doing the following:
(a) LED11(G) remains ON for 3-sec.
(b) LED11(G) goes OFF; LED12 (O) remains ON for 1-sec.
(c) LED12 (O) goes OFF; LED13 (R) remains ON for 3-sec.
(d) Let LED13 (R) remain ON for more 1-sec; LED12 remains ON for 1-sec
2. Now, you want to add a button (K1) with your hardware setup; you want to modify your program so that the program can detect the closed condition of K1 and then execute a 'block of codes (say, ignite LED10); after that the normal program flow continues.
3. Yes! You are right in saying that the goal of Step-2 could not be achieved keeping the delay() function in the program as this function prevents the MCU (blocks the MCU) from doing any other side activities like checking the close condition of K1. On the other hand, the millis() function is not a blocking function, and it allows checking the close condition of K1 while 'time delay' process is still going on.
while(millis() - previousmillis != interval)
{
if(digitalRead(2) == LOW)
{
igniteED10();
}
}
4. The following codes (a modified version of the millis() based codes of Post#3) carries out all the events of step-1; it also checks if K1 has been closed. If the closed condition of K1 is detected, the program ignites LED10 as a side job; after that the normal program flow continues -- the program carries out the events of Step-1.
const int R = 13;
const int O = 12;
const int G = 11;
const int A = 10;
void setup()
{
pinMode(G, OUTPUT);
pinMode(O, OUTPUT);
pinMode(R, OUTPUT);
pinMode(A, OUTPUT);
pinMode(2, INPUT_PULLUP);
PORTB = 0x00; //all LEDs OFF
}
void loop()
{
digitalWrite(G, HIGH); //11
callMillis(3000);//delay(3000);
digitalWrite(G, LOW); //11
digitalWrite(O, HIGH); //12
callMillis(1000);//delay(1000);
digitalWrite(O, LOW); //12
digitalWrite(R, HIGH); //13
callMillis(3000);//delay(3000);
digitalWrite(O, HIGH); //12
callMillis(1000);//delay(1000);
digitalWrite(O, LOW); //12
digitalWrite(R, LOW); //13
}
void callMillis(unsigned long x)
{
unsigned long presentMillis = millis();
while(millis() - presentMillis != x)
{
if(digitalRead(2) == LOW)
{
igniteLED10();
}
}
}
void igniteLED10()
{
digitalWrite(A, HIGH); //10
}