Last active
March 1, 2021 20:49
-
-
Save jwhb/43eba3bd308bf69bfa46 to your computer and use it in GitHub Desktop.
MSC1937 16-Digit Segment Display Arduino Sketch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int DATA = 2; | |
int RST = 4; | |
int SCLK = 7; | |
void setup() { | |
pinMode(DATA, OUTPUT); | |
pinMode(RST, OUTPUT); | |
pinMode(SCLK, OUTPUT); | |
//randomSeed(analogRead(0)); | |
Serial.begin(9600); | |
ResetAll('-'); | |
BootAnim(); | |
} | |
void loop() { | |
String str; | |
while (Serial.available() > 0) { | |
str = Serial.readStringUntil('\n'); | |
if(str.equalsIgnoreCase("\\boot")) BootAnim(); | |
else if(str.equalsIgnoreCase("\\reset")) ResetDisplay(); | |
else if(str.startsWith("\\brightness:")) { | |
String brt_str = str.substring(str.indexOf(":") + 1); | |
SetBrightness(brt_str.toInt()); | |
} else if(str.startsWith("\\resetall:")) { | |
ResetAll(str.charAt(str.indexOf(":") + 1)); | |
} else if(str.startsWith("\\position:")) { | |
String pos_str = str.substring(str.indexOf(":") + 1); | |
SetPosition(pos_str.toInt()); | |
} else { | |
WriteString(str); | |
} | |
Serial.println(str); | |
} | |
} | |
void BootAnim() { | |
char c = '*'; | |
char d = '-'; | |
byte dly = 100; | |
ResetAll(' '); | |
WriteChar(c); | |
int i = 2; | |
while(i <= 16) { | |
delay(dly); | |
SetPosition(i - 1); | |
WriteChar(d); | |
WriteChar(c); | |
i++; | |
} | |
i = 15; | |
while(i > 0) { | |
delay(dly); | |
SetPosition(i); | |
WriteChar(c); | |
WriteChar(d); | |
i--; | |
} | |
delay(dly); | |
SetPosition(1); | |
WriteChar(d); | |
delay(4 * dly); | |
SetPosition(1); | |
WriteString(":)Hello World(:"); | |
} | |
byte WriteString(String s) { | |
for (int i = 0; i < s.length(); i++) { | |
WriteChar(s.charAt(i)); | |
} | |
} | |
byte WriteChar(char c) { | |
WriteData(GetChar(c)); | |
} | |
byte GetChar(char c) { | |
byte i_c = c; | |
if (c >= 97 && c <= 126) i_c -= 32; // lowercase => uppercase, special chars to equivalents | |
else if (c >= 32 && c <= 63); // do nothing (equivalent); | |
else if (c >= 64 && c <= 95) i_c -= 64; //@ and uppercase chars and [/]^_ | |
else i_c = 32; // blank if unsupported char | |
return i_c; | |
} | |
/* 0 - 31 */ | |
byte SetBrightness(int val) { | |
byte dat = byte(224 + val); | |
WriteData(dat); | |
return dat; | |
} | |
/* Values: 0-15 | |
* 0: All chars | |
*/ | |
byte SetLength(int len) { | |
byte dat = byte(192 + len); | |
WriteData(dat); | |
return dat; | |
} | |
/* Values: 1-16 | |
*/ | |
byte SetPosition(int pos) { | |
int i_pos = pos - 2; | |
if(pos == 1) i_pos = 15; | |
byte dat = byte(160 + i_pos); | |
WriteData(dat); | |
return dat; | |
} | |
void ResetAll(char c) { | |
ResetDisplay(); | |
SetPosition(1); | |
for(int i=0; i < 16; i++) WriteChar(c); | |
SetBrightness(31); | |
} | |
void ResetDisplay() { | |
digitalWrite(RST, LOW); | |
delay(2); | |
digitalWrite(RST, HIGH); | |
delay(2); | |
} | |
void WriteData(byte dat) { | |
for (int i = 7 ; i >= 0; i--) { | |
digitalWrite(DATA, bitRead(dat, i)); | |
delayMicroseconds(1); | |
digitalWrite(SCLK, HIGH); | |
delayMicroseconds(1); | |
digitalWrite(SCLK, LOW); | |
delayMicroseconds(2); | |
digitalWrite(SCLK, HIGH); | |
delayMicroseconds(1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment