Forum › Coding Corner › 4 boards together
- This topic has 2 replies, 3 voices, and was last updated 2 years, 3 months ago by
kkttbogart.
-
AuthorPosts
-
-
March 27, 2022 at 7:57 pm #6885
PBH
This sketch with remarks should be self explaining. I am available for questions, but most of all I wonder if anyone would have a more efficient coding as I was particularly struggling with the management of the shift registers in the RGB matrix. Every time writing the bytes ‘B00000000’ times 4 works, but just seems not very efficient. Happy with any remarks.
Kind regards,
Peter/* This sketch is based on shortcircuits.cc basic sketches and uses the Short Circuits
Motherboard (MB), Digitiser (DI), Sensor array (SE) and RGB matrix (MA) boards.
It shows time and temperature alternatively every minute (mode1/2 by sw1) and runs through
time/temp/humidity/lightlevel/noiselevel (modes 1-5) with sw2 and outputs to DI and MA.
The LEDs show which mode (1-4) is current. In mode 5 (noiselevel) the LEDS display the level.
The brightness of DI and MA is controlled by the LDR
Not used: DI Pot and SE card reader
Attention to the comments in setup() to initialise the RTC
Most of the sketch is explained by comments. It might not be the most efficient programming, but it works 😉Motherboard connections:
D2 SE DHT
D3 (PWM) DI SER [SERDI]
D4 DI CLK [CLKDI]
D5 (PWM) DI LAT [LATDI]
D6 (PWM) DI OE and MA OE [OE]
D7 MA LAT [LATMA]
D8 MA CLK [CLKMA]
D9 (PWM) MA SER [SERMA]
D10 (PWM) SE CS
D11 (PWM) SE Din
D12 SE Dout
D13 SE CLK
A0 DI Sw1 (can use any pin)
A1 DI Sw2 (can use any pin)
A2 SE MIC (needs analog pin)
A3 SE LDR (needs analog pin)
A4-5 MB RTC (internally connected)
*/// INITIALISATION FOR THE DHT
#include <DHT_U.h> // includes DHT_U library
#define DHTPIN 2 // digital pin connected to the DHT sensor
#define DHTTYPE DHT11 // indicate the sensor type: DHT11 or DHT22
DHT_Unified dht(DHTPIN, DHTTYPE); // sets the pin and type// INITIALISATION FOR THE MOTHERBOARD RTC #1/2
#include <RTClib.h> // include RTClib library
RTC_DS1307 rtc; // create an instance of the DS1307// INPUT PIN VARIABLES AND INITIALISATION FOR THE DIGITISER
const int SERDI = 3;
const int CLKDI = 4;
const int LATDI = 5;
const int SERMA = 9;
const int CLKMA = 8;
const int LATMA = 7;
const int OE = 6;
const int sw1 = A0;
const int sw2 = A1;/* bits in sequence: LED 1,2,3,4, Digit 4,3,2,1 (U5). LED on = 1, Digit on = 0 */
const int digit[4] = { B00001110,B00001101,B00001011,B00000111 };/* digit sequence: G, F, A, B, DP, C, D, E. Check: Digit on = 0 */
const int number[10] = { B10001000,B11101011,B01001100,B01001001,B00101011,
B00011001,B00111000,B11001011,B00001000,B00001011};
int digit0 = digit[0]; // to switch digit0 on
int digit1 = digit[1]; // to switch digit1 on
int digit2 = digit[2]; // to switch digit2 on
int digit3 = digit[3]; // to switch digit3 on// INPUT PIN VARIABLES AND INITIALISATION FOR THE SENSOR ARRAY
const int MIC = A2;
const int LDR = A3;int micMin = 1024; // Sets micMin to 1024 before the readings decrease the number
int micMax = 0; // Sets micMax to 0 before the readings increase the number
int micReading = 0; // variable to hold mics raw input value
int light = 0; // variable to hold light level reading// INITIALISATION FOR RGB MATRIX
//MA MSBFIRST LED Order:
//B Green1, Green2, Green3, Green4, Blue1, Blue2, Blue3, Blue4
//B Row1, Row2, Row3, Row4, Red1, Red2, Red3, Red4
int row11 = B00000000;
int row12 = B11110000;
int row21 = B00000000;
int row22 = B11110000;
int row31 = B00000000;
int row32 = B11110000;
int row41 = B00000000;
int row42 = B11110000;// INITIALISATION FOR MAIN LOOP
int loopSizeNoise = 50; // to calculate average noiselevel
int dimVal = 0; // to dim LEDs and digits
int thousands = 0; // value for digit0
int hundreds = 0; // value for digit1
int tens = 0; // value for digit2
int ones = 0; // value for digit3
int noiseCounter = 0; // to determine average noiselevel
int micVal = 0; // to determine average noiselevel
int mode = 1; // 1:time, 2:temp, 3:humid, 4:light, 5:noiselevel
int sw1State = 0; // state of button1
int sw1StateLast = 0; // last state of button 1
int sw2State = 0; // state of button2
int sw2StateLast = 0; // last state of button 2
int digitValue = 0; // value to be displayed
int secondPrevious = 0; // if value of second (time) changes > …
int modeSwitchCounter = 0; // to delay automatical mode switches
int loopWait = 10; // x seconds wait between automatic switch mode 1<->2
int decimalPoint = 0; // decimal point for digit1
int letterC = B10011100; // prepares for displaying ‘C’ on digit3 in mode4
int letterH = B00101010; // prepares for displaying ‘H’ on digit3 in mode5
int letterL = B10111100; // prepares for displaying ‘L’ on digit0 in mode7
int takeTemp = 2; // measure temperature every x minutes
int takeTempPrevious = 0; // to measure temperature just once every takeTemp minutes
int tempMeasured = 0; // value for MA display
int tempMeasuredPrevious = 0; // value for MA display
int counterMA = 0; // for MA sequences
int counterMA2 = 0; // for MA sequences
int counterMA3 = 1; // for MA sequences
int steps = 1; // for MA sequences
bool interval = true; // to change modes automatically//Void Setup – runs once at start
void setup() {
// Serial.begin(9600); // Initialise Serial Monitor only for checks
rtc.begin(); // initialise RTC
dht.begin(); // initialise DHT sensorpinMode(SERDI, OUTPUT); // SERDI (data) pin as output
pinMode(CLKDI, OUTPUT); // CLK (clock) pin as output
pinMode(LATDI, OUTPUT); // LATCH pin as output
pinMode(SERMA, OUTPUT); // SER (data) pin as output
pinMode(CLKMA, OUTPUT); // CLK (clock) pin as output
pinMode(LATMA, OUTPUT); // LATCH pin MA as output
pinMode(OE, OUTPUT); // Output EnableanalogWrite(OE, 255); // Set brightness minimal (0 = full brightness). Register outputs enabled.
pinMode(sw1, INPUT); // Switch 1
pinMode(sw2, INPUT); // Switch 2
pinMode(LDR, INPUT); // Declaring the LDR pin as an inputpinMode(13, OUTPUT); // LED on motherboard
digitalWrite(13,LOW); // set pin 13 (LED) off// INITIALISATION FOR THE RTC #2/2
/*
To set the time on the RTC to match your computer’s clock, uncomment the next line and upload the code.
To prevent it resetting to the same time every time you reset the device, add the “//” to the beginning of the line then uplaod the code again.
The time will then be set and saved even after a reset (with a coin cell present).
*/
//rtc.adjust(DateTime(__DATE__,__TIME__)); // Set the RTC to Compile Date and Time
}void resetMA() { // Turns all LEDS of RGB matrix off
row11 = row21 = row31 = row41 = B00000000;
row12 = row22 = row32 = row42 = B11110000;
}void setMA() {
digitalWrite(LATMA,LOW); // row1
shiftOut(SERMA,CLKMA,MSBFIRST,row11);
shiftOut(SERMA,CLKMA,MSBFIRST,row12);
digitalWrite(LATMA,HIGH);
delay(1);
digitalWrite(LATMA,LOW); // row2
shiftOut(SERMA,CLKMA,MSBFIRST,row21);
shiftOut(SERMA,CLKMA,MSBFIRST,row22);
digitalWrite(LATMA,HIGH);
delay(1);
digitalWrite(LATMA,LOW); // row3
shiftOut(SERMA,CLKMA,MSBFIRST,row31);
shiftOut(SERMA,CLKMA,MSBFIRST,row32);
digitalWrite(LATMA,HIGH);
delay(1);
digitalWrite(LATMA,LOW); // row4
shiftOut(SERMA,CLKMA,MSBFIRST,row41);
shiftOut(SERMA,CLKMA,MSBFIRST,row42);
digitalWrite(LATMA,HIGH);
}void sequence1(int symbol){ // MA sequence 1
if(symbol == 1) {
row11 = B11001100; row12 = B01111100;
row21 = B11001100; row22 = B10111100;
row31 = B00110011; row32 = B11010011;
row41 = B00110011; row42 = B11100011;
}
if(symbol == 2) {
row11 = B01100110; row12 = B01110110;
row21 = B01100110; row22 = B10110110;
row31 = B01100110; row32 = B11010110;
row41 = B01100110; row42 = B11100110;
}
if(symbol == 3) {
row11 = B00110011; row12 = B01110011;
row21 = B00110011; row22 = B10110011;
row31 = B11001100; row32 = B11011100;
row41 = B11001100; row42 = B11101100;
}
if(symbol == 4) {
row11 = B00000000; row12 = B11110000;
row21 = B11111111; row22 = B10111111;
row31 = B11111111; row32 = B11011111;
row41 = B00000000; row42 = B11110000;
}
setMA();
}void sequence2(int symbol){ // MA sequence 2
if(symbol == 1) {
row11 = B00000000; row12 = B01111111; // blauw binnen, zwart buiten
row21 = B00000110; row22 = B10111001;
row31 = B00000110; row32 = B11011001;
row41 = B00000000; row42 = B11101111;
}
if(symbol == 2) {
row11 = B00001111; row12 = B01110000; // zwart binnen, blauw buiten
row21 = B00001111; row22 = B10110000;
row31 = B00001111; row32 = B11010000;
row41 = B00001111; row42 = B11100000;
}
if(symbol == 3) {
row11 = B00001111; row12 = B01110000; // rood binnen, zwart buiten
row21 = B00001001; row22 = B10110110;
row31 = B00001001; row32 = B11010110;
row41 = B00001111; row42 = B11100000;
}
if(symbol == 4) {
row11 = B00000000; row12 = B01111111; // zwart binnen, rood buiten
row21 = B00000000; row22 = B10111111;
row31 = B00000000; row32 = B11011111;
row41 = B00000000; row42 = B11101111;
}
setMA();
}void sequence3(int symbol){ // MA sequence 3
if(symbol == 1) {
row11 = B00000001; row12 = B01111000;
row21 = B00000000; row22 = B10110000;
row31 = B00000000; row32 = B11010000;
row41 = B00001000; row42 = B11100001;
}
if(symbol == 2) {
row11 = B00000000; row12 = B01110100;
row21 = B00000001; row22 = B10110000;
row31 = B00001000; row32 = B11010000;
row41 = B00000000; row42 = B11100010;
}
if(symbol == 3) {
row11 = B00000000; row12 = B01110010;
row21 = B00001000; row22 = B10110000;
row31 = B00000001; row32 = B11010000;
row41 = B00000000; row42 = B11100100;
}
if(symbol == 4) {
row11 = B00001000; row12 = B01110001;
row21 = B00000000; row22 = B10110000;
row31 = B00000000; row32 = B11010000;
row41 = B00000001; row42 = B11101000;
}
if(symbol == 5) {
row11 = B00000100; row12 = B01110000;
row21 = B00000000; row22 = B10110001;
row31 = B00000000; row32 = B11011000;
row41 = B00000010; row42 = B11100000;
}
if(symbol == 6) {
row11 = B00000010; row12 = B01110000;
row21 = B00000000; row22 = B10111000;
row31 = B00000000; row32 = B11010001;
row41 = B00000100; row42 = B11100000;
}
setMA();
}void sequence4(int symbol){ // MA sequence 4
if(symbol == 1) {
row11 = B10001000; row12 = B01111000;
row21 = B10001000; row22 = B10111000;
row31 = B10001000; row32 = B11011000;
row41 = B10001000; row42 = B11101000;
}
if(symbol == 2) {
row11 = B01000100; row12 = B01110100;
row21 = B01000100; row22 = B10110100;
row31 = B01000100; row32 = B11010100;
row41 = B01000100; row42 = B11100100;
}
if(symbol == 3) {
row11 = B00100010; row12 = B01110010;
row21 = B00100010; row22 = B10110010;
row31 = B00100010; row32 = B11010010;
row41 = B00100010; row42 = B11100010;
}
if(symbol == 4) {
row11 = B00010001; row12 = B01110001;
row21 = B00010001; row22 = B10110001;
row31 = B00010001; row32 = B11010001;
row41 = B00010001; row42 = B11100001;
}
if(symbol == 5) {
row11 = B00000000; row12 = B01111111;
row21 = B00000000; row22 = B10110000;
row31 = B00000000; row32 = B11010000;
row41 = B00000000; row42 = B11100000;
}
if(symbol == 6) {
row11 = B00000000; row12 = B01110000;
row21 = B11110000; row22 = B10111111;
row31 = B00000000; row32 = B11010000;
row41 = B00000000; row42 = B11100000;
}
if(symbol == 7) {
row11 = B00000000; row12 = B01110000;
row21 = B00000000; row22 = B10110000;
row31 = B11110000; row32 = B11010000;
row41 = B00000000; row42 = B11100000;
}
if(symbol == 8) {
row11 = B00000000; row12 = B01110000;
row21 = B00000000; row22 = B10110000;
row31 = B00000000; row32 = B11010000;
row41 = B00001111; row42 = B11100000;
}
setMA();
}void tempInMatrix() { // mode 4 values for MA
if(tempMeasured == tempMeasuredPrevious || tempMeasuredPrevious == 0) { // steady temperature or at start of program
if(tempMeasured < 170) { // blue
row11 = B00000000; row12 = B01110000;
row21 = B00000000; row22 = B10110000;
row31 = B00000000; row32 = B11010000;
row41 = B00001111; row42 = B11100000;
}
else if(tempMeasured < 230) { // green
row11 = B00000000; row12 = B01110000;
row21 = B00000000; row22 = B10110000;
row31 = B11110000; row32 = B11010000;
row41 = B00000000; row42 = B11100000;
}
else if(tempMeasured < 280) { // yellow
row11 = B00000000; row12 = B01110000;
row21 = B11110000; row22 = B10111111;
row31 = B00000000; row32 = B11010000;
row41 = B00000000; row42 = B11100000;
}
else { // red
row11 = B00000000; row12 = B01111111;
row21 = B00000000; row22 = B10110000;
row31 = B00000000; row32 = B11010000;
row41 = B00000000; row42 = B11100000;
}
}
else if(tempMeasured > tempMeasuredPrevious) { // increasing temperature
if(tempMeasured < 170) { // blue
row11 = B00000001; row12 = B01110000;
row21 = B00000011; row22 = B10110000;
row31 = B00000111; row32 = B11010000;
row41 = B00000000; row42 = B11100000;
}
else if(tempMeasured < 230) { // green
row11 = B00010000; row12 = B01110000;
row21 = B00110000; row22 = B10110000;
row31 = B01110000; row32 = B11010000;
row41 = B00000000; row42 = B11100000;
}
else if(tempMeasured < 280) { // yellow
row11 = B00010000; row12 = B01110001;
row21 = B00110000; row22 = B10110011;
row31 = B01110000; row32 = B11010111;
row41 = B00000000; row42 = B11100000;
}
else { // red
row11 = B00000000; row12 = B01110001;
row21 = B00000000; row22 = B10110011;
row31 = B00000000; row32 = B11010111;
row41 = B00000000; row42 = B11100000;
}
}
else if(tempMeasured < tempMeasuredPrevious) { // decreasing temperature
if(tempMeasured < 170) {
row11 = B00001000; row12 = B01110000;
row21 = B00001100; row22 = B10110000;
row31 = B00001110; row32 = B11010000;
row41 = B00000000; row42 = B11100000;
}
else if(tempMeasured < 230) {
row11 = B10000000; row12 = B01110000;
row21 = B11000000; row22 = B10110000;
row31 = B11100000; row32 = B11010000;
row41 = B00000000; row42 = B11100000;
}
else if(tempMeasured < 280) {
row11 = B10000000; row12 = B01111000;
row21 = B11000000; row22 = B10111100;
row31 = B11100000; row32 = B11011110;
row41 = B00000000; row42 = B11100000;
}
else {
row11 = B00000000; row12 = B01111000;
row21 = B00000000; row22 = B10111100;
row31 = B00000000; row32 = B11011110;
row41 = B00000000; row42 = B11100000;
}
}
}//Void Loop – repeats forever
void loop() {
// DIM LIGHTS
light = analogRead(LDR); // save current LDR value to variable
dimVal = map(light,0,1024,254,0); // dimming value max 254 means not completely off
analogWrite(OE,dimVal); // set brightness of digits and LEDs. less light > less brightness// STATE OF SWITCHES
sw1State = digitalRead(sw1);
sw2State = digitalRead(sw2);
if(sw1State != sw1StateLast){
if(sw1State == HIGH){
mode = 1;
interval = true;
}
resetMA(); // Turns all LEDS of RGB matrix off
delay(10); // delay to avoid bouncing
sw1StateLast = sw1State;
}
if(sw2State != sw2StateLast){
if(sw2State == HIGH){
mode++;
interval = false;
if(mode > 5){ // only 5 modes (time/temp/humid/light/noise)
mode = 1;
}
}
resetMA(); // Turns all LEDS of RGB matrix off
sw2StateLast = sw2State;
}// EXECUTE MODES
digit0 = digit[0]; // switching all digits on (again)
digit1 = digit[1];
digit2 = digit[2];
digit3 = digit[3];DateTime now = rtc.now(); // get the current time
if(interval){ // at start of program, or when sw1 is activated
if(now.second() != secondPrevious){ // every second
secondPrevious = now.second();
modeSwitchCounter++;
if(modeSwitchCounter >= loopWait){ // change mode 1<->2 after loopWait seconds
if(mode == 1){
mode = 2;
}else if(mode == 2){
mode = 1;
}
modeSwitchCounter = 0;
}
}
}//PREPARE VALUES/BYTES FOR SENDING TO DI OR MA
if(mode == 1) { // TIME
resetMA(); // all MA LEDs turned off
digitValue = (now.hour() * 100 + now.minute()); // calculate time in 4 digits
digit0 = digit[0] | B10000000; // turn LED1 on
if(now.minute() == 0 && now.second() < 10) { // every hour on minute 0 for 10 seconds play sequence1
counterMA++;
if(counterMA > 5) { // speed of sequence
counterMA = 0;
steps++;
if(steps > 4) {steps = 1;} // sequence of 4 steps
}
sequence1(steps);
}
if(now.minute() == 15 && now.second() < 10) { // every hour on minute 15 for 10 seconds play sequence1
counterMA++;
if(counterMA > 10) { // speed of sequence
counterMA = 1;
steps++;
if(steps > 4) {steps = 1;}
}
sequence2(steps);
}
if(now.minute() == 30 && now.second() < 10) { // every hour on minute 30 for 10 seconds play sequence1
counterMA++;
counterMA2++;
if(counterMA > 7) { // speed of sequence
counterMA = 1;
if(counterMA2 < 500) {
steps++;
if(steps > 6) {steps = 1;} // sequence of 6 steps
}
if(counterMA2 >= 500) {
steps–;
if(steps < 1) {steps = 6;}
}
if(counterMA2 > 1000) {counterMA2 = 0;}
}
sequence3(steps);
}
if(now.minute() == 45 && now.second() < 10) { // every hour on minute 45 for 10 seconds play sequence1
counterMA++;
if(counterMA > 5) { // speed of sequence
counterMA = 0;
if(counterMA3 / 2 == 0) {
if(counterMA2 < 4 && steps < 4) {steps++; counterMA2++;} // vertical lights
else if(counterMA2 < 7 && steps > 1) {steps–; counterMA2++;}
else {counterMA2 = 1; steps = 5; counterMA3++;}
}
else {
if(counterMA2 < 4 && steps < 8) {steps++; counterMA2++;} // horizontal lights
else if(counterMA2 < 7 && steps > 4) {steps–; counterMA2++;}
else {counterMA2 = 1; steps = 1; counterMA3 = 1;}
}
}
sequence4(steps);
}
}
if(mode == 2) { // TEMP
sensors_event_t event; // Prepares for the following events
dht.temperature().getEvent(&event); // Gets the temperature data from the DHT
digitValue = 100 * event.temperature; // Times 100 as digit4 should show ‘C’ (and digit2 ‘.’)
digit0 = digit[0] | B01000000; // turn LED2 on
if(now.minute() % takeTemp == 0 || tempMeasuredPrevious == 0) { // Every takeTemp minutes or at start of program
if(takeTempPrevious != now.minute()) { // just once per takeTemp minutes
takeTempPrevious = now.minute();
tempMeasuredPrevious = tempMeasured; // store previous tempMeasured
sensors_event_t event; // Prepares for the following events
dht.temperature().getEvent(&event); // Gets the temperature data from the DHT
tempMeasured = 10 * event.temperature; // to use decimals too
}
}
tempInMatrix(); // display change of temperature on MA
}
if(mode == 3) { // HUMIDITY
resetMA(); // all MA LEDs turned off
sensors_event_t event; // Prepares for the following events
dht.humidity().getEvent(&event); // Gets the relative humidity data from the DHT
digitValue = 10 * event.relative_humidity; // Times 10 as digit4 should show ‘H’
digit0 = digit[0] | B00100000; // turn LED3 on
}
if(mode == 4) { // LIGHT LEVEL
resetMA(); // all MA LEDs turned off
digit0 = digit[0] | B00010000; // turn LED 4 on
digitValue = map(light,0,1024,5,100); // maps reading to 5-100
}
if(mode == 5) { // NOISE LEVEL
if(noiseCounter < loopSizeNoise) { // continue display without changing values
noiseCounter++;
micVal = analogRead(MIC); // read value from microphone
micMin = min(micMin, micVal); // decreases micMin if micVal is less than it
micMax = max(micMax, micVal); // increases micMax if micVal is more than it
}
else { // change values
micReading = micMax – micMin; // calculates the average levels
digitValue = map(micReading,0,775,0,100); // Maps micReading to a percentage
noiseCounter = 0; // reset noise measuring loop variables
micMax = 0;
micMin = 1024;
}
}// SET BITS FOR RGB MATRIX TO DISPLAY NOISELEVEL
if(mode == 5){ // setting of LEDs in accordance with noiselevel
if(digitValue < 10) {
digit0 = digit0; // all LED off
row11 = row21 = row31 = B00000000; // MA A4 blue
row12 = row22 = row32 = B11110000;
row41 = B00001000; row42 = B11100000;
}
else if(digitValue < 16) {
digit0 = digit0 | B10000000; // LED1 on
row11 = row21 = row31 = B00000000; // MA A4-B4 blue
row12 = row22 = row32 = B11110000;
row41 = B00001100; row42 = B11100000;
}
else if(digitValue < 22) {
digit0 = digit0 | B10000000; // LED1 on
row11 = row21 = row31 = B00000000; // MA A4-C4 blue
row12 = row22 = row32 = B11110000;
row41 = B00001110; row42 = B11100000;
}
else if(digitValue < 28) {
digit0 = digit0 | B10000000; // LED1 on
row11 = row21 = row31 = B00000000; // MA A4-D4 blue
row12 = row22 = row32 = B11110000;
row41 = B00001111; row42 = B11100000;
}
else if(digitValue < 34) {
digit0 = digit0 | B11000000; // LED1,2 on
row11 = row21 = B00000000; // MA A1-D1 blue, A3 green
row12 = row22 = B11110000;
row31 = B10000000; row32 = B11010000;
row41 = B00001111; row42 = B11100000;
}
else if(digitValue < 40) {
digit0 = digit0 | B11000000; // LED1,2 on
row11 = row21 = B00000000; // MA A1-D1 blue, A3-B3 green
row12 = row22 = B11110000;
row31 = B11000000; row32 = B11010000;
row41 = B00001111; row42 = B11100000;
}
else if(digitValue < 46) {
digit0 = digit0 | B11000000; // LED1,2 on
row11 = row21 = B00000000; // MA A1-D1 blue, A3-C3 green
row12 = row22 = B11110000;
row31 = B11100000; row32 = B11010000;
row41 = B00001111; row42 = B11100000;
}
else if(digitValue < 52) {
digit0 = digit0 | B11000000; // LED1,2 on
row11 = row21 = B00000000; // MA A1-D1 blue, A3-D3 green
row12 = row22 = B11110000;
row31 = B11110000; row32 = B11010000;
row41 = B00001111; row42 = B11100000;
}
else if(digitValue < 58) {
digit0 = digit0 | B11100000; // LED1,2,3 on
row11 = B00000000; row12 = B11110000; // MA A1-D1 blue, A3-D3 green, A2 yellow
row21 = B10000000; row22 = B10111000;
row31 = B11110000; row32 = B11010000;
row41 = B00001111; row42 = B11100000;
}
else if(digitValue < 64) {
digit0 = digit0 | B11100000; // LED1,2,3 on
row11 = B00000000; row12 = B11110000; // MA A1-D1 blue, A3-D3 green, A2-B2 yellow
row21 = B11000000; row22 = B10111100;
row31 = B11110000; row32 = B11010000;
row41 = B00001111; row42 = B11100000;
}
else if(digitValue < 70) {
digit0 = digit0 | B11100000; // LED1,2,3 on
row11 = B00000000; row12 = B11110000; // MA A1-D1 blue, A3-D3 green, A2-C2 yellow
row21 = B11100000; row22 = B10111110;
row31 = B11110000; row32 = B11010000;
row41 = B00001111; row42 = B11100000;
}
else if(digitValue < 76) {
digit0 = digit0 | B11100000; // LED1,2,3 on
row11 = B00000000; row12 = B11110000; // MA A1-D1 blue, A3-D3 green, A2-D2 yellow
row21 = B11110000; row22 = B10111111;
row31 = B11110000; row32 = B11010000;
row41 = B00001111; row42 = B11100000;
}
else if(digitValue < 82) {
digit0 = digit0 | B11110000; // LED1,2,3,4 on
row11 = B00000000; row12 = B01111000; // MA A1-D1 blue, A3-D3 green, A2-D2 yellow, A1 red
row21 = B11110000; row22 = B10111111;
row31 = B11110000; row32 = B11010000;
row41 = B00001111; row42 = B11100000;
}
else if(digitValue < 86) {
digit0 = digit0 | B11110000; // LED1,2,3,4 on
row11 = B00000000; row12 = B01111100; // MA A1-D1 blue, A3-D3 green, A2-D2 yellow, A1-B1 red
row21 = B11110000; row22 = B10111111;
row31 = B11110000; row32 = B11010000;
row41 = B00001111; row42 = B11100000;
}
else if(digitValue < 92) {
digit0 = digit0 | B11110000; // LED1,2,3,4 on
row11 = B00000000; row12 = B01111110; // MA A1-D1 blue, A3-D3 green, A2-D2 yellow, A1-C1 red
row21 = B11110000; row22 = B10111111;
row31 = B11110000; row32 = B11010000;
row41 = B00001111; row42 = B11100000;
}
else if(digitValue >= 92) {
digit0 = digit0 | B11110000; // LED1,2,3,4 on
row11 = B00000000; // MA A1-D1 blue, A3-D3 green, A2-D2 yellow, A1-D1 red
row21 = B11110000;
row31 = B11110000;
row41 = B00001111;
if (now.second() % 2 == 0){ // MA all blinking per second
row12 = B01111111;
row22 = B10111111;
row32 = B11010000;
row42 = B11100000;
} else {
row12 = row22 = row32 = row42 = B11110000;
}
}
}// PREPARE VALUES FOR DIGITS
thousands = digitValue / 1000;
hundreds = (digitValue%1000)/100;
tens = (digitValue%100) / 10;
ones = (digitValue%10);
if(thousands == 0 && (mode != 1) && (mode != 2)){ // if no value for digit0 switch it off
digit0 = digit0 | B00000001;
}
if(thousands == 0 && hundreds == 0 && mode != 1){ // if also no value for digit1 switch it off unless display of time (e.g. 00.01)
digit1 = digit1 | B00000010;
}// OUTPUT TO DIGITISER
// SETTING DIGIT0
if(mode == 4){
digit0 ^= 1UL << 0; // switches state of digit0 (to on again, as it is supposed to be off)
digitalWrite(LATDI, LOW);
shiftOut(SERDI,CLKDI,LSBFIRST,letterL); // displays ‘L’ on digit0
shiftOut(SERDI,CLKDI,LSBFIRST,digit0);
digitalWrite(LATDI,HIGH);
}else{
digitalWrite(LATDI, LOW);
shiftOut(SERDI,CLKDI,LSBFIRST,number[thousands]);
shiftOut(SERDI,CLKDI,LSBFIRST,digit0);
digitalWrite(LATDI,HIGH);
}
delay(4);//SETTING DIGIT1
if(mode == 1){
decimalPoint = number[hundreds];
if (now.second() % 2 == 0){ // decimal point should be inserted in digit1 as in mode4, but blinking per second
decimalPoint &= ~(1UL << 3); // clears the 3rd bit (= the decimal point on)
} else {
decimalPoint |= 1UL << 3; // setting the 3rd bit (= the decimal point off)
}
digitalWrite(LATDI, LOW);
shiftOut(SERDI,CLKDI,LSBFIRST,decimalPoint);
shiftOut(SERDI,CLKDI,LSBFIRST,digit1);
digitalWrite(LATDI,HIGH);
} else if(mode == 2){ // decimal point should be inserted in digit1
decimalPoint = number[hundreds];
decimalPoint &= ~(1UL << 3); // clears the 3rd bit (= the decimal point on)
digitalWrite(LATDI, LOW);
shiftOut(SERDI,CLKDI,LSBFIRST,decimalPoint);
shiftOut(SERDI,CLKDI,LSBFIRST,digit1);
digitalWrite(LATDI,HIGH);
} else {
digitalWrite(LATDI, LOW);
shiftOut(SERDI,CLKDI,LSBFIRST,number[hundreds]);
shiftOut(SERDI,CLKDI,LSBFIRST,digit1);
digitalWrite(LATDI,HIGH);
}
delay(4);//SETTING DIGIT2
digitalWrite(LATDI, LOW);
shiftOut(SERDI,CLKDI,LSBFIRST,number[tens]);
shiftOut(SERDI,CLKDI,LSBFIRST,digit2);
digitalWrite(LATDI,HIGH);
delay(4);//SETTING DIGIT3
if(mode == 2){ // setting digit3
digitalWrite(LATDI, LOW);
shiftOut(SERDI,CLKDI,LSBFIRST,letterC);
shiftOut(SERDI,CLKDI,LSBFIRST,digit3);
digitalWrite(LATDI,HIGH);
}else if(mode == 3){
digitalWrite(LATDI, LOW);
shiftOut(SERDI,CLKDI,LSBFIRST,letterH);
shiftOut(SERDI,CLKDI,LSBFIRST,digit3);
digitalWrite(LATDI,HIGH);
}else{ // regular setting of digit3
digitalWrite(LATDI, LOW);
shiftOut(SERDI,CLKDI,LSBFIRST,number[ones]);
shiftOut(SERDI,CLKDI,LSBFIRST,digit3);
digitalWrite(LATDI,HIGH);
}
delay(1);//SETTING MATRIX
setMA();
} -
March 29, 2022 at 4:23 pm #6914
Martyn
Wow Peter, that’s some epic coding right there! It’s going to take me a bit to digest it all. If it works and it fits in the ATMega328P’s memory, then bravo!
There are lots of ways to condense code, but it gets more unreadable the more you condense it. A lot of the nifty efficient coding is beyond my level of knowledge, to be honest. To make things neater, you could use separate sketches for each function and call those like you would a library.
Here is a tutorial I found after a quick search (I haven’t followed it or looked at it in detail, but it seems to outline what I’m talking about). https://www.youtube.com/watch?v=2pxYEwaMtaI
Apologies for the late reply. I’m getting ready for a trade show at the moment, so I’m building boards, designing backdrops, packing kits and sorting insurance (who knew buying from China and selling to America was so uninsurable…).
-
May 25, 2022 at 6:18 pm #7191
kkttbogart
Just got done making this and getting it programmed. Another tip for some errors if you copy and paste from here. Make sure minus(-) signs are removed and added back in. Also make sure the decrement uses 2 minus(-) signs. This will give a (;) error if you dont fix those.
Thanks for this code. If I get more enhancements I will post updates.
Kevin
-
-
AuthorPosts
- You must be logged in to reply to this topic.