]> kolegite.com Git - vmks.git/commitdiff
ShiftRegister Example Bounds check
authorgzafirov <gzafirov@elsys-bg.org>
Sun, 10 Mar 2024 13:00:24 +0000 (15:00 +0200)
committergzafirov <gzafirov@elsys-bg.org>
Sun, 10 Mar 2024 13:00:24 +0000 (15:00 +0200)
Examples/other/shift-register-hc595/source.ino

index 80bc4f0fa90f018a5a8d4636ffa4ee636851a4ec..b5cec1fc52ce87847f5f12649a46be96c8fab39a 100644 (file)
@@ -9,31 +9,30 @@ const uint8_t shreg_pins[] = {
 
 void setup()
 {
-       for (int i = 0; i < 4; ++i)
+       for (uint8_t i = 0; i < (sizeof(shreg_pins)/sizeof(shreg_pins[0])); ++i){
                pinMode(shreg_pins[i], OUTPUT);
+       }
+       
        digitalWrite(shreg_stcp_pin, LOW);
        digitalWrite(shreg_shcp_pin, LOW);
 }
 
 void shreg_write(uint8_t data) {
-       for (int i = 0; i < 8; ++i) {
+       for (uint8_t i = 0; i < 8; ++i) {
                digitalWrite(shreg_shcp_pin, LOW);
-               delay(10);
-               unsigned val = data & (0x80 >> i);
+               uint8_t val = data & (0x80 >> i);
                digitalWrite(shreg_input_pin, val > 0);
                digitalWrite(shreg_shcp_pin, HIGH);
-               delay(10);
        }
        digitalWrite(shreg_shcp_pin, LOW);
 
        digitalWrite(shreg_stcp_pin, HIGH);
-       delay(10);
        digitalWrite(shreg_stcp_pin, LOW);
 }
 
 void loop()
 {
-       for (int i = 0; i < 8; ++i) {
+       for (uint8_t i = 0; i < 8; ++i) {
                shreg_write(1 << i);
                delay(500);
        }