Display navigation to fast

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!

The problem is in the code you didn't post.

A circuit diagram may also be helpful.

MorganS:
The problem is in the code you didn't post.

A circuit diagram may also be helpful.

sorry i did a update. I think a circuit diagram dosent help here. Im getting brake and throttle value from a databus every few milliseconds

What does this mean?

instead of just jumping one position of my navigation menu, sometimes it jumps on the last, third…. position of my navigation menu.

And, why do you think the action has anything at all to do with loop execution speed?

jremington:
What does this mean?
And, why do you think the action has anything at all to do with loop execution speed?

Here is a photo of my menu:

The arrow indicates my actual position. By pressing my brake as fast as possible, brakeVal = 1 will get one and the arrow will jump on the next menu option. If I am holding my brake, the arrow will jump through all menu options.

What I want now is by pressing my brake once or by holding my brake, it should just move the arrow by ONE postion.

rosch94:
Here is a photo of my menu:

navimenu hosted at ImgBB — ImgBB

The arrow indicates my actual position. By pressing my brake as fast as possible, brakeVal = 1 will get one and the arrow will jump on the next menu option. If I am holding my brake, the arrow will jump through all menu options.

What I want now is by pressing my brake once or by holding my brake, it should just move the arrow by ONE postion.

Then change your program so you only display some value if has changed from the previous value.

Paul

Please post the complete program. If it is a long program then it would be a good idea to make a short complete program that illustrates the problem.

...R

You are probably not reading the switches properly, but you failed to post the relevant part of the program.

jremington:
You are probably not reading the switches properly, but you failed to post the relevant part of the program.

its just a snippet.

I got guys. This one gives me the result i want: :slight_smile:

    if (millis() > buttontime + 500)
    {
      if (throttlelebrakeNew.brake >= 160)
      {
        buttontime = millis();
        brakeVal = 1;
      }
      else if (throttlelebrakeNew.brake <= 50)
      {

        brakeVal = -1;
      }
      else
      {
        brakeVal = 0;
      }

      if (throttlelebrakeNew.throttle >= 160)
      {
        buttontime = millis();

        throttleVal = 1;
      }
      else if (throttlelebrakeNew.throttle <= 50)
      {

        throttleVal = -1;
      }
      else
      {
        throttleVal = 0;
      }
    }

This is not the correct way to use millis() as it will not work when millis() rolls over

if (millis() > buttontime + 500)

Always use subtraction - like this

if (millis() - buttontime >=  500)

...R

Robin2:
This is not the correct way to use millis() as it will not work when millis() rolls over

if (millis() > buttontime + 500)

Always use subtraction - like this

if (millis() - buttontime >=  500)

...R

i changed it and it works fine.. ty.

one think i dont understand about millis(). Im using long to store the value of it. And my programm is never running more than a few hours. So it should never overrun or? Since unsinged long can store 32bits.

Since unsinged long can store 32bits.

A signed long is also 32 bits.

You really need to be carefully singeing those longs...

PaulS:
A signed long is also 32 bits.

You really need to be carefully singeing those longs...

yes but my assumption was that an overrun can never happen because my programm is running only a few hours day.

The "only a few hours per day" thing could easily be left switched on for 49 days, so why write code you know will have a problem?

Years can be stored with 2 digits too. But I am sure that some software I am writing now will still be running in 2100. I don't know which one, so I have to write everything with that assumption.

rosch94:
yes but my assumption was that an overrun can never happen because my programm is running only a few hours day.

That is reasonable - but why not do the job properly - typing the word unsigned does not take very much effort.

...R