I am trying to write UI for an LCD screen to assign a 4digit code to a series of sensors (chara, charb, etc, to sensor a, sensor b, etc). When i enter an IF statement, i want the loop to be confined there so that the buttons that are called upon there are not double tasked elsewhere in the code. As it is now, if i want to reassign a button, it may impact a button press count from earlier or later in the code, throwing everything off. I thought i had a handle on it but it has fallen apart a bit.
Anyway, the way the program works is using the buttons on an AdaFruit RGB LCD shield, which has debouncing as part of the library. There is a 4 way panel plus a select button.
I think that i want to use a While() statement, but whenever i do the program monitor gets stuck just above the if statements for the buttons, evidenced by tests with the serial monitor.
the controller is an UNO
thanks
#include <Wire.h>
#include <Adafruit_MCP23017.h>
#include <Adafruit_RGBLCDShield.h>
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
#include <EEPROM.h>
#define RED 0x1
#define YELLOW 0x3
#define GREEN 0x2
#define TEAL 0x6
#define BLUE 0x4
#define VIOLET 0x5
#define WHITE 0x7
int butCnt = 0;
int chara1 = 0;
int chara2 = 0;
int chara3 = 0;
int chara4 = 0;
int charaUM = 55;
int charuct_b;
int charb1 = 0;
int charb2 = 0;
int charb3 = 0;
int charb4 = 0;
int charbUM = 55;
int cursCnt = 0;
int display1=0;
int butselect=0;
int chartot=0; //sets total number of charucts
void setup() {
Serial.begin (115200);
lcd.begin (16, 2);
lcd.println("To begin, press ");
lcd.setCursor(2, 1);
lcd.println("SELECT or <- ");
uint8_t i = 0;
}
void charaset() {
// put your main code here, to run repeatedly:
uint8_t buttons = lcd.readButtons();
if (buttons) {
if (buttons & BUTTON_SELECT) {
butselect++;
delay(650);
lcd.clear();
lcd.print("charUCT:");
lcd.setCursor(9, 1);
lcd.print(chara1);
lcd.setCursor(10, 1);
lcd.print(chara2);
lcd.setCursor(11, 1);
lcd.print(chara3);
lcd.setCursor(12, 1);
lcd.print(chara4);
Serial.print("button select");
Serial.println(butselect);
}
}
if (butselect==1) //start of charuct entry
{
if (buttons)
{
if (buttons & BUTTON_RIGHT) {
cursCnt++;
delay(500);
Serial.print("curscount");
Serial.println(cursCnt);
lcd.clear();
lcd.print("charUCT:");
lcd.setCursor(9, 1);
lcd.print(chara1);
lcd.setCursor(10, 1);
lcd.print(chara2);
lcd.setCursor(11, 1);
lcd.print(chara3);
lcd.setCursor(12, 1);
lcd.print(chara4);
}
if (buttons & BUTTON_LEFT) {
cursCnt--;
delay(500);
Serial.print("curs count");
Serial.println(cursCnt);
lcd.clear();
lcd.print("charUCT:");
lcd.setCursor(9, 1);
lcd.print(chara1);
lcd.setCursor(10, 1);
lcd.print(chara2);
lcd.setCursor(11, 1);
lcd.print(chara3);
lcd.setCursor(12, 1);
lcd.print(chara4);
}
if (cursCnt == 0) {
lcd.setCursor(9, 0);
lcd.print("_");
if (buttons & BUTTON_UP) {
chara1++;
Serial.print("chara1 cnt ");
Serial.println(chara1);
delay(500);
lcd.setCursor(9, 1);
lcd.print(chara1);
}
if (buttons & BUTTON_DOWN) {
chara1--;
delay(500);
lcd.setCursor(9, 1);
lcd.print(chara1);
}
}
if (cursCnt == 1) {
lcd.setCursor(10, 0);
lcd.print("_");
if (buttons & BUTTON_UP) {
chara2++;
delay(500);
lcd.setCursor(10, 1);
lcd.print(chara2);
}
if (buttons & BUTTON_DOWN) {
chara2--;
delay(500);
lcd.setCursor(10, 1);
lcd.print(chara2);
}
}
if (cursCnt == 2) {
lcd.setCursor(11, 0);
lcd.print("_");
if (buttons & BUTTON_UP) {
chara3++;
delay(500);
lcd.setCursor(11, 1);
lcd.print(chara3);
}
if (buttons & BUTTON_DOWN) {
chara3--;
delay(500);
lcd.setCursor(11, 1);
lcd.print(chara3);
}
}
if (cursCnt == 3) {
lcd.setCursor(12, 0);
lcd.print("_");
if (buttons & BUTTON_UP) {
chara4++;
delay(500);
lcd.setCursor(12, 1);
lcd.print(chara4);
}
if (buttons & BUTTON_DOWN) {
chara4--;
delay(500);
lcd.setCursor(12, 1);
lcd.print(chara4);
}
}
if (cursCnt == 4) {
lcd.clear();
lcd.print("CONFIRM A: ");
lcd.print (chara1);
lcd.print (chara2);
lcd.print (chara3);
lcd.print (chara4);
lcd.setCursor(0, 1);
lcd.print("NO<- YES->");
}
//if no, then cursor moves left to numbers. this is not sophisticated, but it works.
if (cursCnt==5){
chartot++;
lcd.clear();
lcd.print("char1 wrtn EEPROM"); //write EEPROM code, this line will be written when everything else works.
delay(1500);
lcd.clear();
lcd.print("Add sensor?");
lcd.setCursor(0,1);
lcd.print("Yes-> Done(SEL)");
if (buttons & BUTTON_SELECT) //at this point the select-button count falls apart, and it is not deducted or set= to whatever number i chose, since that button is used earlier
{
butselect--;
butselect--;
Serial.println("button select ");
Serial.println(butselect);
}
}
}
}
}
void loop () {
uint8_t buttons = lcd.readButtons();
charaset();
if (buttons)
{
if ((buttons & BUTTON_LEFT) || (butselect %2==0)) {
lcd.clear();
lcd.print ("eeprom read");
lcd.setCursor(0, 1);
lcd.print("95%rem");
lcd.setCursor(9, 1);
lcd.print(chara1);
lcd.setCursor(10, 1);
lcd.print(chara2);
lcd.setCursor(11, 1);
lcd.print(chara3);
lcd.setCursor(12, 1);
lcd.print(chara4);
delay(1500);
lcd.clear();
lcd.print("would show next");
delay(500);
}
}
}