Last active
July 18, 2020 10:35
-
-
Save buildcircuit/8adc8ab6ec3f6b247801d5ff6c99c59a to your computer and use it in GitHub Desktop.
Up and down counter module example for 2.3" display
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// connect 9V to the counter module | |
// Connect 5V of Arduino to 5V of module | |
// Connect GND of Arduino to GND of module | |
// Connect pin 13 of Arduino to pin 5 of module | |
// Connect pin 12 of Arduino to pin 4 of module | |
// Connect pin 11 of Arduino to pin 14 of module | |
// other connections | |
//Use this example: https://www.arduino.cc/en/tutorial/debounce and make two exactly same circuits, connect one switch to pin 2 and other to pin 3 of Arduino | |
const int UpCountPin = 2; | |
const int UpPulsePin = 13; | |
const int resetpin=11; | |
const int DownCountPin = 3; | |
const int DownPulsePin = 12; | |
int UpCountState = HIGH; | |
int buttonStateUp; | |
int lastbuttonStateUp = LOW; | |
int DownCountState = HIGH; | |
int buttonStateDown; | |
int lastbuttonStateDown = LOW; | |
unsigned long lastDebounceTimeforUp = 0; | |
unsigned long debounceDelayforUp = 50; | |
unsigned long lastDebounceTimeforDown = 0; | |
unsigned long debounceDelayforDown = 50; | |
void setup() { | |
pinMode(UpCountPin, INPUT); | |
pinMode(UpPulsePin, OUTPUT); | |
pinMode(resetpin,OUTPUT); | |
pinMode(DownCountPin, INPUT); | |
pinMode(DownPulsePin, OUTPUT); | |
// set initial LED state | |
digitalWrite(UpPulsePin, UpCountState); | |
digitalWrite(DownPulsePin, DownCountState); | |
digitalWrite(resetpin, HIGH); | |
delay(100); | |
digitalWrite(resetpin, LOW); | |
} | |
void loop() { | |
int readingUp = digitalRead(UpCountPin); | |
int readingDown = digitalRead(DownCountPin); | |
if (readingUp != lastbuttonStateUp) { | |
// reset the debouncing timer | |
lastDebounceTimeforUp = millis(); | |
} | |
if (readingDown != lastbuttonStateDown) { | |
// reset the debouncing timer | |
lastDebounceTimeforDown = millis(); | |
} | |
if ((millis() - lastDebounceTimeforUp) > debounceDelayforUp) { | |
if (readingUp != buttonStateUp) { | |
buttonStateUp = readingUp; | |
if (buttonStateUp == HIGH) { | |
UpCountState = !UpCountState; | |
} | |
} | |
} | |
if ((millis() - lastDebounceTimeforDown) > debounceDelayforDown) { | |
if (readingDown != buttonStateDown) { | |
buttonStateDown = readingDown; | |
if (buttonStateDown == HIGH) { | |
DownCountState = !DownCountState; | |
} | |
} | |
} | |
digitalWrite(UpPulsePin, UpCountState); | |
digitalWrite(DownPulsePin, DownCountState); | |
lastbuttonStateUp = readingUp; | |
lastbuttonStateDown = readingDown; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://www.buildcircuit.net/23