First time poster. I have been messing with arduino only a short time. I have some code I am playing with trying to convert all my Delays to Millis. I have added a button and want to keep checking the button state, so obviously I have to change the delays. I have been able to get Millis to work the first instance I use it. The second instance is inside a "For" statement and that is where I can't seem to get it to work. It acts as if it's skipping the code altogether. No matter what I try it just doesn't delay, it moves on. The spot we are looking at in the code is in Case 1 after the first FOR, you can see I commented out the delay and just before it is my Millis code that I can't seem to get to work. I could sure use some help. Thanks.
// 3 Modes
// Mode 1 LED Sequencer
// Mode 2 ALL LEDs ON
// Mode 3 ALL LEDs OFF
// constants won't change. Used here to
// set pin numbers:
const int buttonPin = 2; // Button connected to digital pin 2
const int ledPin3 = 3; // LED connected to digital pin 3
const int ledPin4 = 4; // LED connected to digital pin 4
const int ledPin5 = 5; // LED connected to digital pin 5
const int ledPin6 = 6; // LED connected to digital pin 6
const int ledPin7 = 7; // LED connected to digital pin 7
const int ledPin8 = 8; // LED connected to digital pin 8
const int ledPin9 = 9; // LED connected to digital pin 9
const int ledPin10 = 10; // LED connected to digital pin 10
const int ledPin11 = 11; // LED connected to digital pin 11
const int ledPin12 = 12; // LED connected to digital pin 12
// Variables will change:
int counter = 1; // set counter at 0 to start
int previous = LOW;
unsigned long previousTime1 = 0; // set int to track event1
unsigned long previousTime2 = 0; // set int to track event2
void setup() {
Serial.begin(9600);
while (! Serial);
Serial.println("Serial Ready");
pinMode(buttonPin, INPUT); // sets the digital pin as input
pinMode(ledPin3, OUTPUT); // sets the digital pin as output
pinMode(ledPin4, OUTPUT); // sets the digital pin as output
pinMode(ledPin5, OUTPUT); // sets the digital pin as output
pinMode(ledPin6, OUTPUT); // sets the digital pin as output
pinMode(ledPin7, OUTPUT); // sets the digital pin as output
pinMode(ledPin8, OUTPUT); // sets the digital pin as output
pinMode(ledPin9, OUTPUT); // sets the digital pin as output
pinMode(ledPin10, OUTPUT); // sets the digital pin as output
pinMode(ledPin11, OUTPUT); // sets the digital pin as output
pinMode(ledPin12, OUTPUT); // sets the digital pin as output
}
void loop() {
unsigned long currentTime = millis(); // updates frequently
//Handle input
int buttonVal = digitalRead(buttonPin);
//Serial.print("buttonVal");
// Serial.println(buttonVal);
if(buttonVal == HIGH && previous == LOW)
{
delay(200);
counter ++;
//Reset count if over max mode number
if(counter == 4)
{
counter = 1;
}
}
else
//Change mode
switch (counter) {
case 1:
digitalWrite(ledPin11, HIGH); // sets the LED on
digitalWrite(ledPin12, HIGH); // sets the LED on
Serial.println(currentTime);
if ( currentTime - previousTime1 >= 5000) { // test whether the period has elapsed
previousTime1 = currentTime; // reset our clock
for ( int step1 = 1; step1 < 4; step1++ ) {
digitalWrite(ledPin3, HIGH); // sets the LED on
digitalWrite(ledPin4, HIGH); // sets the LED on
digitalWrite(ledPin5, HIGH); // sets the LED on
digitalWrite(ledPin6, HIGH); // sets the LED on
Serial.println(currentTime);
if ( currentTime - previousTime2 >= 5000) { // test whether the period has elapsed
previousTime2 = currentTime; // reset our clock
//delay(1000); // waits for one second
digitalWrite(ledPin3, LOW); // sets the LED off
digitalWrite(ledPin4, LOW); // sets the LED off
digitalWrite(ledPin5, LOW); // sets the LED off
digitalWrite(ledPin6, LOW); // sets the LED off
}
delay(100); // waits for a tenth second
digitalWrite(ledPin7, HIGH); // sets the LED on
digitalWrite(ledPin8, HIGH); // sets the LED on
digitalWrite(ledPin9, HIGH); // sets the LED on
digitalWrite(ledPin10, HIGH); // sets the LED on
delay(1000); // waits for one second
digitalWrite(ledPin7, LOW); // sets the LED off
digitalWrite(ledPin8, LOW); // sets the LED off
digitalWrite(ledPin9, LOW); // sets the LED off
digitalWrite(ledPin10, LOW); // sets the LED off
delay(100); // waits for half a second
}
for ( int step2 = 3; step2 < 11; step2++ ) {
digitalWrite(step2, HIGH); // sets the LED on
delay(300); // waits for half a second
digitalWrite(step2, LOW); // sets the LED off
}