Skip to content

Instantly share code, notes, and snippets.

@EllieTheYeen
Last active June 1, 2024 00:51
Show Gist options
  • Save EllieTheYeen/0cd13a56f191e682a8a8ecc7400f1f58 to your computer and use it in GitHub Desktop.
Save EllieTheYeen/0cd13a56f191e682a8a8ecc7400f1f58 to your computer and use it in GitHub Desktop.
This is just some test thing for the Arduino multi-function shield or as it funnily is spelled on the board "Mulie-function shidle" where it parses numbers in CSV lines and turns on the 7 segment display and LEDs plus some debouncing that does not work that well
#define BUZ 3
#define KNB A0
#define BT1 A1
#define BT2 A2
#define BT3 A3
#define LD1 10
#define LD2 11
#define LD3 12
#define LD4 13
#define DAT 8
#define CLK 7
#define LAT 4
uint8_t bpn[] = { BT1, BT2, BT3 }, bps[sizeof(bpn)] = { 0 }, hasval = 0, glyphs[4] = { 0 }, glpos = 0;
uint16_t everysectime = 0, lastaread = 0, last7seg = 0, lastup = 0, rval = 0, rpos = 0, dev = 0, lastanalog = 9999, thenum = 0;
void setup() {
pinMode(KNB, INPUT);
pinMode(BT1, INPUT_PULLUP);
pinMode(BT2, INPUT_PULLUP);
pinMode(BT3, INPUT_PULLUP);
pinMode(LAT, OUTPUT);
pinMode(CLK, OUTPUT);
pinMode(DAT, OUTPUT);
pinMode(BUZ, OUTPUT);
pinMode(LD1, OUTPUT);
pinMode(LD2, OUTPUT);
pinMode(LD3, OUTPUT);
pinMode(LD4, OUTPUT);
Serial.begin(9600);
digitalWrite(BUZ, LOW);
delay(10);
digitalWrite(BUZ, HIGH);
setLeds(0);
sendShift(0);
}
void loop() {
readButtons();
readAnalogs();
checkTimers();
readSerial();
emit7Seg();
}
void onValue(uint16_t pos, uint16_t val, uint8_t end) {
if (pos == 0 && !end) {
dev = val;
}
if (pos == 1 || (end && pos == 0)) {
useDevice(dev, val);
}
}
void checkTimers() {
uint16_t cur = millis();
if (abs(cur - everysectime) >= 1000) {
everysectime = cur;
everySecond();
}
}
void setLeds(uint8_t n) {
digitalWrite(LD1, !(n & 1));
digitalWrite(LD2, !((n >> 1) & 1));
digitalWrite(LD3, !((n >> 2) & 1));
digitalWrite(LD4, !((n >> 3) & 1));
}
void readAnalogs() {
uint16_t now = millis();
if (abs(now - lastaread) < 100) return;
lastaread = now;
int16_t read = analogRead(KNB);
if (abs(read - lastanalog) >= 3) {
lastanalog = read;
Serial.print("3,");
Serial.print(read);
Serial.print(",");
Serial.println(now);
}
}
void readSerial() {
int16_t read = Serial.read();
if (read == -1) {
return;
} else if (read == ',') {
endField(false);
rpos++;
} else if (read == '\n') {
endField(true);
rpos = 0;
} else if (read >= '0' && read <= '9') {
if (!hasval) {
hasval = 1;
}
rval *= 10;
rval += read - '0';
}
}
void endField(uint8_t end) {
if (!hasval) return;
onValue(rpos, rval, end);
hasval = 0, rval = 0;
}
void readButtons() {
uint16_t now = millis();
for (uint8_t i = 0; i < sizeof(bpn); i++) {
bool down = !digitalRead(bpn[i]);
if (bps[i] != down) {
if (!down && abs(now - lastup) < 20) {
return;
}
lastup = now;
bps[i] = down;
onButton(i, down);
}
}
}
void onButton(uint8_t butto, uint8_t state) {
Serial.print(butto);
Serial.print(",");
Serial.print(state);
Serial.print(",");
Serial.println(millis());
/*if (state == 1) {
if (butto == 2) {
thenum++;
} else if (butto == 1) {
thenum += 10;
} else if (butto == 0) {
thenum += 100;
//glyphs[0] = glyphs[1] = glyphs[2] = glyphs[3] = 0;
//sendShift(0);
}
if (thenum > 9999) thenum = 0;
set7SegNum(thenum);
}*/
}
void sendShift(uint16_t i) {
digitalWrite(LAT, LOW);
shiftOut(DAT, CLK, LSBFIRST, (i >> 8) & 255);
shiftOut(DAT, CLK, LSBFIRST, i & 255);
digitalWrite(LAT, HIGH);
}
void emit7Seg() {
if (glyphs[0] == 0 && glyphs[1] == 0 && glyphs[2] == 0 && glyphs[3] == 0) return;
uint16_t now = millis();
if (abs(now - last7seg) < 5) return;
last7seg = now;
glpos = (glpos + 1) % 4;
uint16_t out = 0;
out |= ((~glyphs[glpos]) << 8);
out |= 128 >> glpos;
sendShift(out);
}
void set7SegNum(uint16_t num) {
thenum = num;
if (num > 9999) {
glyphs[0] = 0;
glyphs[1] = 0;
glyphs[2] = 0;
glyphs[3] = 0;
return;
}
int16_t thousands = num / 1000;
if (thousands) {
glyphs[0] = numToGlyph(thousands);
} else {
glyphs[0] = 0;
}
num = num - (thousands * 1000);
int hundreds = num / 100;
if (hundreds || thousands) {
glyphs[1] = numToGlyph(hundreds);
} else {
glyphs[1] = 0;
}
num = num - (hundreds * 100);
int tens = num / 10;
if (tens || hundreds || thousands) {
glyphs[2] = numToGlyph(tens);
} else {
glyphs[2] = 0;
}
num = num - (tens * 10);
glyphs[3] = numToGlyph(num);
}
uint8_t numToGlyph(uint8_t i) {
switch (i) {
case 0:
return B11111100;
case 1:
return B01100000;
case 2:
return B11011010;
case 3:
return B11110010;
case 4:
return B01100110;
case 5:
return B10110110;
case 6:
return B10111110;
case 7:
return B11100000;
case 8:
return B11111110;
case 9:
return B11110110;
default:
return 0;
}
}
void everySecond() {
uint16_t n = random();
}
void useDevice(uint16_t dev, uint16_t val) {
Serial.print("dev=");
Serial.print(dev);
Serial.print(" val=");
Serial.println(val);
if (dev == 0) {
set7SegNum(val);
} else if (dev == 1) {
setLeds(val);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment