Hi Dave,
welcome to the Arduino-forum.
putting the information about which LED to light up into the bits of a single byte is very efficient. Needs just one byte.
Well if you want to have the option to blink an LED this is additional information.
There are a lot of ways to transfer the added information.
Before mkaing a suggestion I would like to know what kind of device are you using to send the byte that is received by the code?
Depending on this different solutions are best fitted.
Blinking means adding new functionality. This requires additional lines of code.
The timing for switching on/off the LED = blink the LED needs some lines of code
There are thousands of ways how blinking can be realised.
Some users here invited you to learn it from scratch. Sure this is one way but in this short words it does not create hope to understand it in an acceptable time.
So here is a code that shows for LED Amber1 how the code can be modified to make the LED blink.
Though as long as only one byte is received the LED can blink or be off but not be switched on permanently. Anyway as a first step to get you going here is the modified code.
You can use this forum as a customised tutorial that will explain your questions.
If your questions are specific this will work. The more global your questions are the more global the answer are. (Just like the answers above)
The more specific your questions are the more specific the answers are
const byte GREEN = 13;
const byte RED1 = 12;
const byte RED2 = 11;
const byte RED3 = 10;
const byte RED4 = 9;
const byte RED5 = 8;
const byte AMBER1 = 7;
const byte AMBER2 = 6;
boolean Amber1Blink = false;
unsigned long Amber1BlinkTimer;
// easy to use helper-function for non-blocking timing
boolean TimePeriodIsOver (unsigned long &startOfPeriod, unsigned long TimePeriod) {
unsigned long currentMillis = millis();
if ( currentMillis - startOfPeriod >= TimePeriod ) {
// more time than TimePeriod has elapsed since last time if-condition was true
startOfPeriod = currentMillis; // a new period starts right here so set new starttime
return true;
}
else return false; // actual TimePeriod is NOT yet over
}
// setup the pins for output and open the serial port
void setup() {
pinMode(GREEN, OUTPUT);
pinMode(RED1, OUTPUT);
pinMode(RED2, OUTPUT);
pinMode(RED3, OUTPUT);
pinMode(RED4, OUTPUT);
pinMode(RED5, OUTPUT);
pinMode(AMBER1, OUTPUT);
pinMode(AMBER2, OUTPUT);
Serial.begin(115200);
}
// look for data on the serial port and turn on/off the lights
// data is a single byte with the bits set to 1=light on and 0=light off
// bits are 0=green, 1=red1, 2=red2, 3=red3, 4=red4, 5=red5, 6=amber1, 7=amber2
void loop() {
byte b;
if ( Serial.available() ) {
b = Serial.read();
if ( b & 0b00000001 ) {
digitalWrite(GREEN, HIGH);
} else {
digitalWrite(GREEN, LOW);
}
if ( b & 0b00000010 ) {
digitalWrite(RED1, HIGH);
} else {
digitalWrite(RED1, LOW);
}
if ( b & 0b00000100 ) {
digitalWrite(RED2, HIGH);
} else {
digitalWrite(RED2, LOW);
}
if ( b & 0b00001000 ) {
digitalWrite(RED3, HIGH);
} else {
digitalWrite(RED3, LOW);
}
if ( b & 0b00010000 ) {
digitalWrite(RED4, HIGH);
} else {
digitalWrite(RED4, LOW);
}
if ( b & 0b00100000 ) {
digitalWrite(RED5, HIGH);
} else {
digitalWrite(RED5, LOW);
}
// set a flag-variable to activate / DE-activate blinking
if ( b & 0b01000000 ) {
Amber1Blink = true; //digitalWrite(AMBER1, HIGH);
Amber1BlinkTimer = millis(); // store snapshot of actual time
}
else {
Amber1Blink = false; //digitalWrite(AMBER1, LOW);
}
if ( b & 0b10000000 ) {
digitalWrite(AMBER2, HIGH);
} else {
digitalWrite(AMBER2, LOW);
}
} // END-OF if ( Serial.available() ) {
// this part of the code is executed with each iteration of
// function loop()
if (Amber1Blink) { // if blinking is activated
if ( TimePeriodIsOver(Amber1BlinkTimer,500) ) { // check if 500 milliseconds have passed by
// if REALLY 500 milliseconds of time HAVE passed by
if (digitalRead(AMBER1) == HIGH) { // check if LED is ON
digitalWrite(AMBER1,LOW); // turn LED off
}
else {
digitalWrite(AMBER1,HIGH); // turn LED on
}
} // END-OF if ( TimePeriodIsOver(
} // END-OF if (Amber1Blink)
}
best regards Stefan
If you want to read more about how the blinking works in detail read this tutorial
P.S. @ the other users: If you are all too lazy to provide support adapted to the knowledge-level of the thread-opener the more my version of non-blocking timing will apear in the forum and will become - in the long run - the new standard