From 107df3253c7b7a30f46dbea0022876b65da9e523 Mon Sep 17 00:00:00 2001 From: paul Date: Tue, 21 Jan 2025 13:37:46 -0500 Subject: [PATCH] ardunio code for the connected LED matrix for the elevator --- led_matrix.ino | 182 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 182 insertions(+) create mode 100644 led_matrix.ino diff --git a/led_matrix.ino b/led_matrix.ino new file mode 100644 index 0000000..c108bc5 --- /dev/null +++ b/led_matrix.ino @@ -0,0 +1,182 @@ +#include "LedControl.h" +/* + pin 12 is connected to the DataIn + pin 11 is connected to LOAD(CS) + pin 10 is connected to the CLK + */ +LedControl lc=LedControl(12,10,11,1); + +// web url to make the matrices +// https://xantorohara.github.io/led-matrix-editor/#18181899db7e3c18|18181899db7e3c18|0018181899db7e3c|000018181899db7e|00000018181899db|0000000018181899|1800000000181818|3c18000000001818|7e3c180000000018|db7e3c1800000000|99db7e3c18000000|1899db7e3c180000|181899db7e3c1800|18181899db7e3c18 + +const uint64_t UPARROW[] = { + 0x18181899db7e3c18,0x0018181899db7e3c,0x000018181899db7e,0x00000018181899db, + 0x0000000018181899,0x1800000000181818,0x3c18000000001818,0x7e3c180000000018, + 0xdb7e3c1800000000,0x99db7e3c18000000,0x1899db7e3c180000,0x181899db7e3c1800 +}; + +const uint64_t DOWNARROW[] = { + 0x183c7edb99181818,0x3c7edb9918181800,0x7edb991818180000,0xdb99181818000000, + 0x9918181800000000,0x1818180000000018,0x181800000000183c,0x1800000000183c7e, + 0x00000000183c7edb,0x000000183c7edb99,0x0000183c7edb9918,0x00183c7edb991818 +}; + +const uint64_t ATTHETOP[] = { + 0x1899db7e3c1800ff +}; + +const uint64_t ATTHEBOTTOM[] = { + 0xff00183c7edb9918 +}; + +const uint64_t HELPBUTTON[] = { + 0x18ff814242423c18 +}; + +const int UPARROW_LEN = sizeof(UPARROW)/8; +const int DOWNARROW_LEN = sizeof(DOWNARROW)/8; +const int ATTHEBOTTOM_LEN = sizeof(ATTHEBOTTOM)/8; +const int ATTHETOP_LEN = sizeof(ATTHETOP)/8; +const int HELPBUTTON_LEN = sizeof(HELPBUTTON)/8; +int i = 0; +int RX_data=0; +bool goingup, goingdown, atthetop, atthebottom, helpbutton = false; +int topbottomtimer = 0; + +void setup() { + // put your setup code here, to run once: + Serial.begin(9600); + lc.shutdown(0,true); + /* Set the brightness to a medium values */ + lc.setIntensity(0,0); + /* and clear the display */ + lc.clearDisplay(0); +} + +void loop() { + // let's read the serial port first to see what action we need to take. + if(Serial.available()) + { + RX_data = Serial.read(); + } + + // now we take that data and make our decisions + if(RX_data == '1') // elevator going down is 1 + { + goingup = false; + goingdown = true; + } + else if(RX_data == '2') // elevator going up is 2 + { + goingdown = false; + goingup = true; + } + else if(RX_data == '3') // elevator stopped is 3 + { + goingup = false; + goingdown = false; + atthebottom = false; + atthetop = false; + } + else if(RX_data == '4') // elevator has reached the top + { + goingup = false; + goingdown = false; + atthebottom = false; + atthetop = true; + } + else if(RX_data == '5') // elevator is at the bottom + { + goingup = false; + goingdown = false; + atthetop = false; + atthebottom = true; + } + // for the help button, we only change its status, so when it is pressed/unpressed + // whatever action was happening before will continue + else if(RX_data == '6') // someone pressed the help button + { + helpbutton = true; + } + else if (RX_data == '7') // the help button is not pressed anymore + { + helpbutton = false; + } + + if (atthetop || atthebottom) + { + if (topbottomtimer <= 100) + { + topbottomtimer++; + } + } + + // now that we've set the application state based on the serial input, make the LEDs happen! + if (helpbutton) + { + lc.shutdown(0,false); + displayImage(HELPBUTTON[i]); + if (++i >= HELPBUTTON_LEN ) + { + i = 0; + } + delay(100); + } + else if (goingup) + { + lc.shutdown(0,false); + displayImage(UPARROW[i]); + if (++i >= UPARROW_LEN ) + { + i = 0; + } + delay(100); + topbottomtimer = 0; + } + else if (goingdown) + { + lc.shutdown(0,false); + displayImage(DOWNARROW[i]); + if (++i >= DOWNARROW_LEN ) + { + i = 0; + } + delay(100); + topbottomtimer = 0; + } + else if (atthetop && topbottomtimer < 100) + { + lc.shutdown(0,false); + displayImage(ATTHETOP[i]); + if (++i >= ATTHETOP_LEN ) + { + i = 0; + } + delay(100); + } + else if (atthebottom && topbottomtimer < 100) + { + lc.shutdown(0,false); + displayImage(ATTHEBOTTOM[i]); + if (++i >= ATTHEBOTTOM_LEN ) + { + i = 0; + } + delay(100); + } + // otherwise, turn off the display to save energy + else + { + lc.clearDisplay(0); + lc.shutdown(0,true); + } +} + +void displayImage(uint64_t image) { + for (int i = 0; i < 8; i++) { + byte row = (image >> i * 8) & 0xFF; + for (int j = 0; j < 8; j++) { + lc.setLed(0, i, j, bitRead(row, j)); + } + } +}