Hi, this is my first post on the Arduino forum, I hope I get all the formatting correct.
I have a very strange problem. My code is based on the 'BlinkWithoutDelay' example. If I write ledState to the red LED, the blue LED will flash, but if I check the state of ledState in an 'if' statement, and write HIGH or LOW to the red LED accordingly... it works! Code snippets below.
loop() has exactly the same timing code and setting of ledState as BlinkWithoutDelay, just with added code to drive the LEDs:
void loop() {
// here is where you'd put code that needs to be running all the time.
// check to see if it's time to blink the LED; that is, if the difference
// between the current time and last time you blinked the LED is bigger than
// the interval at which you want to blink the LED.
unsigned long currentMillis = millis();
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 (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(LED_YELLOW, ledState);
//digitalWrite(LED_RED, ledState); // this line makes BLUE LED flash, not RED...!
if (ledState == LOW) {
digitalWrite(LED_RED, LOW); // This code block works as expected...!
} else {
digitalWrite(LED_RED, HIGH);
}
}
}
If I uncomment the commented line (and comment out the 'if' block) then the blue LED flashes. Code as posted makes the red LED flash. Yellow LED is behaving as expected. If I read this on a forum I'd be inclined to not believe it so if you want I'll post a video
I have included PinNames.h and defined names for the three coloured LEDs as well as the 'standard' yellow LED
#include "PinNames.h"
const PinName LED_GREEN = PinName::p16;
const PinName LED_RED = PinName::p24;
const PinName LED_BLUE = PinName::p6;
const PinName LED_YELLOW = PinName::p13;
I have set the pin modes and initial states in setup()
void setup() {
// set the digital pin as output:
pinMode(LED_YELLOW, OUTPUT);
pinMode(LED_RED, OUTPUT);
pinMode(LED_GREEN, OUTPUT);
pinMode(LED_BLUE, OUTPUT);
digitalWrite(LED_YELLOW, HIGH);
digitalWrite(LED_RED, HIGH);
digitalWrite(LED_GREEN, HIGH);
digitalWrite(LED_BLUE, HIGH);
}
Any ideas what is going on...?