#include <string.h>
#include <math.h>
#include <Keypad.h>
#include <LiquidCrystal.h>
#include <EEPROM.h>
const float BETA = 3950;
char Lchar, Rchar;
String buffer;
float numstore[20];
int actionstore[20];
int x = 0;
float result;
int cursorx;
volatile bool deleting = 0;
volatile bool syntaxerror = 0;
int displayOn = 1;
int percent = 0;
unsigned long lastpush = 0;
volatile bool calculated = 0;
int storval, newstor;
float tempres;
LiquidCrystal lcd(11, 10, 9, 8, 7, 6);
const byte ROWS = 4;
const byte COLS = 3;
char leftKeypadMap[ROWS][COLS] = {
{'7', '8', '9'},
{'4', '5', '6'},
{'1', '2', '3'},
{'.', '0', '='}
};
byte LrowPins[ROWS] = {22, 24, 26, 28};
byte LcolPins[COLS] = {23, 25, 27};
Keypad Lkey = Keypad(makeKeymap(leftKeypadMap), LrowPins, LcolPins, ROWS, COLS);
char rightKeypadMap[ROWS][COLS] = {
{'+', 'M', 'm'},
{'-', 'r', 'c'},
{'x', 'n', '%'},
{'/', 's', '>'}
};
byte RrowPins[ROWS] = {32, 34, 36, 38};
byte RcolPins[COLS] = {33, 35, 37};
Keypad Rkey = Keypad(makeKeymap(rightKeypadMap), RrowPins, RcolPins, ROWS, COLS);
void EEPROMwrite(int startpos, const String &strToWrite)
{
byte length = strToWrite.length();
EEPROM.write(startpos, length);
for (int i = 0; i < length; i++)
{
EEPROM.write(startpos + 1 + i, strToWrite[i]);
}
}
String EEPROMread(int startpos)
{
int newStrLength = EEPROM.read(startpos);
char data[newStrLength + 1];
for (int i = 0; i < newStrLength; i++)
{
data[i] = EEPROM.read(startpos + 1 + i);
}
data[newStrLength] = '\0';
return String(data);
}
void startsplash(){
String splashtext;
lcd.setCursor(4,0);
byte bitcheck = EEPROM.read(0);
if(bitcheck == 255){
EEPROMwrite(0,"EEE20003");
}
splashtext = EEPROMread(64);
lcd.print(splashtext); //CHANGE GROUP 9 STRING TO CHANGEABLE using EEPROMread()
delay(2000);
lcd.clear();
password();
lcd.clear();
}
void password()
{
String passcode = EEPROMread(0);
String userin;
Serial.println("Please enter password");
Serial.flush();
while(Serial.available() < 1){}
while (Serial.available() > 0)
{
userin = Serial.readStringUntil('\n');
}
Serial.println(userin);
if(userin == passcode){
Serial.println("Correct");
mainmenu();
} else {
Serial.println("Incorrect, please try again");
password();
}
}
void mainmenu(){
Serial.println("\n\n\n");
Serial.println("--------- Group 9: Calculator ---------");
Serial.println(" Please select an option:");
Serial.println(" 1) Change splash text on display");
Serial.println(" 2) View add-on module stats");
Serial.println(" 3) Start calculator function");
String userin;
Serial.flush();
while(Serial.available() < 1){}
while (Serial.available() > 0)
{
userin = Serial.readStringUntil('\n');
}
Serial.println("");
Serial.print("Selection: ");
Serial.println(userin.toInt());
switch(userin.toInt()){
case 1:
changesplash();
break;
case 2:
Serial.print("Entering temperature mode");
delay(300);
Serial.print(".");
delay(300);
Serial.print(".");
delay(300);
Serial.print(".");
delay(150);
Serial.println("\n\n");
displayOn = 3;
break;
case 3:
Serial.print("Entering calulcator");
delay(300);
Serial.print(".");
delay(300);
Serial.print(".");
delay(300);
Serial.print(".");
delay(150);
Serial.println("\n\n");
displayOn = 2;
break;
default:
Serial.println("Invalid entry. Please re-enter");
mainmenu();
break;
}
}
void changesplash(){
Serial.println("Changing splash text:");
Serial.print("Please enter new splash text: ");
String splashchange;
Serial.flush();
while(Serial.available() == 0){}
while (Serial.available() > 0)
{
splashchange = Serial.readStringUntil("\n");
}
EEPROMwrite(64, splashchange);
Serial.println(splashchange);
Serial.print("Restarting");
delay(300);
Serial.print(".");
delay(300);
Serial.print(".");
delay(300);
Serial.print(".");
delay(150);
Serial.println("\n\n");
}
void calcoff(){
unsigned long currentpush = millis();
if(currentpush - lastpush > 200)
{
if(displayOn == 0)
{
displayOn = 1;
}else{
displayOn = 0;
}
lastpush = currentpush;
}
}
float calculations()
{
float numout, numstor;
numout = numstore[0];
switch(x){
case 1:
numout = numstore[0];
break;
case 2:
numout = numstore[0];
switch(actionstore[0]) {
case 1:
numstor = numout + numstore[1];
break;
case 2:
numstor = numout - numstore[1];
break;
case 3:
numstor = numout * numstore[1];
break;
case 4:
numstor = numout / numstore[1];
break;
case 5:
numstor = (numout * (numstore[1]/100));
percent = 0;
break;
}
numout = numstor;
break;
default:
for(int i = 0; i < x; i++)
{
switch(actionstore[i]) {
case 1:
numstor = numout + numstore[i+1];
break;
case 2:
numstor = numout - numstore[i+1];
break;
case 3:
numstor = numout * numstore[i+1];
break;
case 4:
numstor = numout / numstore[i+1];
break;
case 5:
numstor = (numout *(numstore[i+1]/100));
percent = 0;
break;
}
numout = numstor;
}
break;
}
return numout;
}
void setup() {
Serial.begin(9600);
lcd.begin(16,2);
attachInterrupt(digitalPinToInterrupt(21), calcoff, FALLING);
byte bitchecksplash = EEPROM.read(64);
if(bitchecksplash == 255){
EEPROMwrite(64,"Group 59");
}
startsplash();
}
void loop() {
switch(displayOn){
case 0:
lcd.noDisplay();
lcd.clear();
break;
case 1:
lcd.display();
startsplash();
break;
case 2:
Lchar = Lkey.getKey();
Rchar = Rkey.getKey();
if(Lchar != NO_KEY ){
lcd.print(Lchar);
if(deleting == 1){
deleting = 0;
}
if(isDigit(Lchar) == true || Lchar == '.'){
buffer = buffer + Lchar;
cursorx++;
if(calculated == 1){
lcd.clear();
lcd.print(Lchar);
for (int i = 0; i < x; i++){
numstore[i] = 0;
actionstore[i] = 0;
}
x = 0;
deleting = 0;
cursorx = 0;
calculated = !calculated;
}
}else if (Lchar == '=')
{
calculated = 1;
numstore[x] = buffer.toFloat();
buffer = "";
x++;
result = calculations();
if(percent == 1){
lcd.print(result);
lcd.print('%');
int lengthof = String(result).length();
cursorx = cursorx + 2 + lengthof;
}
else{
lcd.print(result);
int lengthof = String(result).length();
cursorx = cursorx + lengthof + 1;
}
}
}
else if(Rchar != NO_KEY){
if(deleting == 1 && Rchar != '>'){
deleting = 0;
}
if(Rchar == '>')
{
if(deleting == 0){
lcd.setCursor(cursorx-1, 0);
lcd.print("");
cursorx = cursorx - 1;
lcd.setCursor(cursorx, 0);
deleting = 1;
} else{
lcd.clear();
buffer = "";
for (int i = 0; i < x; i++)
{
numstore[i] = 0;
actionstore[i] = 0;
}
x = 0;
deleting = 0;
cursorx = 0;
calculated = !calculated;
}
}
if(Rchar == '+' || Rchar == '-' || Rchar == 'x' || Rchar == '/' || Rchar == '%')
{
lcd.print(Rchar);
cursorx++;
numstore[x] = buffer.toFloat();
if(Rchar == '+')
{
actionstore[x] = 1;
}
else if(Rchar == '-')
{
actionstore[x] = 2;
}
else if(Rchar == 'x')
{
actionstore[x] = 3;
}
else if(Rchar == '/')
{
actionstore[x] = 4;
}
else if(Rchar == '%')
{
actionstore[x] = 5;
}
buffer = "";
x++;
}
if(Rchar == 's')
{
lcd.clear();
lcd.print("sqrt(");
lcd.print(result);
lcd.print(") = ");
lcd.setCursor(2,1);
tempres = sqrt(result);
lcd.print(tempres);
result = tempres;
calculated = true;
}
if(Rchar == 'M' && calculated == true){
float currentmem = EEPROMread(128).toFloat();
float newmem = currentmem + result;
EEPROMwrite(128, String(newmem));
}
if(Rchar == 'm' && calculated == true){
float currentmem = EEPROMread(128).toFloat();
float newmem = currentmem - result;;
EEPROMwrite(128, String(newmem));
}
if(Rchar == 'r'){
float currentmem = EEPROMread(128).toFloat();
if(currentmem == 0){}
else{
lcd.print(currentmem);
buffer = buffer + currentmem;
cursorx = cursorx + String(currentmem).length();
}
if(calculated == 1){
lcd.clear();
lcd.print(buffer);
calculated = !calculated;
for (int i = 0; i < x; i++)
{
numstore[i] = 0;
actionstore[i] = 0;
}
x = 0;
deleting = 0;
cursorx = 0;
}
}
if(Rchar == 'c'){
EEPROMwrite(128, "");
}
if(Rchar == 'n'){
if(buffer.startsWith("-")){
Serial.println("NEG");
for(int i = buffer.length(); i > 0; i--){
lcd.setCursor(cursorx - i,0);
lcd.print("");
}
lcd.setCursor(cursorx - buffer.length(),0);
buffer = buffer.substring(1);
Serial.println(buffer);
lcd.print(buffer);
cursorx++;
}else{
Serial.println("POS");
buffer = '-' + buffer;
lcd.setCursor(cursorx - buffer.length() + 1,0);
lcd.print(buffer);
cursorx++;
}
}
}
break;
case 3:
int analogValue = analogRead(A0);
float celsius = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
lcd.setCursor(0,0);
lcd.print("Temperature: ");
lcd.setCursor(3,1);
lcd.print(celsius);
lcd.print(" C");
delay(500);
lcd.clear();
break;
}
}