Hi everyone,
Hope I have put this in the correct place.
I have a sketch that is used in slotcar racing and controls a number of led's. As the topic title says I would like to have the amber led not just illuminate but blink.
I have little idea of writing code I just know how to upload it to my arduino so I was hoping someone could help me with this if its not too much work.
Below is the sketch in its entirety... thanks in advance
// define which pins are to be used for the various light functions #define GREEN 13 #define RED1 12 #define RED2 11 #define RED3 10 #define RED4 9 #define RED5 8 #define AMBER1 7 #define AMBER2 6
// 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(9600);
}
// 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); }
if ( b & 0b01000000 ) { digitalWrite(AMBER1, HIGH); } else { digitalWrite(AMBER1, LOW); }
if ( b & 0b10000000 ) { digitalWrite(AMBER2, HIGH); } else { digitalWrite(AMBER2, LOW); }
}
}
Hello davelaty
Keep it simple and stupid.
Design your own time manager.
It is recommended to start with the mother of all Arduino timers, the IDE's BLINKWITHOUTDELAY example.
Using this example, you can see how millis() function works and extend this with some additional functions.
startTimer() --> starts the timer
stopTimer() --> stops the timer
and
actionTimer() --> performs an action when the timer is triggered.
Have a nice day and enjoy programming in C++ and learning.
thanks for the reply's.
I think I assumed it was just a case of adding a line of code or substituting a line of code but it looks like it's more complex than that.
Really appreciate the help but I'm obviously way out my depth. Probably best to park it and live with the amber not blinking
But your variable b will, below the if statement, have the information necessary to blink the amber LEDs.
You've been pointed at "blink without delay" above, take a look, use it as you find it and watch it blink away, come to some kind of grasp on what it is doing and how it works…
Then you might see how to test the relevant bits of b and decide to run the blink blink magic on the LEDs you want to blink when they are on instead of just being on.
Give it your best try, we wanna help you not settle for anything less than what you'd like to accomplish.
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
Sure again and I put one on top which most of you will think of: egomanic.
(Well it is a matter of a decade)
then, as user paulpaulson likes to say, "the mother of all timers" will retire and eke out a secluded existence in the retirement home of programming history
Here's the thing... what all do you want these leds to do?
Do you want to make drag race starting lights that start at the top and sequence to the bottom green? Will the led on the left ever differ from the one on the right?
Do you need any buttons?
All AVR IO pins are grouped in 8 bit Ports. You can read and write Port registers to work groups of pins together. The UNO has 3 Ports with 6 open IO pins (the other 2 are used unless you don't use Serial).
Hello JCA34F
Many many thanks for your "quickie blinker" sketch.
This is the best solution I have ever seen as a KISS principle solution.
Have a nice day and enjoy programming in C++ and learning.
I prefer your bitwise method, showing the "on" subject in a lineup of "off", being this a microcontroller forum, using bitwise makes "sensor." I have overheard, "Too much abstraction spoils the coder." Not to worry, while you are asleep, "we all" are posting.