-
Hi @micbay, "There are no pins left on the nano to add more LEDs, but you might be able to use an I2C compatible stop light and add it to the 7-segment display bar chain, then add a line of code to the pre-start countdown to operate it." i found this: and seems perfect for that, also seems really nice to add to the slotcar enviroment. so i think i need to modify this line to avoid using more than 2 digitbar: and after that i could chain to the second ledbar my bargrapgh (as third device chained) and add to the sketch the right library to control it and add the code in section "pre-start countdown", right? is that enought? here another question, the ledbar has 5 pins, the i2c adafruit bargraph that i found (as every i2c device) has only 4 pins (excluding VCC and GND), where i need to wire up the SCL and SDA of bargraph to the pins of the ledbar (LED CLK, LED CS and LED DIN, which of them)? thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 11 comments 9 replies
-
Hi sommo, Adding the new Adafruit GFX library will increase the program memory usage, hopefully it will not be too much, but is something to be aware of. You should be able to tell from the Arduino sketch upload how much that impact is. ALTERNATIVE USING LED BAR SERIAL BUS 2 LANES NOTE |
Beta Was this translation helpful? Give feedback.
-
Getting a chance to look at the link for the Adafruit bar you provided, I realize there are multiple libraries it requires, not just the GFX. Using the LCD example code for the controller and the code from the Adafruit example referenced in the LEDbar link this is my best guess as to the minimum code to support the LCD and Adafruit Bar at the same time. I can't verify anything other than this will compile as an .ino file since I don't have the Adafruit bar, but this might help give a better jumping off point. After installing the 'Adafruit GFX' and the 'Adafruit LED_Backpack' libraries, (using the Arduino IDE library manager) you will find new example sketches that can be pulled up from the Arduino IDE by going to 'File->examples->Adafruit Backpack Library->bargraph24' and this is where I pulled the code to operate the Adafruit LED bar and injected it in code example for the LCD display for the controller project.
|
Beta Was this translation helpful? Give feedback.
-
Stoplight Using LED Chain Serial BusThough it may be tricky to understand, this graphic shows how you could tap into an extra MAX7219 7-segment display bar to use it to power stop light LEDs. If an additional I2C device can't be run using the same library used to operate the LCD, and the additional libraries take up too much memory, then this approach would allow one to use the same library already installed for the LED bars. This however takes a bit more understanding to control and hardware wiring some Red, Yellow, and Green, LED components, likely requiring soldering and maybe some de-soldering. As discussed in the LED bar section of the readme, each digit is equivalent to 8 stand alone LED components. By hacking the wiring of a LED bar to remove the digit and wire your raw stoplight LEDs in their place, one can then turn them on and off by writing a number whose digit bars turn on those connections. Even better than hacking an assembled LED bar would be to just buy a raw MAX7219 chip and wire it up to your stoplight LEDs. This approach would require code updates to extend the LED_BAR_COUNT = LANE_COUNT + 1, and of course, code to command the stoplight LEDs appropriately. |
Beta Was this translation helpful? Give feedback.
-
Really Really Thank you. for (uint8_t b=0; b<24; b++) { into the |
Beta Was this translation helpful? Give feedback.
-
If you can describe the behavior you'd like to see from the 24 LED bargraph, I can help you with the code. From the Adafruit page for the Bi-color 24 LED bargraph, it looks like there is a red and a green LED for each of the 24 available bars. To display Red it turns on the red one, for green, it turns on the green one, and for yellow it turns on both the Red & Green. The library API appears to do that combo for you by instructing just 'LED_YELLOW'. The 'bargraph24' example code is not a good direct analog for what you want to do visually, it just illustrates how to turn every LED to one of its 3 main colors. Those 'for' loops, turn each individual LED on the bar, on then off, in turn, very rapidly. Then it goes to the next color and does that again. So what you'll see is a single Red LED appear to move from one end of the bar to the other, then a Green light rapidly move from one end to the other, and finally yellow. And it will just keep cycling through in this way. In your case you want to turn them on and off at different times in sync with the pre-start countdown in some manner. Depending on how you want to orient the graph and and whether you want to treat the whole bar as a single light or treat it as a start-light tree, as in drag racing, we would need to adjust the activation functions accordingly. One approach, I'll attempt to illustrate here would be to turn the whole bar red upon the start of the pre-start countdown. Then for the final 3 seconds turn 1/3 of the bar yellow, such that on 1 the whole bar is yellow, then on 0 (aka start), the whole bar turns green. Then 2sec into the race it will turn off. To implement this we'll start by setting up a function to call from our main loop(), that can be used to set the LEDs, a global boolean to track the bargraph's 'ON' state, as well as declare and initialize the software object representing the bargraph (ie
// Adafruit Bar LED libraries
// code taken from built in Adafruit LEDBackpack library 'bargraph24)
#include <Adafruit_GFX.h>
#include "Adafruit_LEDBackpack.h"
// Add global code, such as this, somewhere above the main setup() and loop()
// declaring object representing Adafruit LED bar, called 'bar'.
Adafruit_24bargraph bar = Adafruit_24bargraph();
// create a global variable to track the 'on' state of LED bar
// We'll use this flage to make if faster to check status during a race
bool bargraphOn = false;
// This function sets specified block of LEDs on bargraph to the input color.
// 'color' can be 'LED_RED', 'LED_YELLOW', 'LED_GREEN', or 'LED_OFF'
// 'start' is the index reference of 1st LED to change, index 0 is LED 1
// 'end' is index of last LED to change, index 23 is LED 24
// by default, leaving 'start' and 'end' out of function call, will set entire bar
void setBargraph(byte color, byte end = 23, byte start = 0) {
// cyle through each LED to change, set new color, and update display
for (uint8_t i=start; i<=end; i++) {
bar.setBar(i, color);
bar.writeDisplay();
}
}
// In setup() add line to initialize the bargraph object
void setup() {
// use actual address from documentation if not the same as '0x70'
bar.begin(0x70);
} In the main Controller project the So if we want the bargraph to turn Red upon initiation of the pre-start countdown, we would use our new if (entryFlag && preStart) {
// Set race time to preStartCountDown wich is in seconds, so convert to millis.
currentTime[0] = preStartCountDown * 1000;
// Record current loop's ms timestamp as start of display update tick.
lastTickMillis = curMillis;
// Draw pre-start text to LCD and LEDs
PreStartDisplaysUpdate();
// Reset any race variables to initial values.
ResetRaceVars();
//------------NEW CODE-------------
// set entire LED Bargraph to Red
setBargraph(LED_RED);
// set bargraph ON flag to true
bargraphOn = true;
//---------END ADDED CODE----------
entryFlag = false;
} Then when the pre-start countdown reaches the final 3 seconds, we can turn 1/3 of the bargraph, progressively yellow, until the whole bar is yellow on the countdown call of '1'. We do this by adding code to the block dealing with the final 3 seconds of the pre-start countdown, found near line #2280. Finally, upon completion of the pre-start countdown we turn the bar all green. if (preStart) {
// Check race time's, pre-start countdown.
if (currentTime[0] > 0){
// update LCD on each tick
if (curMillis - lastTickMillis > displayTick){
currentTime[0] = currentTime[0] - displayTick;
PrintClock(currentTime[0], PRESTART_CLK_POS, 4, 2, lcdDisp, 2);
lastTickMillis = curMillis;
}
// In last 3 seconds send countdown to LEDs
if (currentTime[0] < 2999 && (currentTime[0]/1000 + 1) != ledCountdownTemp) {
// Add 1 because it should change on start of the digit not end
ledCountdownTemp = currentTime[0]/1000 + 1;
// Write current seconds digit to all active LEDs
ledWriteDigits(ledCountdownTemp);
//------------NEW CODE-------------
// set next 3rd of LED bar yellow
setBargraph(LED_YELLOW, ((ledCountdownTemp*8)-1), (ledCountdownTemp-1)*8);
//---------END ADDED CODE----------
Beep();
}
} else {
// The prestart phase has finished
// Set Flags for RACE START and end the prestart state
preStart = false;
// reset ledCountdownTemp to default for next race
ledCountdownTemp = 0;
// reset entry flag to true so race timing variables can be initialized
entryFlag = true;
//------------NEW CODE-------------
// At completion of pre-start, set full LED bargraph to green
setBargraph(LED_GREEN);
//---------END ADDED CODE----------
}
} After the completion of the pre-start countdown, the bargraph should now be all green. It will stay this way until we tell it to turn off. Since the race is now active, we must do this from inside the active race conditional which starts around line #2306. Once the race starts, while the bargraph is 'ON' we check how far into the race we are, once we hit 2 seconds, we turn the bargraph 'OFF" and set flag to false. else {
// ********* LIVE RACE **********
// Regardless of race type, we do these things
//------------NEW CODE-------------
// After 2sec (ie 2000ms), turn bargraph off
if(bargraphOn){
if (currentTime[0] >= 2000) {
setBargraph(LED_OFF);
bargraphOn = false;
}
}
//---------END ADDED CODE----------
// Update current racetime.
if (countingDown) {
// When counting down we need to guard against negatives.
currentTime[0] = raceSetTimeMs < (curMillis - startMillis[0]) ? 0 : raceSetTimeMs - (curMillis - startMillis[0]);
} else {
// Lap based race time counts up until final lap finished by 1st racer.
currentTime[0] = curMillis - startMillis[0];
}
------------bunch of other code--------------
} |
Beta Was this translation helpful? Give feedback.
-
@micbay, You are awesome! and also faster than the shipment of the ledbar (i guess it will arrive in 1-2 weeks)! |
Beta Was this translation helpful? Give feedback.
-
Hi @micbay,
any ideas? |
Beta Was this translation helpful? Give feedback.
-
On a related side note, I am almost ready to post an upgrade that adds a Drag Racing race selection. To do that properly I needed to integrate a start light tree, so I bought one of those Adafruit bars and it has arrived. It came disassembled and with no instructions, but it turns out it's very important that the LED bars are assembled in the right orientation. |
Beta Was this translation helpful? Give feedback.
-
Hi @micbay, Arduino IDE 2.0. |
Beta Was this translation helpful? Give feedback.
-
With controller version 2.0+ support for start lights has been implemented. |
Beta Was this translation helpful? Give feedback.
-
Thank you! |
Beta Was this translation helpful? Give feedback.
With controller version 2.0+ support for start lights has been implemented.
Both methods discussed in this thread, using an Adafruit Bi-Color Bargraph-24, and using a custom MAX7219 based light tree are now supported. Instructions on how to integrate them are included in the project documentation.