So this is my code:
#include <LiquidCrystal_I2C.h>
#include <wire.h>
#include "Adafruit_MCP23008.h"
#include <cckeypad.h>
#include <randExec.h>
bool press; //bool for detecting buttons on Keypad
String AnswerForEq; // String that stores the correct Answers
char c[7] = {" "};
char answ[6] = {" "}; // Char Array that stores user input (answer)
char num[4][4] = { { '7', '8', '9', 'A' },
{ '4', '5', '6', 'B' },
{ '1', '2', '3', 'C' },
{ '.', '0', '-', 'D' } };
char z = 0;
int length1, length2, pos;
unsigned long Tstart;
bool test, ans;
String q = " ";
randExec randExec(A1);
cckeypad cckeypad(1, num);
LiquidCrystal_I2C lcd(0x27,20,4);
void setup() {
Serial.begin(9600);
Serial.println("START");
pinMode(13, OUTPUT);
lcd.init();
lcd.backlight();
lcd.clear();
delay(100);
cckeypad.Kbegin();
for(int i; i<6; i++){
answ[i]=' ';
}
}
void loop() {
getAns();
}
void clearAns(){
for(int i; i<6; i++){
answ[i]=' ';
}
clearA();
pos=0;
}
void getKeypad(){
cckeypad.Kcheck(&z);
if(z!=' '){
if(!press){
if(z=='D'){
pos=0;
press = true;
clearAns();
clearA();
}else if(z=='C'){
ans=1;
}else{
answ[pos] = z;
pos++;
if(pos>=6){pos=0;}
press = true;
lcd.setCursor(0, 3);
for(int i=0; i<6; i++){
if(answ[i]!=' '){
lcd.setCursor(i, 3);
lcd.print(answ[i]);
}
}}
delay(10);
}
}else{
press = false;
}
}
void Comp(){
float answerFloat = AnswerForEq.toFloat();
float answer2Float = String(answ).toFloat();
Serial.println(answer2Float);
if(answerFloat == answer2Float){
test=true;
}else{
test=false;
}
}
void ShowQuestion(){
lcd.setCursor(0, 0);
lcd.clear();
q = randExec.gen();
while(q.length()<=2){
q = randExec.gen();
}
lcd.print(q);
Serial.println(q);
}
void Rig(){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("RICHTIG");
delay(2000);
}
void Wro(){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("FALSCH");
delay(2000);
}
void clearA(){
for(int i; i < 20; i++){
lcd.setCursor(i, 3);
lcd.print(" ");
}
}
void getAns(){
ans=0;
ShowQuestion();
AnswerForEq=randExec.res();
Serial.println(AnswerForEq);
Tstart=millis()+10000;
while(ans==0){
getKeypad();
if(millis()>=Tstart){
break;
}
}
Comp();
if(test){
digitalWrite(13, HIGH);
Rig();
}else{
digitalWrite(13, LOW);
Wro();
}
clearAns();
}
the problem is that in the answ
char array I get 789 at the end and I don't know why. The libraries are linked below:
randExec.zip (1.1 KB)
cckeypad.zip (1.3 KB)