From: gzafirov Date: Sun, 10 Mar 2024 13:00:24 +0000 (+0200) Subject: ShiftRegister Example Bounds check X-Git-Url: https://kolegite.com/gitweb/?a=commitdiff_plain;h=870256c81b89394412f40ac99055ae2acb36cbb9;p=vmks.git ShiftRegister Example Bounds check --- diff --git a/Examples/other/shift-register-hc595/source.ino b/Examples/other/shift-register-hc595/source.ino index 80bc4f0..b5cec1f 100644 --- a/Examples/other/shift-register-hc595/source.ino +++ b/Examples/other/shift-register-hc595/source.ino @@ -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); }