Part 2 of help!

Remember the thing of the string to double thing? I need help again! I made a few changes to my code For some reason the screen says: Type item price start over?nclDo
19 Replies
WeeGee
WeeGee5mo ago
This code? (deleted because long and not helpful) You'll need to say what's changed (or paste the code again) @MinecraftFan69420 You'll need to paste the code again to show us what you've changed (I'll delete my paste of it) done
ur regular minecraftian
Oh OK
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>

LiquidCrystal_I2C screen(0x27, 16, 2);

// Keypad info
const byte cols = 4, rows = 4;
char keys[rows][cols] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowpins[4] = {9, 8, 7, 6}, colpins[4] = {5, 4, 3, 2};
Keypad inputkeypad = Keypad(makeKeymap(keys), rowpins, colpins, rows, cols);
// Price info
double total = 0;
// Input
String priceinput = "";
byte mode = 0;

void setup() {
screen.init();
screen.backlight();
Serial.begin(9600);
}

void loop() {
screen.clear();
char inputkey = inputkeypad.getKey();
if (inputkey) {
if (inputkey == 'C') {mode = 2; screen.clear();}
if (inputkey == 'D') {priceinput += ".";}
else if (inputkey == '*') {
switch (mode) {
case 1: total += priceinput.toDouble(); priceinput = ""; mode = 0;
case 2: total = 0; mode = 0;
}
}
else if (inputkey == '#') {
if (mode == 1) {priceinput = "";}
}
else if (! (inputkey == 'A' || inputkey == 'B')) {mode = 1; priceinput += inputkey;}
}
switch (mode) {
case 0:
screen.home();
screen.print("Type item price");
screen.setCursor(0, 1);
screen.print(String("Total: P") + String(total));
case 1:
screen.print(priceinput);
screen.setCursor(0, 1);
screen.print("*:Entr #:Cancl");
case 2:
screen.print("Do you wanna");
screen.setCursor(0,1);
screen.print("start over?");
delay(1000);
screen.home();
screen.print("*:Yes #:No");
}
delay(100);
}
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>

LiquidCrystal_I2C screen(0x27, 16, 2);

// Keypad info
const byte cols = 4, rows = 4;
char keys[rows][cols] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowpins[4] = {9, 8, 7, 6}, colpins[4] = {5, 4, 3, 2};
Keypad inputkeypad = Keypad(makeKeymap(keys), rowpins, colpins, rows, cols);
// Price info
double total = 0;
// Input
String priceinput = "";
byte mode = 0;

void setup() {
screen.init();
screen.backlight();
Serial.begin(9600);
}

void loop() {
screen.clear();
char inputkey = inputkeypad.getKey();
if (inputkey) {
if (inputkey == 'C') {mode = 2; screen.clear();}
if (inputkey == 'D') {priceinput += ".";}
else if (inputkey == '*') {
switch (mode) {
case 1: total += priceinput.toDouble(); priceinput = ""; mode = 0;
case 2: total = 0; mode = 0;
}
}
else if (inputkey == '#') {
if (mode == 1) {priceinput = "";}
}
else if (! (inputkey == 'A' || inputkey == 'B')) {mode = 1; priceinput += inputkey;}
}
switch (mode) {
case 0:
screen.home();
screen.print("Type item price");
screen.setCursor(0, 1);
screen.print(String("Total: P") + String(total));
case 1:
screen.print(priceinput);
screen.setCursor(0, 1);
screen.print("*:Entr #:Cancl");
case 2:
screen.print("Do you wanna");
screen.setCursor(0,1);
screen.print("start over?");
delay(1000);
screen.home();
screen.print("*:Yes #:No");
}
delay(100);
}
@WeeGee
WeeGee
WeeGee5mo ago
if you add ino to right after the first ``` ticks, then it'll color code
ur regular minecraftian
Let's see
WeeGee
WeeGee5mo ago
like this ```ino (you put the code here) ```
ur regular minecraftian
Oh Does it work for other languages? First python
WeeGee
WeeGee5mo ago
yeah! py is python
ur regular minecraftian
print("Hello World')
print("Hello World')
Second javascript
console.log("Hello World")
console.log("Hello World")
OOH That's cool but can you help me first? @WeeGee ? Hello?
WeeGee
WeeGee5mo ago
trying to understand what you're saying happens when you run the code; your LCD shows
Type item price
start over?nclDo
Type item price
start over?nclDo
on the lines?
WeeGee
WeeGee5mo ago
and you expect something different, what do you expect to see?
ur regular minecraftian
So the first thing the home screen Shows
Type item price
Total: P(x)
Type item price
Total: P(x)
(the x represents the ttoal) Pressing C takes you to the screen where you clear all It says
Do you wanna
start over?
Do you wanna
start over?
Then after a second it says Wait BRB
WeeGee
WeeGee5mo ago
I think that the issue is that you're not overwriting the parts of the screen you expect to:
start over?ncl
start over?ncl
looks like you're overwriting two lines without a full clear:
*:Enrt #:Cancl
start over?
*:Enrt #:Cancl
start over?
note that this is similar to what you see, but with a
Do you wanna
Do you wanna
written after it
ur regular minecraftian
Then after a second it says
*:Yes #:No
*:Yes #:No
@WeeGee So how to fix? @WeeGee Hello?
WeeGee
WeeGee5mo ago
I've recreated this in Wokwi here and it does just as you show: https://wokwi.com/projects/429394747609747457
PriceTotaler - Wokwi ESP32, STM32, Arduino Simulator
Run IoT and embedded projects in your browser: ESP32, STM32, Arduino, Pi Pico, and more. No installation required!
WeeGee
WeeGee5mo ago
The problem is that you're not waiting for button input, your main loop is just running free You're printing things on top of each other because the main loop isn't ever "waiting" to see if the user presses anything
if (inputkey) {
if (inputkey == 'C') {mode = 2; screen.clear();}
if (inputkey == 'D') {priceinput += ".";}
else if (inputkey == '*') {
switch (mode) {
case 1: total += priceinput.toDouble(); priceinput = ""; mode = 0;
case 2: total = 0; mode = 0;
}
}
else if (inputkey == '#') {
if (mode == 1) {priceinput = "";}
}
else if (! (inputkey == 'A' || inputkey == 'B')) {mode = 1; priceinput += inputkey;}
}
if (inputkey) {
if (inputkey == 'C') {mode = 2; screen.clear();}
if (inputkey == 'D') {priceinput += ".";}
else if (inputkey == '*') {
switch (mode) {
case 1: total += priceinput.toDouble(); priceinput = ""; mode = 0;
case 2: total = 0; mode = 0;
}
}
else if (inputkey == '#') {
if (mode == 1) {priceinput = "";}
}
else if (! (inputkey == 'A' || inputkey == 'B')) {mode = 1; priceinput += inputkey;}
}
This if statement is not running when you don't press any keys, so the code (when no keys are pressed) looks like this:
void loop() {
screen.clear();
char inputkey = inputkeypad.getKey(); //this does nothing effectively
switch (mode) {
case 0:
screen.home();
screen.print("Type item price");
screen.setCursor(0, 1);
screen.print(String("Total: P") + String(total));
case 1:
screen.print(priceinput);
screen.setCursor(0, 1);
screen.print("*:Entr #:Cancl");
case 2:
screen.print("Do you wanna");
screen.setCursor(0,1);
screen.print("start over?");
delay(1000);
screen.home();
screen.print("*:Yes #:No");
}
delay(100);
}
void loop() {
screen.clear();
char inputkey = inputkeypad.getKey(); //this does nothing effectively
switch (mode) {
case 0:
screen.home();
screen.print("Type item price");
screen.setCursor(0, 1);
screen.print(String("Total: P") + String(total));
case 1:
screen.print(priceinput);
screen.setCursor(0, 1);
screen.print("*:Entr #:Cancl");
case 2:
screen.print("Do you wanna");
screen.setCursor(0,1);
screen.print("start over?");
delay(1000);
screen.home();
screen.print("*:Yes #:No");
}
delay(100);
}
The next issue is that you don't have any break statements in your switch-case so each of your cases is running one after the other, which means the code is effectively doing this:
void loop() {
screen.clear();
char inputkey = inputkeypad.getKey(); //this does nothing effectively
screen.home();
screen.print("Type item price");
screen.setCursor(0, 1);
screen.print(String("Total: P") + String(total));
screen.print(priceinput);
screen.setCursor(0, 1);
screen.print("*:Entr #:Cancl");
screen.print("Do you wanna");
screen.setCursor(0,1);
screen.print("start over?");
delay(1000);
screen.home();
screen.print("*:Yes #:No");
delay(100);
}
void loop() {
screen.clear();
char inputkey = inputkeypad.getKey(); //this does nothing effectively
screen.home();
screen.print("Type item price");
screen.setCursor(0, 1);
screen.print(String("Total: P") + String(total));
screen.print(priceinput);
screen.setCursor(0, 1);
screen.print("*:Entr #:Cancl");
screen.print("Do you wanna");
screen.setCursor(0,1);
screen.print("start over?");
delay(1000);
screen.home();
screen.print("*:Yes #:No");
delay(100);
}
and that's not really what you want (each case is not a separate block; it's just a label that the program "jumps" to depending on what the argument to the switch statement evaluated to) so I'd recommend - making the program wait for input (you can do this by either checking each loop and doing "nothing" if no key is pressed, which would probably look like pulling your switch-case into the if statement for if(inputkey)) - adding breaks to your cases so that the program doesn't fall-through as is happening now A sidenote, you're still using doubles to represent dollars and cents; this is a bad idea! things will do weird things if you do this. Don't! You're also using Strings which is OK, but can be an issue on constrained systems like arduino @MinecraftFan69420 does this make sense as why your code isn't doing what you expected, and now why your code is doing what it's doing?
ur regular minecraftian
Gotcha
WeeGee
WeeGee5mo ago
You'll probably want to read up on switch-case
ur regular minecraftian
@WeeGee Yet another problem Whenever I press C it's supposed to set mode to 2, but it's 1. Why? Didn't change the code @WeeGee ello

Did you find this page helpful?