hello world, I'm new to this world. I want to make an LED light up when the number in the counter is even but with the condition that I can't use the delay() function.
I'm just lost and I can't see what's wrong with my code.
I would be so thankful to receive some help.
Thank you.
This is my code:
//Arduino code for lighting up an LED using millis() as timer and dealy.
int ledPin = 6;
int contador = 1;
int ledState = LOW;
unsigned long previousMillis = 0;
const long interval = 2000;
void setup()
{
Serial.begin(9600);
pinMode( ledPin, OUTPUT);
Serial.print("Arduino, READY");
}
void loop()
{
unsigned long currentMillis = millis();
for (contador=1; contador<=12; contador++)
{
if (currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
if (ledState == LOW)
{
ledState = HIGH;
}
else
{
ledState = LOW;
}
digitalWrite (ledPin, ledState);
}
Serial.print (contador);
switch(contador) {
case 1:
Serial.println(" - one");
break;
case 2:
Serial.println(" - two");
break;
case 3:
Serial.println(" - three");
break;
case 4:
Serial.println(" - four");
break;
case 5:
Serial.println(" - five");
break;
case 6:
Serial.println(" - six");
break;
case 7:
Serial.println(" - seven");
break;
case 8:
Serial.println(" - eight");
break;
case 9:
Serial.println(" - nine");
break;
case 10:
Serial.println(" - ten");
break;
case 11:
Serial.println(" - eleven");
break;
case 12:
Serial.println(" - twelve");
break;
}
if(contador%2==0)
{
digitalWrite(ledPin, HIGH);
}
else
{
digitalWrite(ledPin, LOW);
}
if (currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
if (ledState == LOW)
{
ledState = HIGH;
}
else
{
ledState = LOW;
}
digitalWrite (ledPin, ledState);
}
}
}