I wrote a program to calculate the steps between two values to output a total of 6 values for a calibration of solutions. Somewhere in my Code is a bug that resets the value of the variable back to zero, because the output of the 6 values always changes to 0.
//SDA to Pin A4
//SCL to Pin A5
#include <Keypad.h>
#include <LCD_I2C.h>
//_Declaration_
//Display
LCD_I2C lcd(0x27, 16, 2);
//Keypad
const int ROW_NUM = 4; //four rows
const int COLUMN_NUM = 4; //three columns
char keys[ROW_NUM][COLUMN_NUM] = {
{'1', '2', '3', 'A'},
{'4', '5', '6' ,'B'},
{'7', '8', '9' ,'C'},
{'*', '0', '#', '.'}
};
byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
//*Variables
//Keypad
String inputString; //Working memory
long inputInt; //long term memory
byte whileloop = 0; //to keep while loops running
//User input number pad
float lowConc; // Memory of the lowest Concentration
float hiConc; // Memory f the highest Concentration
// Calculated distances between lowConc and highConc to make 6 Meassurements
float Abs =(hiConc-lowConc)/5;
//Concentration of the Calibrationsolutions
float Conc1 = lowConc;
float Conc2 = lowConc+Abs;
float Conc3 = Conc2+Abs;
float Conc4 = Conc3+Abs;
float Conc5 = Conc4+Abs;
float Conc6 = Conc5+Abs;
//Textbloecke
String low = "lowest";
String hi = "highest";
void setup() {
lcd.begin();
lcd.backlight();
inputString.reserve(10); // maximum number of digit for a number is 10, change if needed
}
//Programming blocks
void dcd (int a, int b) //Code block to delay, clear display and delay again
{
delay(a);
lcd.clear();
delay(b);
}
void printSolution () //Ausgabe von Text um die berechneten Konzentrationen anzuzeigen
{
lcd.setCursor(0, 0);
lcd.print("Concentration of");
lcd.setCursor(0, 1);
lcd.print("the solutions:");
dcd(4000, 500);
lcd.print(Conc1);
dcd(2000, 400);
lcd.print(Conc2);
dcd(2000,400);
lcd.print(Conc3);
dcd(2000, 400);
lcd.print(Conc4);
dcd(2000,400);
lcd.print(Conc5);
dcd(2000, 400);
lcd.print(Conc6);
dcd(2000,400);
}
void cursor0 ()
{
lcd.setCursor(0, 0);
}
void loop() {
// printSolution ();
// printinputConc (low/hi);
Insert_lowest_Concentration:
printinputConc(low); //"insert lowest concentration"
// keypadInput(lowConc); //input w keypad
while (lowConc == 0) {
char key = keypad.getKey();
if (key) {
lcd.print(key);
if (key >= '0' && key <= '9') { // only act on numeric keys
inputString += key; // append new character to input string
lcd.clear();
cursor0();
lcd.print(inputString);
}
else if (key == '#')
{
if (inputString.length() > 0)
{
lowConc = inputString.toInt();
lcd.clear(); // clear screen
inputString = "";
// break; //manually breaks the while loop. Quick fix for failure of a = inputInt if inputInt is used after that
}
}
else if (key == '*')
{
inputString = ""; // clear input
lcd.clear(); // clear screen
}
else if (key == '.')
{
inputString += key; // append new character to input string
lcd.clear();
cursor0();
lcd.print(inputString);
}
}
}
// void Confirmlow () "[lowConc] Confirm?" {
lcd.clear();
cursor0();
lcd.print(lowConc);
lcd.setCursor(0, 1);
lcd.print("Confirm?");
while (whileloop == 0) {
char key = keypad.getKey();
if (key) {
if (key == '#') {
break;
}
else if (key == '*') {
// inputString = "";
// // inputInt = inputString.toInt(); // clear input
// lowConc = inputString.toInt();
// // a = inputInt; //clear variable
lcd.clear(); // clear screen
goto Insert_lowest_Concentration;
}
}
}
Insert_highest_Concentration:
printinputConc(hi); //"insert highest concentration"
// keypadInput(hiConc); //input w keypad
while (hiConc == 0) {
char key = keypad.getKey();
if (key) {
lcd.print(key);
if (key >= '0' && key <= '9') { // only act on numeric keys
inputString += key; // append new character to input string
lcd.clear();
cursor0();
lcd.print(inputString);
}
else if (key == '#')
{
if (inputString.length() > 0)
{
//inputInt = inputString.toInt(); // YOU GOT AN INTEGER NUMBER
//a = inputInt; // should save the number in inputInt as either lowConc or hiConc. !!!!!! This Line has to work
hiConc = inputString.toInt();
lcd.clear(); // clear screen
inputString = "";
// break; //manually breaks the while loop. Quick fix for failure of a = inputInt if inputInt is used after that
}
}
else if (key == '*')
{
inputString = ""; // clear input
lcd.clear(); // clear screen
}
else if (key == '.')
{
inputString += key; // append new character to input string
lcd.clear();
cursor0();
lcd.print(inputString);
}
}
}
// Confirmhi(); // "[hiConc] Confirm?"
lcd.clear();
cursor0();
lcd.print(hiConc);
lcd.setCursor(0, 1);
lcd.print("Confirm?");
while (whileloop == 0) {
char key = keypad.getKey();
if (key) {
if (key == '#') {
break;
}
else if (key == '*') {
// inputString = "";
// // inputInt = inputString.toInt(); // clear input
// hiConc = inputString.toInt();
// // a = inputInt; //clear variable
lcd.clear(); // clear screen
goto Insert_highest_Concentration;
}
}
}
printSolution(); //"[Conc1]","[Conc1]","[Conc1]","[Conc1]","[Conc1]","[Conc1]"
// startcon() //Start filling
lcd.clear();
cursor0();
lcd.print("Start filling?");
while (whileloop == 0) {
char key = keypad.getKey();
if (key) {
if (key == '#') {
break;
}
else if (key == '*') {
inputString = "";
// inputInt = inputString.toInt(); // clear input
lowConc = inputString.toInt();
// a = inputInt; //clear variable
lcd.clear(); // clear screen
goto Insert_lowest_Concentration;
}
}
}
}
The value doesn´t change between the input and the Confirmation output as this displays it correctly. The Problem isn´t in the Calculations either or at least that shouldn´t be it because ot displays the 6 calculated values correctly when I manually put the right numbers for the variables in the script. I can´t find a command either which resets it.
I completely rewritten your code because it was a bit rough. You must avoid the Goto and you have to create separate functions for the coded pieces several times used.
This makes the code readable, understandable and easy to debug.
More importantly I use the Tofloat () function to convert typed strings into Float number, which should solve your problem.
The code compiles well but I cannot guarantee that it works the first time. it need some improvments to check input value and so on.
hope it help !
//SDA to Pin A4
//SCL to Pin A5
#include <Keypad.h>
#include <LCD_I2C.h>
//_Declaration_
//Display
LCD_I2C lcd(0x27, 16, 2);
//Keypad
const int ROW_NUM = 4; //four rows
const int COLUMN_NUM = 4; //three columns
char keys[ROW_NUM][COLUMN_NUM] = {
{'1', '2', '3', 'A'},
{'4', '5', '6' , 'B'},
{'7', '8', '9' , 'C'},
{'*', '0', '#', '.'}
};
byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
//*Variables
//Keypad
String inputString; //Working memory
long inputInt; //long term memory
//User input number pad
float lowConc; // Memory of the lowest Concentration
float hiConc; // Memory f the highest Concentration
// Calculated distances between lowConc and highConc to make 6 Meassurements
float Abs = (hiConc - lowConc) / 5;
//Concentration of the Calibrationsolutions
float Conc1 = lowConc;
float Conc2 = lowConc + Abs;
float Conc3 = Conc2 + Abs;
float Conc4 = Conc3 + Abs;
float Conc5 = Conc4 + Abs;
float Conc6 = Conc5 + Abs;
//Textbloecke
String low = "lowest";
String hi = "highest";
void setup() {
lcd.begin();
lcd.backlight();
inputString.reserve(10); // maximum number of digit for a number is 10, change if needed
}
void loop() {
Insert_lowest_Concentration(); //get low conc until '#'
Insert_highest_Concentration(); //get high conc until '#'
printSolution(); //print result and raz low conc
}
void Insert_lowest_Concentration() {
printinputConc(low); //"insert lowest concentration"
lowConc = readInput();
// void Confirmlow () "[lowConc] Confirm?" {
lcd.clear();
cursor0();
lcd.print(lowConc);
lcd.setCursor(0, 1);
lcd.print("Confirm?");
if (confirm()) Insert_lowest_Concentration();
}
void Insert_highest_Concentration() {
printinputConc(hi); //"insert highest concentration"
hiConc = readInput(); //get hiConc
// Confirmhi(); // "[hiConc] Confirm?"
lcd.clear();
cursor0();
lcd.print(hiConc);
lcd.setCursor(0, 1);
lcd.print("Confirm?");
if (confirm()) Insert_highest_Concentration();
}
void printSolution () //Ausgabe von Text um die berechneten Konzentrationen anzuzeigen
{
lcd.setCursor(0, 0);
lcd.print("Concentration of");
lcd.setCursor(0, 1);
lcd.print("the solutions:");
dcd(4000, 500);
lcd.print(Conc1);
dcd(2000, 400);
lcd.print(Conc2);
dcd(2000, 400);
lcd.print(Conc3);
dcd(2000, 400);
lcd.print(Conc4);
dcd(2000, 400);
lcd.print(Conc5);
dcd(2000, 400);
lcd.print(Conc6);
dcd(2000, 400);
// startcon() //Start filling
lcd.clear();
cursor0();
lcd.print("Start filling?");
if (confirm()) Insert_lowest_Concentration();
}
float readInput() {
String typedChars;
while (true) {
char key = keypad.getKey();
if (key) {
lcd.print(key);
if (key >= '0' && key <= '9') { // only act on numeric keys
typedChars += key; // append new character to input string
lcd.clear();
cursor0();
lcd.print(typedChars);
}
else if (key == '#')
{
if (typedChars.length() > 0)
{
lcd.clear(); // clear screen
return typedChars.toFloat();
}
}
else if (key == '*')
{
typedChars = ""; // clear input
lcd.clear(); // clear screen
}
else if (key == '.')
{
typedChars += key; // append new character to input string
lcd.clear();
cursor0();
lcd.print(typedChars);
}
}
}
}
boolean confirm() {
while (true) {
char key = keypad.getKey();
if (key) {
if (key == '#') {
return false;
}
else if (key == '*') {
lcd.clear(); // clear screen
return true;
}
}
}
}
void printinputConc(String str) {
//do what you want
}
void cursor0 ()
{
lcd.setCursor(0, 0);
}
void dcd (int a, int b) //Code block to delay, clear display and delay again
{
delay(a);
lcd.clear();
delay(b);
}