Hi. I'm trying to store values from a char to a String, sometimes stores the values, sometimes doesn't store them.
In the char array always store the values.
In the code I'm increment ascii code and store it in a char array buffer by pressing up or down button and left or right for increment or decrement length of the char.
For each char array I want to store it in the String array.
#include <Wire.h>
//Grove-LCD RGB Backlight V4.0
#include "rgb_lcd.h"
rgb_lcd lcd;
uint32_t previousPushMillis = 0;
uint32_t previousMillis = 0;
uint32_t currentMillis = 0;
uint8_t menuCat = 0;
int16_t analogPin0 = A0;
int16_t buttonValue = 0; //Store value from the analog pin
bool selectButton = false;
bool upButton = false;
bool downButton = false;
bool leftButton = false;
bool rightButton = false;
bool pushedButton = false; //flag if a button was pressed or not
bool flagLCD = false;
uint16_t referenceIdCount = 0; //ID count for String array
uint8_t const referenceId = 200; //Max val for String array
String referenceMenu[referenceId]; //String array
char refMem[16]; // Temp char array to store values to a String
int8_t refMemCount = 0; //counter for char array
void setup() {
lcd.begin(16, 2);
lcd.clear();
Serial.begin(9600);
}
void loop() {
currentMillis = millis();
keypad();
menu();
}
void menu() {
switch (menuCat) {
case 0:
//increment String array
if (upButton == true && (currentMillis - previousMillis >= 50)) {
if (referenceIdCount < (referenceId - 1)) {
referenceIdCount++;
} else {
referenceIdCount = referenceId - 1;
}
flagLCD = false;
previousMillis = millis();
}
//decrement String array
if (downButton == true && (currentMillis - previousMillis >= 50)) {
if (referenceIdCount > 0) {
referenceIdCount--;
} else {
referenceIdCount = 0;
}
flagLCD = false;
previousMillis = millis();
}
//select the current String array number and go to the char array input
if (selectButton == true && (currentMillis - previousMillis >= 500)) {
menuCat = 10;
// Serial.println("Lenght: ");
// Serial.println(referenceMenu[referenceIdCount].length());
// verify if the current String array number is not empty and if not empty store the actual value to char array
if (referenceMenu[referenceIdCount].length() != 0) {
referenceMenu[referenceIdCount].toCharArray(refMem, 16);
refMemCount = 0;
}
flagLCD = false;
previousMillis = millis();
}
if (flagLCD == false) {
lcd.clear();
lcd.setCursor(12, 0);
lcd.print(referenceIdCount);
lcd.setCursor(0, 1);
lcd.print(referenceMenu[referenceIdCount]);
flagLCD = true;
}
break;
case 10:
//go to increment the char array
if (leftButton == true && (currentMillis - previousMillis >= 500)) {
menuCat = 11;
previousMillis = millis();
}
// go to decrement the char array
if (rightButton == true && (currentMillis - previousMillis >= 500)) {
menuCat = 12;
previousMillis = millis();
}
//go to incremet the ascii code for the selected char from array
if (upButton == true && (currentMillis - previousMillis >= 200)) {
menuCat = 13;
previousMillis = millis();
}
//go to decrement the ascii code for the selected char from array
if (downButton == true && (currentMillis - previousMillis >= 200)) {
menuCat = 14;
previousMillis = millis();
}
// go to store the values from char array to selected String
if (selectButton == true && (currentMillis - previousMillis >= 500)) {
menuCat = 15;
previousMillis = millis();
}
lcd.setCursor(0, 1);
lcd.print(refMem);
break;
//increment the char array
case 11:
if (refMemCount <= 0) {
refMemCount = 0;
} else {
refMemCount--;
}
menuCat = 10;
break;
//decrement the char array
case 12:
if (refMemCount >= 15) {
refMemCount = 15;
} else {
refMemCount++;
}
menuCat = 10;
break;
//incremet the ascii code for the selected char from array
case 13:
refMem[refMemCount]++;
if (refMem[refMemCount] >= 90) {
refMem[refMemCount] = 90;
} else if (refMem[refMemCount] > 57 && refMem[refMemCount] < 65) {
refMem[refMemCount] = 65;
} else if (refMem[refMemCount] > 32 && refMem[refMemCount] < 48) {
refMem[refMemCount] = 48;
} else if (refMem[refMemCount] < 32){
refMem[refMemCount] = 32;
}
menuCat = 10;
break;
//decrement the ascii code for the selected char from array
case 14:
refMem[refMemCount]--;
if (refMem[refMemCount] <= 32) {
refMem[refMemCount] = 32;
} else if (refMem[refMemCount] > 32 && refMem[refMemCount] < 48) {
refMem[refMemCount] = 32;
} else if (refMem[refMemCount] > 57 && refMem[refMemCount] < 65) {
refMem[refMemCount] = 57;
}
menuCat = 10;
break;
//store the values from char array to selected String
case 15:
lcd.noCursor();
String strtingRefMenu(refMem);
referenceMenu[referenceIdCount] = strtingRefMenu;
lcd.setCursor(0, 1);
// lcd.print(referenceMenu[referenceIdCount]);
// Serial.print("Ref Mem: ");
// Serial.println(refMem);
// Serial.print("Ref Menu String: ");
// Serial.println(strtingRefMenu);
// Serial.print("Ref ID: ");
// Serial.println(referenceIdCount);
Serial.print("Ref Menu: ");
Serial.println(referenceMenu[referenceIdCount]);
menuCat = 0;
refMemCount = 0;
break;
}
}
void keypad() {
buttonValue = analogRead(analogPin0);
if (buttonValue < 1000) {
delay(100);
buttonValue = analogRead(analogPin0);
}
switch (buttonValue / 100) {
case 7:
if (pushedButton == false && (currentMillis - previousPushMillis) >= 100) {
selectButton = true;
upButton = false;
downButton = false;
leftButton = false;
rightButton = false;
previousPushMillis = millis();
}
break;
case 1:
if (pushedButton == false && (currentMillis - previousPushMillis) >= 100) {
selectButton = false;
upButton = true;
downButton = false;
leftButton = false;
rightButton = false;
previousPushMillis = millis();
}
break;
case 3:
if (pushedButton == false && (currentMillis - previousPushMillis) >= 100) {
selectButton = false;
upButton = false;
downButton = true;
leftButton = false;
rightButton = false;
previousPushMillis = millis();
}
break;
case 0:
if (pushedButton == false && (currentMillis - previousPushMillis) >= 100) {
selectButton = false;
upButton = false;
downButton = false;
leftButton = true;
rightButton = false;
previousPushMillis = millis();
}
break;
case 5:
if (pushedButton == false && (currentMillis - previousPushMillis) >= 100) {
selectButton = false;
upButton = false;
downButton = false;
leftButton = false;
rightButton = true;
previousPushMillis = millis();
}
break;
case 10:
selectButton = false;
upButton = false;
downButton = false;
leftButton = false;
rightButton = false;
break;
}
}
I really need help for this. I was looking and looking over the code and I can't understand what is wrong.
Thanks.