Created
July 14, 2020 07:03
-
-
Save EDISON-SCIENCE-CORNER/c0d95932c38200bc5f7ffc81c800f907 to your computer and use it in GitHub Desktop.
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
https://edisonsciencecorner.blogspot.com/ | |
https://edisonsciencecorner.blogspot.com/ | |
https://edisonsciencecorner.blogspot.com/ | |
#include <Wire.h> | |
#include <LiquidCrystal_I2C.h> | |
LiquidCrystal_I2C lcd(0x27,16,2); | |
int counter = 0; | |
const int Up_buttonPin = 2; | |
const int Down_buttonPin = 3; | |
int buttonPushCounter = 0; // counter for the number of button presses | |
int up_buttonState = 0; // current state of the up button | |
int up_lastButtonState = 0; // previous state of the up button | |
int down_buttonState = 0; // current state of the up button | |
int down_lastButtonState = 0; // previous state of the up button | |
bool bPress = false; | |
byte LT[8] = | |
{ | |
B00111, | |
B01111, | |
B11111, | |
B11111, | |
B11111, | |
B11111, | |
B11111, | |
B11111 | |
}; | |
byte UB[8] = | |
{ | |
B11111, | |
B11111, | |
B11111, | |
B00000, | |
B00000, | |
B00000, | |
B00000, | |
B00000 | |
}; | |
byte RT[8] = | |
{ | |
B11100, | |
B11110, | |
B11111, | |
B11111, | |
B11111, | |
B11111, | |
B11111, | |
B11111 | |
}; | |
byte LL[8] = | |
{ | |
B11111, | |
B11111, | |
B11111, | |
B11111, | |
B11111, | |
B11111, | |
B01111, | |
B00111 | |
}; | |
byte LB[8] = | |
{ | |
B00000, | |
B00000, | |
B00000, | |
B00000, | |
B00000, | |
B11111, | |
B11111, | |
B11111 | |
}; | |
byte LR[8] = | |
{ | |
B11111, | |
B11111, | |
B11111, | |
B11111, | |
B11111, | |
B11111, | |
B11110, | |
B11100 | |
}; | |
byte MB[8] = | |
{ | |
B11111, | |
B11111, | |
B11111, | |
B00000, | |
B00000, | |
B00000, | |
B11111, | |
B11111 | |
}; | |
byte block[8] = | |
{ | |
B11111, | |
B11111, | |
B11111, | |
B11111, | |
B11111, | |
B11111, | |
B11111, | |
B11111 | |
}; | |
void setup() | |
{ | |
pinMode( Up_buttonPin , INPUT_PULLUP); | |
pinMode( Down_buttonPin , INPUT_PULLUP); | |
lcd.begin(); // initialize the lcd | |
lcd.createChar(0,LT); | |
lcd.createChar(1,UB); | |
lcd.createChar(2,RT); | |
lcd.createChar(3,LL); | |
lcd.createChar(4,LB); | |
lcd.createChar(5,LR); | |
lcd.createChar(6,MB); | |
lcd.createChar(7,block); | |
// Print a message to the LCD. | |
lcd.backlight(); | |
lcd.clear(); | |
displayNumber(); | |
} | |
void printNumber(int val){ | |
int col=5; | |
if( val >= 10){ | |
printDigits(val/10,col); | |
printDigits(val%10,col+4); | |
} | |
else{ | |
printDigits(val,col); | |
} | |
} | |
void loop() | |
{ | |
checkUp(); | |
checkDown(); | |
if( bPress){ | |
bPress = false; | |
displayNumber(); | |
} | |
} | |
void displayNumber(){ | |
lcd.clear(); | |
lcd.setCursor(0,0); | |
lcd.print("NO:"); | |
lcd.setCursor(10,0); | |
printNumber( buttonPushCounter); | |
} | |
void custom0(int x){ | |
lcd.setCursor(x,0); | |
lcd.write(0); | |
lcd.write(1); | |
lcd.write(2); | |
lcd.setCursor(x, 1); | |
lcd.write(3); | |
lcd.write(4); | |
lcd.write(5); | |
} | |
void custom1(int x){ | |
lcd.setCursor(x,0); | |
lcd.write(1); | |
lcd.write(2); | |
lcd.print(" "); | |
lcd.setCursor(x,1); | |
lcd.write(4); | |
lcd.write(7); | |
lcd.write(4); | |
} | |
void custom2(int x){ | |
lcd.setCursor(x,0); | |
lcd.write(6); | |
lcd.write(6); | |
lcd.write(2); | |
lcd.setCursor(x, 1); | |
lcd.write(3); | |
lcd.write(4); | |
lcd.write(4); | |
} | |
void custom3(int x){ | |
lcd.setCursor(x,0); | |
lcd.write(6); | |
lcd.write(6); | |
lcd.write(2); | |
lcd.setCursor(x, 1); | |
lcd.write(4); | |
lcd.write(4); | |
lcd.write(5); | |
} | |
void custom4(int x){ | |
lcd.setCursor(x,0); | |
lcd.write(3); | |
lcd.write(4); | |
lcd.write(7); | |
lcd.setCursor(x, 1); | |
lcd.print(" "); | |
lcd.print(" "); | |
lcd.write(7); | |
} | |
void custom5(int x){ | |
lcd.setCursor(x,0); | |
lcd.write(3); | |
lcd.write(6); | |
lcd.write(6); | |
lcd.setCursor(x, 1); | |
lcd.write(4); | |
lcd.write(4); | |
lcd.write(5); | |
} | |
void custom6(int x){ | |
lcd.setCursor(x,0); | |
lcd.write(0); | |
lcd.write(6); | |
lcd.write(6); | |
lcd.setCursor(x, 1); | |
lcd.write(3); | |
lcd.write(4); | |
lcd.write(5); | |
} | |
void custom7(int x){ | |
lcd.setCursor(x,0); | |
lcd.write(1); | |
lcd.write(1); | |
lcd.write(2); | |
lcd.setCursor(x, 1); | |
lcd.print(" "); | |
lcd.print(" "); | |
lcd.write(7); | |
} | |
void custom8(int x){ | |
lcd.setCursor(x,0); | |
lcd.write(0); | |
lcd.write(6); | |
lcd.write(2); | |
lcd.setCursor(x, 1); | |
lcd.write(3); | |
lcd.write(4); | |
lcd.write(5); | |
} | |
void custom9(int x){ | |
lcd.setCursor(x,0); | |
lcd.write(0); | |
lcd.write(6); | |
lcd.write(2); | |
lcd.setCursor(x, 1); | |
lcd.print(" "); | |
lcd.print(" "); | |
lcd.write(7); | |
} | |
void printDigits(int digits, int x){ | |
// utility function for digital clock display: prints preceding colon and leading 0 | |
switch (digits) { | |
case 0: | |
custom0(x); | |
break; | |
case 1: | |
custom1(x); | |
break; | |
case 2: | |
custom2(x); | |
break; | |
case 3: | |
custom3(x); | |
break; | |
case 4: | |
custom4(x); | |
break; | |
case 5: | |
custom5(x); | |
break; | |
case 6: | |
custom6(x); | |
break; | |
case 7: | |
custom7(x); | |
break; | |
case 8: | |
custom8(x); | |
break; | |
case 9: | |
custom9(x); | |
break; | |
} | |
} | |
void checkUp() | |
{ | |
up_buttonState = digitalRead(Up_buttonPin); | |
// compare the buttonState to its previous state | |
if (up_buttonState != up_lastButtonState) { | |
// if the state has changed, increment the counter | |
if (up_buttonState == LOW) { | |
bPress = true; | |
// if the current state is HIGH then the button went from off to on: | |
buttonPushCounter++; | |
Serial.println("on"); | |
Serial.print("number of button pushes: "); | |
Serial.println(buttonPushCounter); | |
} else { | |
// if the current state is LOW then the button went from on to off: | |
Serial.println("off"); | |
} | |
// Delay a little bit to avoid bouncing | |
delay(50); | |
} | |
// save the current state as the last state, for next time through the loop | |
up_lastButtonState = up_buttonState; | |
} | |
void checkDown() | |
{ | |
down_buttonState = digitalRead(Down_buttonPin); | |
// compare the buttonState to its previous state | |
if (down_buttonState != down_lastButtonState) { | |
// if the state has changed, increment the counter | |
if (down_buttonState == LOW) { | |
bPress = true; | |
// if the current state is HIGH then the button went from off to on: | |
buttonPushCounter--; | |
Serial.println("on"); | |
Serial.print("number of button pushes: "); | |
Serial.println(buttonPushCounter); | |
} else { | |
// if the current state is LOW then the button went from on to off: | |
Serial.println("off"); | |
} | |
// Delay a little bit to avoid bouncing | |
delay(50); | |
} | |
// save the current state as the last state, for next time through the loop | |
down_lastButtonState = down_buttonState; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment