Part 3

Follow-up of last post. The code has changed since then. Problem 1: Input not adding to totl
4 Replies
ur regular minecraftian
Code:
#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();}
else if (inputkey == 'D') {mode = 1; 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 = ""; mode = 0;}
}
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)); break;
case 1:
screen.print(priceinput);
screen.setCursor(0, 1);
screen.print("*:Enter #:Cancel"); break;
case 2:
screen.print("Do you wanna");
screen.setCursor(0,1);
screen.print("start over?");
delay(1000);
screen.clear();
screen.home();
screen.print("*:Yes #:No");
delay(1000); break;
}
Serial.println(total);
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();}
else if (inputkey == 'D') {mode = 1; 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 = ""; mode = 0;}
}
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)); break;
case 1:
screen.print(priceinput);
screen.setCursor(0, 1);
screen.print("*:Enter #:Cancel"); break;
case 2:
screen.print("Do you wanna");
screen.setCursor(0,1);
screen.print("start over?");
delay(1000);
screen.clear();
screen.home();
screen.print("*:Yes #:No");
delay(1000); break;
}
Serial.println(total);
delay(100);
}
Maybe @WeeGee could help me out? Or how b @PenPengu ?
WeeGee
WeeGee5mo ago
You don't need to make a new channel for each change set you make, and pinging for it isn't usually a thing You still haven't added break statements to all of your cases
ur regular minecraftian
Oh Oh silly me And also The reset inteface not working @WeeGee @WeeGee There's a problem with the start over interface! The * and # aint working
WeeGee
WeeGee5mo ago
Most of your problems are still to do with the missing breaks in your cases You don't want every case after the one that runs to run, so you need to explicitly put that in Here's a demo in C that you can run in the browser that shows the problem https://tio.run/##XctNDoIwEAXgdXuKBhe0IRr@F4KuPIabpoBMgsXQEheGs9eChjRdzcz75onjQwhjDiDFMDctqZVuYDz1VwxSkycHSRn5YNSNE10TIBcSV3bUpLAjiphFpN6gRU9hO5DgqiXxeV3Ra7bAJxrGIat2TDxMXEw9TF3MPMxczD3MXSw8LFwsPSz/2LQdnwe9q6LB7RcF28OCndJdbq0FL8Z8AQ
#include <stdio.h>
int main() {
for(int i = 0; i < 5; i++){
switch(i){
case 0:
putchar('0');
case 1:
putchar('1');
case 2:
putchar('2');
case 3:
putchar('3');
case 4:
putchar('4');
case 5:
putchar('5');
case 6:
putchar('6');
default:
puts("Default");
}
putchar('\n');
}
}
#include <stdio.h>
int main() {
for(int i = 0; i < 5; i++){
switch(i){
case 0:
putchar('0');
case 1:
putchar('1');
case 2:
putchar('2');
case 3:
putchar('3');
case 4:
putchar('4');
case 5:
putchar('5');
case 6:
putchar('6');
default:
puts("Default");
}
putchar('\n');
}
}
this outputs
0123456Default

123456Default

23456Default

3456Default

456Default
0123456Default

123456Default

23456Default

3456Default

456Default
if you want it to be what you're trying to do, you need to do this:
#include <stdio.h>
int main() {
for(int i = 0; i < 5; i++){
switch(i){
case 0:
putchar('0');
break;
case 1:
putchar('1');
break;
case 2:
putchar('2');
break;
case 3:
putchar('3');
break;
case 4:
putchar('4');
break;
case 5:
putchar('5');
break;
case 6:
putchar('6');
break;
default:
puts("Default");
}
putchar('\n');
}
}
#include <stdio.h>
int main() {
for(int i = 0; i < 5; i++){
switch(i){
case 0:
putchar('0');
break;
case 1:
putchar('1');
break;
case 2:
putchar('2');
break;
case 3:
putchar('3');
break;
case 4:
putchar('4');
break;
case 5:
putchar('5');
break;
case 6:
putchar('6');
break;
default:
puts("Default");
}
putchar('\n');
}
}
edit: changed indentation each case ending with a break this outputs
0
1
2
3
4
0
1
2
3
4
if you want something to reference on this, there's a decent tutorial on general control flow statements here: https://cplusplus.com/doc/tutorial/control/

Did you find this page helpful?