hi, first real Teensy project.
Im building a simple calculator (just adds up numbers really) for my darts scoreboard using teensy 3.1
But having difficulty with correctly adding in the third line eg. 40+20=60 works, but 40+20+20 doesn't yet.
it displays on an lcd display (40x4) like this:
Can anybody show me where I am going wrong?
Original base code sourced from here https://maker.pro/arduino/projects/how-to-create-an-arduino-controlled-calculator
Im building a simple calculator (just adds up numbers really) for my darts scoreboard using teensy 3.1
But having difficulty with correctly adding in the third line eg. 40+20=60 works, but 40+20+20 doesn't yet.
it displays on an lcd display (40x4) like this:
Can anybody show me where I am going wrong?
Code:
#include<Keypad.h>
#include<LiquidCrystal_I2C.h>
#include<Wire.h>
//#include <Adafruit_GFX.h>
#include "Adafruit_LEDBackpack.h"
Adafruit_7segment matrix = Adafruit_7segment();
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', '+'},
{'4', '5', '6', '='},
{'7', '8', '9', 'C'},
{'+', '0', '=', }
};
byte rowPins[ROWS] = {1, 2, 3, 4};
byte colPins[COLS] = {5, 6, 7, 8};
// this section not completed yet : using momentary buttons w/10k pullup resistors
//const byte switchPin = 15; // player one 'ENTER' button -deducts score from LED display 0x71.
//const byte switchPin = 17; // player two 'ENTER' button -deducts score from LED display 0x72.
//
Keypad myKeypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
LiquidCrystal_I2C lcd(0x27, 20, 4);
boolean firstNumState = true;
boolean secondNumState = true;
boolean thirdNumState = true;
String firstNum = "";
String secondNum = "";
String thirdNum = "";
int result = 0;
char operatr = ' ';
void setup() {
lcd.begin();
lcd.setCursor(0, 0);
lcd.print("Darts Scoreboard");
lcd.setCursor(0, 1);
lcd.print("v1.0");
delay(1000);
// scrollDisplay();
clr();
{
#ifndef __AVR_ATtiny85__
Serial.begin(9600);
//Serial.println("0");
delay(600);
#endif
matrix.begin(0x70);
matrix.setBrightness(3);
matrix.print(0x0, HEX);
matrix.writeDisplay();
delay(600);
matrix.begin(0x71);
matrix.setBrightness(3);
matrix.print(0x501, HEX);
matrix.writeDisplay();
delay(800);
matrix.begin(0x72);
matrix.setBrightness(3);
matrix.print(0x501, HEX);
matrix.writeDisplay();
delay(800);
}
}
void loop() {
char newKey = myKeypad.getKey();
if (newKey != NO_KEY && (newKey == '1' || newKey == '2' || newKey == '3' || newKey == '4' || newKey == '5' || newKey == '6' || newKey == '7' || newKey == '8' || newKey == '9' || newKey == '0')) {
if (firstNumState == true) {
firstNum = firstNum + newKey;
lcd.print(newKey);
}
else {
secondNum = secondNum + newKey;
lcd.print(newKey);
}
}
if (newKey != NO_KEY && (newKey == '+' )) {
if (secondNumState == true) {
operatr = newKey;
firstNumState = true; secondNumState = false;
lcd.setCursor(8, 0);
lcd.print(operatr);
lcd.setCursor(5, 1);
}
}
/*if (newKey != NO_KEY && (newKey == '+' )) {
if (thirdNumState == true) {
operatr = newKey;
firstNumState = false; secondNumState = true; thirdNumState = false;
//lcd.setCursor(5, 1);
lcd.print(secondNum);
lcd.setCursor(7, 1);
lcd.print(operatr);
lcd.setCursor(5, 1);
}
}*/
if (newKey != NO_KEY && (newKey == '+' )) {
if (thirdNumState == true) {
operatr = newKey;
firstNumState = false; secondNumState = false; thirdNumState = true;
lcd.setCursor(5, 1);
lcd.print(thirdNum);
lcd.setCursor(8, 1);
lcd.print(operatr);
lcd.setCursor(5, 1);
}
}
if (newKey != NO_KEY && newKey == '=') {
if (operatr == '+') {
result = firstNum.toInt() + secondNum.toInt() + thirdNum.toInt();
}
//
//lcd.clear();
lcd.setCursor(5, 0);
lcd.print(firstNum);
lcd.setCursor(8, 0);
lcd.print(operatr);
lcd.setCursor(5, 1);
lcd.print(secondNum);
lcd.setCursor(8, 1);
lcd.print(operatr);
lcd.setCursor(5, 2);
lcd.setCursor(8, 2);
lcd.print(thirdNum);
lcd.setCursor(8, 2);
//lcd.print("=");
lcd.setCursor(5, 3);
lcd.print(result);
//matrix.print(result);
matrix.begin(0x70);
matrix.setBrightness(3);
matrix.print(result,10); //prints total score displayed on calculator to LED display 0x70
matrix.writeDisplay();
delay(500);
firstNumState = true; secondNumState = true; thirdNumState = true;
}
if (newKey != NO_KEY && newKey == 'C') {
clr();
}
}
void clr() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("1st: ");
lcd.setCursor(0, 1);
lcd.print("2nd: ");
lcd.setCursor(0, 2);
lcd.print("3rd: ");
lcd.setCursor(5, 0);
firstNum = "";
lcd.setCursor(5, 1);
secondNum = "";
lcd.setCursor(5, 2);
thirdNum = "";
lcd.setCursor(5, 0);
//result = 0;
//operatr = ' ';
lcd.setCursor(10, 0);
lcd.print("Leg: ");
lcd.setCursor(10, 1);
lcd.print("Ave: ");
lcd.setCursor(10, 2);
lcd.print("Leg: ");
lcd.setCursor(10, 3);
lcd.print("Ave: ");
lcd.setCursor(5, 0);
firstNum = "";
lcd.setCursor(5, 1);
secondNum = "";
lcd.setCursor(5, 2);
thirdNum = "";
lcd.setCursor(0,3 );
lcd.print("Scr: ");
lcd.setCursor(5, 3);
result = 0;
lcd.setCursor(5,0 );
}
Original base code sourced from here https://maker.pro/arduino/projects/how-to-create-an-arduino-controlled-calculator
Last edited: