HI,
I try to make a darts scoreboard with arduino.
My LCD keeps flickering and doesn't always update right.
Anyone knows how to prevent it?
Thanks!
this is the code:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <TM1637Display.h>
#include <IRremote.h>
const int lcdColumns = 16; // number of columns on the LCD display
const int lcdRows = 2; // number of rows on the LCD display
const int irReceiverPin = 8; // pin for the IR receiver
IRrecv irReceiver(irReceiverPin); // create an instance of the IRrecv class
decode_results results; // create an instance of the decode_results class
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows); // initialize the LCD
TM1637Display display1(13, 12); // initialize the first four-digit display
TM1637Display display2(10, 9); // initialize the second four-digit display
const byte ROWS = 4; // number of rows on the keypad
const byte COLS = 4; // number of columns on the keypad
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {7, 6, 5, 4}; // connect keypad rows to these Arduino pins
byte colPins[COLS] = {3, 2, 1, 0}; // connect keypad columns to these Arduino pins
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
int score1 = 501; // starting score for player 1
int score2 = 501; // starting score for player 2
int turns1 = 0; // number of turns taken by player 1
int turns2 = 0; // number of turns taken by player 2
int totalscore1 = 0;
int totalscore2 = 0;
bool player1Turn = true; // flag to track whose turn it is
void setup() {
lcd.init(); // initialize the LCD
lcd.backlight(); // turn on the backlight
lcd.clear(); // clear the LCD
lcd.setCursor(0, 0); // move cursor to (0, 0)
lcd.print("Player 1: 501"); // print starting score for player 1
lcd.setCursor(0, 1); // move cursor to (0, 1)
lcd.print("Player 2: 501"); // print starting score for player 2
display1.setBrightness(0x0f); // set brightness of the first four-digit display
display1.showNumberDec(501, false, 4, 0); // show starting score for player 1 on the first four-digit display
display2.setBrightness(0x0f); // set brightness of the second four-digit display
display2.showNumberDec(501, false, 4, 0); // show starting score for player 2 on the second four-digit display
irReceiver.enableIRIn(); // start the IR receiver
}
void loop() {
char key = keypad.getKey(); // get the key that was pressed
if (key) { // if a key was pressed
if (key == '#') { // if the key is the '#' key
// toggle the turn flag
if (player1Turn) {
totalscore1 = 501 - score1;
turns1++;
} else {
totalscore2 = 501 - score2;
turns2++;
}
player1Turn = !player1Turn;
} else if (key == 'A') { // if the key is the 'A' key
// subtract 50 points from the current player's score
if (player1Turn) {
score1 -= 50;
} else {
score2 -= 50;
}
} else if (key == 'B') { // if the key is the 'B' key
// subtract 25 points from the current player's score
if (player1Turn) {
score1 -= 25;
} else {
score2 -= 25;
}
} else if (key == 'C') { // if the key is the 'C' key
// subtract 20 points from the current player's score
if (player1Turn) {
score1 -= 20;
} else {
score2 -= 20;
}
} else if (key == 'D') { // if the key is the 'D' key
// subtract 10 points from the current player's score
if (player1Turn) {
score1 -= 10;
} else {
score2 -= 10;
}
} else { // if the key is a number key
// subtract the value of the key from the current player's score
int value = key - '0';
if (player1Turn) {
score1 -= value;
} else {
score2 -= value;
}
}
}
if (irReceiver.decode(&results)) { // if the receiver has received a signal
switch (results.value) {
case 0xFF6897: // code for button "1"
if (player1Turn) {
score1 -= 1;
} else {
score2 -= 1;
}
break;
case 0xFF9867: // code for button "2"
if (player1Turn) {
score1 -= 2;
} else {
score2 -= 2;
}
break;
case 0xFFB04F: // code for button "3"
if (player1Turn) {
score1 -= 3;
} else {
score2 -= 3;
}
break;
case 0xFF30CF: // code for button "4"
if (player1Turn) {
score1 -= 4;
} else {
score2 -= 4;
}
break;
case 0xFF18E7: // code for button "5"
if (player1Turn) {
score1 -= 5;
} else {
score2 -= 5;
}
break;
case 0xFF7A85: // code for button "6"
if (player1Turn) {
score1 -= 6;
} else {
score2 -= 6;
}
break;
case 0xFF10EF: // code for button "7"
if (player1Turn) {
score1 -= 7;
} else {
score2 -= 7;
}
break;
case 0xFF38C7: // code for button "8"
if (player1Turn) {
score1 -= 8;
} else {
score2 -= 8;
}
break;
case 0xFF5AA5: // code for button "9"
if (player1Turn) {
score1 -= 9;
} else {
score2 -= 9;
}
break;
case 0xFF52AD: // code for button "#"
// toggle the turn flag
if (player1Turn) {
totalscore1 = 501 - score1;
turns1++;
} else {
totalscore2 = 501 - score2;
turns2++;
}
player1Turn = !player1Turn;
break;
case 0xFF629D: // code for button "UP"
if (player1Turn) {
score1 -= 10;
} else {
score2 -= 10;
}
break;
case 0xFFC23D: // code for button "RIGHT"
if (player1Turn) {
score1 -= 20;
} else {
score2 -= 20;
}
break;
case 0xFFA857: // code for button "DOWN"
if (player1Turn) {
score1 -= 25;
} else {
score2 -= 25;
}
break;
case 0xFF22DD: // code for button "LEFT"
if (player1Turn) {
score1 -= 50;
} else {
score2 -= 50;
}
break;
default:
break;
}
irReceiver.resume(); // resume receiver
}
// update the displays with the new scores and average scores
lcd.clear(); // clear the LCD display
lcd.setCursor(0, 0); // move cursor to (0, 0)
lcd.print("P1: "); // print label for player 1
lcd.print(score1); // print player 1's updated score
lcd.print(" "); // print space
// print average score for player 1
if (turns1 > 0) {
lcd.print("Avg: ");
lcd.print(totalscore1 / turns1);
} else {
lcd.print("Avg: N/A");
}
lcd.setCursor(0, 1); // move cursor to (0, 1)
lcd.print("P2: "); // print label for player 2
lcd.print(score2); // print player 2's updated score
lcd.print(" "); // print space
// print average score for player 2
if (turns2 > 0) {
lcd.print("Avg: ");
lcd.print(totalscore2 / turns2);
} else {
lcd.print("Avg: N/A");
}
display1.showNumberDec(score1, false, 4, 0); // update the first four-digit display
display2.showNumberDec(score2, false, 4, 0); // update the second four-digit display
if (player1Turn) {
digitalWrite(11, HIGH);
}
else {
digitalWrite(11, LOW);
}
}