diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2024-11-29 12:42:41 -0500 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2024-11-29 12:42:41 -0500 |
| commit | c42952fe1660f0bf3799815aff56db4df3ff87a6 (patch) | |
| tree | 89d36499329daa037e752155feef5c9323c96ce5 /HvacStation | |
| parent | f18d89b89886e60230933303df1ac1eb7e0a00fc (diff) | |
| download | soen422-c42952fe1660f0bf3799815aff56db4df3ff87a6.zip | |
HvacStation: refactor led bar
Diffstat (limited to 'HvacStation')
| -rw-r--r-- | HvacStation/HvacStation.ino | 26 |
1 files changed, 21 insertions, 5 deletions
diff --git a/HvacStation/HvacStation.ino b/HvacStation/HvacStation.ino index 1e901fd..1678be2 100644 --- a/HvacStation/HvacStation.ino +++ b/HvacStation/HvacStation.ino @@ -57,9 +57,8 @@ setup(void) { // Clear shift register. digitalWrite(REG_SH, LOW); digitalWrite(REG_ST, LOW); - digitalWrite(REG_CLR, LOW); - delay(20); - digitalWrite(REG_CLR, HIGH); + digitalWrite(REG_DS, LOW); + regClear(); Serial.begin(9600); while (!Serial) {} @@ -161,25 +160,42 @@ refreshDisplay(float target, float humidity, float dutycycle) { void refreshLedBar(float dutycycle) { - int out, i; + int out; + // Number of LEDs to illuminate. out = dutycycle * REG_SIZE / 100; out = clamp(out, 0, REG_SIZE); + regClear(); + regWrite(out); +} + +// Clear the shift register connected to the LED bar. +void +regClear(void) { digitalWrite(REG_CLR, LOW); delay(10); digitalWrite(REG_CLR, HIGH); delay(10); +} +// Set the first n bits of the shift register connected to the LED bar. +void +regWrite(int n) { + int i; + + // Write bits. digitalWrite(REG_DS, HIGH); delay(10); - for (i = 0; i < out; i++) { + for (i = 0; i < n; i++) { digitalWrite(REG_SH, HIGH); delay(10); digitalWrite(REG_SH, LOW); delay(10); } digitalWrite(REG_DS, LOW); + + // Store. digitalWrite(REG_ST, HIGH); delay(10); digitalWrite(REG_ST, LOW); |