LimePaste888

quadalphanum but it's a counter for pushbutton with led ring

Apr 29th, 2021 (edited)
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Demo the quad alphanumeric display LED backpack kit
  2. // Counts up and lights up LED when the button is pressed
  3. // Update: Fixed button and LED not lighting up
  4. // Update: lastButtonState set to buttonState after updating buttonState
  5.  
  6. #include <Wire.h>
  7. #include <Adafruit_GFX.h>
  8. #include "Adafruit_LEDBackpack.h"
  9.  
  10. Adafruit_AlphaNum4 alpha4 = Adafruit_AlphaNum4();
  11.  
  12. void setup() {
  13.   pinMode(3, OUTPUT);
  14.   pinMode(7, INPUT_PULLUP);
  15.  
  16.   alpha4.begin(0x70);
  17.  
  18.   alpha4.writeDigitRaw(3, 0x0);
  19.   alpha4.writeDigitRaw(0, 0xFFFF);
  20.   alpha4.writeDisplay();
  21.   delay(200);
  22.   alpha4.writeDigitRaw(0, 0x0);
  23.   alpha4.writeDigitRaw(1, 0xFFFF);
  24.   alpha4.writeDisplay();
  25.   delay(200);
  26.   alpha4.writeDigitRaw(1, 0x0);
  27.   alpha4.writeDigitRaw(2, 0xFFFF);
  28.   alpha4.writeDisplay();
  29.   delay(200);
  30.   alpha4.writeDigitRaw(2, 0x0);
  31.   alpha4.writeDigitRaw(3, 0xFFFF);
  32.   alpha4.writeDisplay();
  33.   delay(200);
  34.  
  35.   alpha4.clear();
  36.   alpha4.writeDisplay();
  37.  
  38. }
  39.  
  40. int count = 0;
  41. int buttonState = 0;
  42. int lastButtonState = LOW;
  43.  
  44. void loop() {
  45.  
  46.   if (buttonState == HIGH and lastButtonState != buttonState) {
  47.     count = count + 1;
  48.   }
  49.   if (count > 9999) {
  50.     count = 0;
  51.   }
  52.   digitalWrite(3, buttonState);
  53.   String countString = String(count);
  54.   lastButtonState = buttonState;
  55.   buttonState = digitalRead(7);
  56.  
  57.   alpha4.writeDigitAscii(0, countString.charAt(-4 + countString.length()));
  58.   alpha4.writeDigitAscii(1, countString.charAt(-3 + countString.length()));
  59.   alpha4.writeDigitAscii(2, countString.charAt(-2 + countString.length()));
  60.   alpha4.writeDigitAscii(3, countString.charAt(-1 + countString.length()));
  61.  
  62.   alpha4.writeDisplay();
  63.   alpha4.clear();
  64.   delay(20);
  65.  
  66. }
Add Comment
Please, Sign In to add comment