I have build a display navigation menu.
I can control the display navigation postion with the two logical states brakeVal and throttleVal.
if (throttlebrakeValues.brake >= 160){
brakeVal = 1;
}
else if (throttlebrakeValues.brake <= 50){
brakeVal = -1;
}
else {
brakeVal = 0;
}
if (throttlebrakeValues.throttle >= 160) {
throttleVal = 1;
}
else if (throttlebrakeValues.throttle <= 50) {
throttleVal = -1;
}
else{
throttleVal = 0;
}
My Problem is that the loop happens so fast, instead of just jumping one position of my navigation menu, sometimes it jumps on the last, third…. position of my navigation menu.
the displaydata method is in the loop(). Here is the necessary part of this method:
void displaydata()
{
maininfo *ptrmaininformation = &mainformation;
uint16_t speed = abs(((*ptrmaininformation).speed / 1000)); //speed is positiv
int8_t brakeVal = -1;
int8_t throttleVal = -1;
if (speed > 1)
{
display_option = 0;
}
if (speed <= 2)
{
if (throttlebrakeValues.brake >= 160){
brakeVal = 1;
}
else if (throttlebrakeValues.brake <= 50){
brakeVal = -1;
}
else {
brakeVal = 0;
}
if (throttlebrakeValues.throttle >= 160) {
throttleVal = 1;
}
else if (throttlebrakeValues.throttle <= 50) {
throttleVal = -1;
}
else{
throttleVal = 0;
}
if ((brakeVal == 1) && (throttleVal == 1))
{
menuposition = 0;
display_option = 1;
ic2display.clear();
}
switch (display_option)
{
case 1:
if ((throttleVal == 1) && (brakeVal == -1))
{
switch (menuposition)
{
case 0:
if (batterywarning == true)
{
batterywarning = false;
}
else
{
batterywarning = true;
}
EEPROM.update(1, batterywarning);
ic2display.clear();
break;
case 1:
display_option = 3;
ic2display.clear();
break;
case 2:
if (cruise_state == true)
{
cruise_state = false;
preparewritePackage(7);
}
else
{
cruise_state = true;
preparewritePackage(6);
}
EEPROM.update(2, cruise_state);
ic2display.clear();
break;
case 3:
if (taillight_state == true)
{
taillight_state = false;
preparewritePackage(4); //OFF
}
else
{
taillight_state = true;
preparewritePackage(5); //ON
}
EEPROM.update(3, taillight_state);
ic2display.clear();
break;
case 4:
switch (kers_state)
{
case 1:
preparewritePackage(2); //medium
kers_state = 2;
break;
case 2:
preparewritePackage(3); //strong
kers_state = 0;
break;
default:
preparewritePackage(1); //weak
kers_state = 1;
break;
}
EEPROM.update(4, kers_state);
ic2display.clear();
break;
case 5:
display_option = 2;
displayClear(1);
break;
}
}
else
{
if ((brakeVal == 1) && (throttleVal == -1))
{
if (menuposition < 5)
{
menuposition++;
}
else
{
menuposition = 0;
}
}
}
ic2display.set1X();
ic2display.setCursor(15, 0);
if (menuposition == 0)
{
ic2display.print((const __FlashStringHelper *)d_arrow);
}
else
{
ic2display.print(" ");
}
ic2display.print((const __FlashStringHelper *)d_navi_batterywarning);
if (batterywarning == false)
{
ic2display.print((const __FlashStringHelper *)d_off);
}
else
{
ic2display.print((const __FlashStringHelper *)d_on);
}
ic2display.setCursor(15, 1);
if (menuposition == 1)
{
ic2display.print((const __FlashStringHelper *)d_arrow);
}
else
{
ic2display.print(" ");
}
ic2display.print((const __FlashStringHelper *)d_navi_batteryinformation);
ic2display.setCursor(15, 2);
if (menuposition == 2)
{
ic2display.print((const __FlashStringHelper *)d_arrow);
}
else
{
ic2display.print(" ");
}
ic2display.print((const __FlashStringHelper *)d_navi_cruise);
if (cruise_state == true)
{
ic2display.print((const __FlashStringHelper *)d_on);
}
else
{
ic2display.print((const __FlashStringHelper *)d_off);
}
ic2display.setCursor(15, 3);
if (menuposition == 3)
{
ic2display.print((const __FlashStringHelper *)d_arrow);
}
else
{
ic2display.print(" ");
}
ic2display.print((const __FlashStringHelper *)d_navi_taillight);
if (taillight_state == true)
{
ic2display.print((const __FlashStringHelper *)d_on);
}
else
{
ic2display.print((const __FlashStringHelper *)d_off);
}
ic2display.setCursor(15, 4);
if (menuposition == 4)
{
ic2display.print((const __FlashStringHelper *)d_arrow);
}
else
{
ic2display.print(" ");
}
ic2display.print((const __FlashStringHelper *)d_navi_kers);
if (kers_state == 1)
{
ic2display.print((const __FlashStringHelper *)d_weak);
}
if (kers_state == 2)
{
ic2display.print((const __FlashStringHelper *)d_middle);
}
if (kers_state == 0)
{
ic2display.print((const __FlashStringHelper *)d_strong);
}
ic2display.setCursor(15, 5);
if (menuposition == 5)
{
ic2display.print((const __FlashStringHelper *)d_arrow);
}
else
{
ic2display.print(" ");
}
ic2display.print((const __FlashStringHelper *)d_navi_menu_exit);
break;
Anyone got an idea how I can fix that?
Thanks for any help!