The C++ source code to download to the Arduino is in available for download
as SignalDriverMax72xx.ino.
It uses the LedControl library, so the code starts by including the header file:
Then since it is using scanf() and various string function, it includes stdio.h and string.h:
#include <stdio.h>
#include <string.h>
Then it allocates a LedControl object:
* We use pins 12,11 and 10 for the SPI interface
* With our hardware we have connected pin 12 to the DATA IN-pin (1) of the first MAX7221
* pin 11 is connected to the CLK-pin(13) of the first MAX7221
* pin 10 is connected to the LOAD-pin(12) of the first MAX7221
* We will only have a single MAX7221 attached to the arduino
*/
LedControl
lc1=LedControl(12,11,10,1);
Next the setup function initializes the MAX72xx chip and sends an announcement to the host computer over the serial port:
* announcement to the host computer over the serial port.
*/
Serial.begin(115200);
Serial.println("Signal Driver Max72XX 0.1");
Serial.print("\n>>");
Serial.flush();
}
Next the signal aspects are defined. These values assuming that the signal heads are wired bottom to top, with the LEDs wired from bit 0 to 5 as: lower red, lower yellow, lower green, upper red, upper yellow, and upper green. (See Wiring the signals. Wiring the signals below.)
#define R_R B00001001
#define R_Y B00001010
#define R_G B00001100
#define Y_R B00010001
#define G_R B00100001
#define DARK B00000000
Next we have a helper function to convert from an aspect name sent from the host computer to the Arduino.
if (strcasecmp(
"R_R",aspectname) == 0)
return R_R;
else if (strcasecmp(
"R_Y",aspectname) == 0)
return R_Y;
else if (strcasecmp(
"R_G",aspectname) == 0)
return R_G;
else if (strcasecmp(
"Y_R",aspectname) == 0)
return Y_R;
else if (strcasecmp(
"G_R",aspectname) == 0)
return G_R;
else if (strcasecmp(
"DARK",aspectname) == 0)
return DARK;
else return -1;
}
Next comes the main loop function. Here we read a one line command from the host computer and decide what to do. There are only three commands defined:
- One to turn all of the LEDs off.
- One to set the aspect of one signal.
- And a final command to initiate a test sequence.
char buffer[256];
char p_buffer[32];
int len;
char unused;
int n;
Serial.print(p_buffer);
Serial.flush();
delay(1000);
case B00000000:
} else {
}
break;
case B11111111:
break;
case B10000000:
break;
default:
break;
}
}
if (Serial.available() > 0) {
}
len = Serial.readBytesUntil('\r',buffer,sizeof(buffer)-1);
if (len <= 1) {
Serial.print("\n>>");
Serial.flush();
return;
}
buffer[len] = '\0';
switch (toupper(buffer[0])) {
case 'D':
break;
case 'S':
{
char aspect[10];
int signalnum, aspectbits;
if (sscanf(buffer,"%c %d %9s",&unused,&signalnum,aspect) != 3) {
Serial.println("\nSyntax error (Set command)!");
} else {
if (aspectbits < 0) {
Serial.println("\nSyntax error (Bad aspect)!");
} else if (signalnum >= 0 && signalnum < 8) {
lc1.setRow(0, signalnum, (
byte) aspectbits);
} else {
Serial.println("\nSyntax error (Bad signal number)!");
}
}
break;
}
case 'T':
switch (n) {
case 1:
break;
case 2:
if (s_digit < 0 || s_digit > 7) {
Serial.println("\nSyntax error (Bad signal number)!");
break;
}
break;
case 3:
if (s_digit < 0 || s_digit > 7) {
Serial.println("\nSyntax error (Bad signal number)!");
break;
}
if (e_digit < 0 || e_digit > 7) {
Serial.println("\nSyntax error (Bad signal number)!");
break;
}
break;
default:
Serial.println("\nUnknown command!");
break;
}
break;
default:
Serial.println("\nUnknown command!");
break;
}
Serial.print("\n>>");
Serial.flush();
}
}
Wiring the signals.
I used this color coding for the signal LEDs when I wired them:
- Green
- The upper target head's green LED (uppermost LED of the upper target).
- Yellow
- The upper target head's yellow LED (middle LED of the upper target).
- Red
- The upper target head's red LED (bottom LED of the upper target).
- Blue
- The lower target head's green LED (uppermost LED of the lower target).
- White
- The lower target head's yellow LED (middle LED of the lower target).
- Black
- The lower target head's red LED (bottom LED of the lower target).
Thus the connections to the terminal blocks at the ends of the signal cables are made as shown here. If a target has fewer than three LEDs, then the wires for the missing LEDs are also missing.
Signal Connector Board, Wiring Color Codes
Once you have entered the code and verified that it compiles and uploaded it to the Arduino, you can test the code with the Serial Monitor tool on the Arduino IDE. Be sure to set the baud rate to 115200. You can then type commands into the Serial Monitor tool's send bar, as shown here.
Serial Monitor, Test Sketch