TomGeorge:
Hi,
I think this will work, without all the nesting in the if statements.const int GreenLEDPin = 2;
const int RedLEDPin = 4;
const int KNOPPin = 7;
unsigned long currentMillis;
unsigned long previousMillis ;
long interval;
long OnTime = 500;
long OffTime = 750;
bool GreenledState = LOW;
bool RedledState = LOW;
bool Status ;
void setup ()
{
pinMode (GreenLEDPin, OUTPUT);
pinMode (RedLEDPin, OUTPUT);
pinMode (KNOPPin, INPUT);
}
void loop()
{
currentMillis = millis();
Status = digitalRead(KNOPPin);
if (Status == HIGH)
{
digitalWrite(RedLEDPin, LOW);
BlinkGreen();
}
else
{
digitalWrite(GreenLEDPin, LOW);
BlinkRed();
}
}
void BlinkGreen()
{
if (GreenledState == HIGH) // Check LED state to load relevant delay time
{
interval = OnTime;
}
else
{
interval = OffTime;
}
if (currentMillis - previousMillis >= interval)
{
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (GreenledState == LOW)
{
GreenledState = HIGH;
}
else
{
GreenledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(GreenLEDPin, GreenledState);
}
}
void BlinkRed()
{
if (RedledState == HIGH) // Check LED state to load relevant delay time
{
interval = OnTime;
}
else
{
interval = OffTime;
}
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (RedledState == LOW)
{
RedledState = HIGH;
}
else
{
RedledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(RedLEDPin, RedledState);
}
}
I have like you, used blink without delay, but I used the ledstate value to determine the delay interval. I have also renamed your pin and variables to describe what they represent. Tom... :)
Oh i understand now. Thanks to everyone for helping me!!!