Just can't get it to count up or down anymore, what have I done wrong?

Hello everyone!

Beginner so be gentle. :slight_smile:

I just can't get this to count up or down or right to left, what have I missed?

There is of course a lot of more code and such over this but I only need to get this below to work.

As now it only prints out 54 on a 20x4 lcd at second row 5 column (don't ask me why) and if I push the right left button* (button in this case is a piece of thread) nothing happens but if I push up or down it goes slightly haywire with "54" all over the display, its like the debounce doesn't work which I can't get.

The button and debounce thing is from the Arduino examples with of course the necessary changes. And yes I know the button things can be made much in some way smaller but I just don't have the effort for it, I just want it to work first.

There are ints, unsigned longs and so on for everything that is stated below.

The "done" and "done2" was recently added and obviously failed effort to prevent the loop to execute the entire code in the "case" over and over but only once every time any button was pushed depending on which.

Oh well here is the failure:

void check_buttons() {

  byte up_reading = digitalRead(53);
  byte down_reading = digitalRead(52);
  byte right_reading = digitalRead(49);
  byte left_reading = digitalRead(47);


  if (up_reading != last_up_state) {
    lastDebounceTime = millis();

    if ((millis() - lastDebounceTime) > debounce) {

      if (up_reading != up_state)
        up_state = up_reading;

      if (up_state == HIGH) {

        up_down++;

        if (up_down > 9)
          up_down = 0;

      }
    }
    last_up_state = up_reading;
    done = 1;
  }



  if (down_reading != last_down_state) {
    lastDebounceTime = millis();

    if ((millis() - lastDebounceTime) > debounce) {

      if (down_reading != down_state)
        down_state = down_reading;

      if (down_state == HIGH) {

        up_down--;

        if (up_down < 0)
          up_down = 9;

      }
    }
    last_down_state = down_reading;
    done = 1;
  }




  if (right_reading != last_right_state) {
    lastDebounceTime = millis();

    if ((millis() - lastDebounceTime) > debounce) {

      if (right_reading != right_state)
        right_state = down_reading;

      if (right_state == HIGH) {

        right_left++;

        if (right_left > 4)
          right_left = 0;
      }
    }
    last_right_state = right_reading;
    done2 = 1;
  }




  if (left_reading != last_left_state) {
    lastDebounceTime = millis();

    if ((millis() - lastDebounceTime) > debounce) {

      if (left_reading != left_state)
        left_state = left_reading;

      if (left_state == HIGH) {

        right_left--;

        if (right_left < 0)
          right_left = 4;

      }
    }
    last_left_state = left_reading;

    done2 = 1;
  }
}





void check_left_right() {


  switch (right_left) {


    case 0: {

        if (done2 == 1) {

          lcd.setCursor(4, 1);
          done2 = 0;
        }

        if (done == 1) {

          up_down = value[0];
          value[0] = up_down;
          lcd.print(up_down);

          done = 0;
        }

        break;

      }


    case 1: {

        if (done2 == 1) {

          lcd.setCursor(6, 1);
          done2 = 0;
        }

        if (done == 1) {

          up_down = value[2];
          value[2] = up_down;
          lcd.print(up_down);

          done = 0;
        }

        break;

      }


    case 2: {

        if (done2 == 1) {

          lcd.setCursor(7, 1);
          done2 = 0;
        }

        if (done == 1) {

          up_down = value[3];
          value[3] = up_down;
          lcd.print(up_down);

          done = 0;
        }

        break;

      }

    case 3: {

        if (done2 == 1) {

          lcd.setCursor(9, 1);
          done2 = 0;
        }

        if (done == 1) {

          up_down = value[5];
          value[5] = up_down;
          lcd.print(up_down);

          done = 0;
        }

        break;

      }


    case 4: {

        if (done2 == 1) {

          lcd.setCursor(10, 1);
          done2 = 0;
        }

        if (done == 1) {

          up_down = value[6];
          value[6] = up_down;
          lcd.print(up_down);

          done = 0;
        }

        break;

      }
  }
}

The "left_right" 0 - 4 control which case to chose, and the "up_down" then sets the numbers 0 - 9 I want in the correct place in the array.

Nothing works now, I have got both the left_right and up_ down to count up as is but in the sketch only left_right works (read worked) as intended.

Thanks!

Ragards Kevin

Someone with 10 posts should know to post code inline using code tags after using the Arduino IDE autoformat feature.

... and, of course, the code should be compileable and complete.

It is auto formated, I do that all the time when I program so I don't know what you

mean?

Oh ok, I thought it would be obvious what the fault was but sure I can ad the rest as well

then, didn't thought it was necessary, sorry.

And I see that I missed to write that the "left_right" thing worked before just fine, but not

"up_down" in code but the count up and down as such did. But I changed the code for the

buttons a little but especially for the case statements which was quite an remake from the

original code but to the better as I felt at that point but obviously not.

Below compile without a single red row. I have used unsigned long at places even where

its not strictly necessary but its to avoid red lines.

Ok here is the whole code;

#include <LiquidCrystal.h>

LiquidCrystal lcd(40, 42, 28, 26, 24, 22);

unsigned long previous_cursor_millis = 0;
unsigned long previous_button_millis = 0;
unsigned long debounce_interval = 20;
unsigned long previousMillis = 0;
unsigned long currentMillis = 0;

unsigned long lastDebounceTime = 0;
unsigned long lastDebounceTime1 = 0;
unsigned long lastDebounceTime2 = 0;
unsigned long lastDebounceTime3 = 0;

unsigned long debounce = 35;

byte previous_up_down = 0;
byte previous_right_left = 0;

int up_state = HIGH;
int left_state = HIGH;
int down_state = HIGH;
int right_state = HIGH;

int last_right_state;
int last_left_state;
int last_up_state;
int last_down_state;

int left_reading = 0;
int right_reading = 0;
int up_reading = 0;
int down_reading = 0;

int done = 0;
int done2 = 0;

int up_down = 0;
int right_left = 0;

const byte right = 49;
const byte left = 47;
const byte up = 53;
const byte down = 52;

char value[] = "6.48E-7";


void setup() {

  pinMode(up, INPUT);
  pinMode(down, INPUT);
  pinMode(left, INPUT);
  pinMode(right, INPUT);
  digitalWrite(up, HIGH);
  digitalWrite(down, HIGH);
  digitalWrite(left, HIGH);
  digitalWrite(right, HIGH);

  lcd.begin(20, 4);
  lcd.clear();
  delay(1000);

}

void loop() {

  currentMillis = millis();

  check_buttons();

  check_left_right();

}


void check_buttons() {

  byte up_reading = digitalRead(53);
  byte down_reading = digitalRead(52);
  byte right_reading = digitalRead(49);
  byte left_reading = digitalRead(47);



  if (up_reading != last_up_state) {
    lastDebounceTime = millis();

    if ((millis() - lastDebounceTime) > debounce) {

      if (up_reading != up_state)
        up_state = up_reading;

      if (up_state == HIGH) {

        up_down++;

        if (up_down > 9)
          up_down = 0;

      }
    }
    last_up_state = up_reading;
    done = 1;
  }



  if (down_reading != last_down_state) {
    lastDebounceTime = millis();

    if ((millis() - lastDebounceTime) > debounce) {

      if (down_reading != down_state)
        down_state = down_reading;

      if (down_state == HIGH) {

        up_down--;

        if (up_down < 0)
          up_down = 9;

      }
    }
    last_down_state = down_reading;
    done = 1;
  }




  if (right_reading != last_right_state) {
    lastDebounceTime = millis();

    if ((millis() - lastDebounceTime) > debounce) {

      if (right_reading != right_state)
        right_state = down_reading;

      if (right_state == HIGH) {

        right_left++;

        if (right_left > 4)
          right_left = 0;
      }
    }
    last_right_state = right_reading;
    done2 = 1;
  }




  if (left_reading != last_left_state) {
    lastDebounceTime = millis();

    if ((millis() - lastDebounceTime) > debounce) {

      if (left_reading != left_state)
        left_state = left_reading;

      if (left_state == HIGH) {

        right_left--;

        if (right_left < 0)
          right_left = 4;

      }
    }
    last_left_state = left_reading;
    done2 = 1;
  }
}






void check_left_right() {


  switch (right_left) {


    case 0: {

        if (done2 == 1) {

          lcd.setCursor(4, 1);
          done2 = 0;
        }

        if (done == 1) {

          up_down = value[0];
          value[0] = up_down;
          lcd.print(up_down);

          done = 0;
        }

        break;

      }


    case 1: {

        if (done2 == 1) {

          lcd.setCursor(6, 1);
          done2 = 0;
        }

        if (done == 1) {

          up_down = value[2];
          value[2] = up_down;
          lcd.print(up_down);

          done = 0;
        }

        break;

      }


    case 2: {

        if (done2 == 1) {

          lcd.setCursor(7, 1);
          done2 = 0;
        }

        if (done == 1) {

          up_down = value[3];
          value[3] = up_down;
          lcd.print(up_down);

          done = 0;
        }

        break;

      }

    case 3: {

        if (done2 == 1) {

          lcd.setCursor(9, 1);
          done2 = 0;
        }

        if (done == 1) {

          up_down = value[5];
          value[5] = up_down;
          lcd.print(up_down);

          done = 0;
        }

        break;

      }


    case 4: {

        if (done2 == 1) {

          lcd.setCursor(10, 1);
          done2 = 0;
        }

        if (done == 1) {

          up_down = value[6];
          value[6] = up_down;
          lcd.print(up_down);

          done = 0;
        }

        break;

      }
  }
}

What is the point of this? Similar things happen more than once:

          up_down = value[6];
          value[6] = up_down;

By the way, putting an int into an array does not turn it into a character.

My thought (which I now get doesn't work) was to be able to put numbers in the array at the different elements. But when I shift right or left through the elements those two rows or code then first look what the number is at the specific element and put whatever the number there in the up_down count so even if I go through the elements the same numbers stay in the up_down and thus doesn't nothing is changed even if I go through the elements which it would have otherwise.

But I knwo I have tested to put say a 9 from an int into a specifik element in the array and when I printed the array it was changed. but to put an char into a int of course doesn't work, no, so only half of it works sort of speak.

So now when you mention the char and int thing my whole plan for this is destroyed and I remember I know I had thought about that part before some time back but could't really find a solution for it then or obviously not now either.


It's a char array and I would like to be able to change the numbers in the different element places with a simple up down count thingy byt thus for a char array. I have no idea how to do that so I have to come up with something totally different and pretty much just trash the entire code which was definitely nothing I had planned. :confused: Dang it.

Please restart: you want to count things. You have 4 bottons (L, R, U, D) if you press L you want tje counter increesed, with R the counter decreesed. And with U and D what do you want?
Aniway why are you working on arrays? Can't you work with a simple number? The lcd.print() function print than too, and it os easyer work with than

I have this:

1.23E-4

I want to be able to change the numbers to whatever I want between 0 and 9 at those 4 places places and also the - sign to a + and vice versa, but that is next problem.

The number changes is what the U and D do by the int "up_down = 0;" which I haven't got to work.

I then need to change from left to right to be able to get to the different elements and that is what L and R do with the help of the switch case which guide the counting int (up_down) to the right place in the array.

After all is done I Serial.print the entire array to a sensor, and it was so convenient to do this with an array since it was just to Serial.print(value); and done.


And btw this do work by whatever the reason, well at least in that way that I see what I expect.

I read the info from the first element in char "value" and put that in a ordinary int and then put that info in all the other elements to test and well it works in such that it show right on the lcd?

#include <LiquidCrystal.h>

LiquidCrystal lcd(40, 42, 28, 26, 24, 22);

int up_down = 0;

char value[] = "1.23E-4";

void setup() {

  lcd.begin(20, 4);
  lcd.clear();
  //delay(1000);


  up_down = value[0];
  value[2] = up_down;
  value[3] = up_down;
  value[6] = up_down;
  lcd.print(value);

  //And I get 1.11E-1 as expected but once again don't know how since a char is put into a int.

}

void loop() {

}

It happens (in C/C++, using ASCII) that the digit characters (char) '0' through '9' are represented by the integers (int) 48 through 57. Thus, if you have a digit, adding one to the digit gives you the next digit. Of course, this runs into problems when your digit character is '9' because the next character is a colon. This can also run into trouble with '+' and '-' which are represented by 43 and 45 respectively.

On the other hand, if you take an existing character (say, '1') and store it in a character location - even if you passed through an int variable - the stored character will be what you started with (say, '1').

One way of converting an arbitrary number to a string is to use the function snprintf(...). An easy way to convert a single-digit integer to a character is to add 48 or even to add '0' which more clearly indicates your purpose.

An ASCII table is available at https://www.asciitable.com/ but there are others.

vaj4088:
It happens (in C/C++, using ASCII) that the digit characters (char) '0' through '9' are represented by the integers (int) 48 through 57.

This is indeed a gotcha. It has led to many requests for help on these forums, from people who apparently did not even realize that there was a gotcha to be "got" by.

Why did the inventors of ASCII not just assign the characters '0' through '9' the values, well, 0 through 9 ?

odometer:
Why did the inventors of ASCII not just assign the characters '0' through '9' the values, well, 0 through 9 ?

Partial answer - compare ASCII hex codes against original 5-bit Baudot codes.

ASCII history

So you don't want to count pressiine, you want to move a number in the display. Something like

byte first_point_row;
byte first point_col;
int number;
lcd.setCursor (first_point_col, first_point_row);
lcd.print (number);

Yea kinda. But the int char thing messes it all up so I have to rethink it all due to that, but at least now I know what was wrong, but I might even take a look at that function that was mentioned, if it exists in Arduino that is.

Delta_G:
A char is a data type that has 8 bits and holds a signed value. They are often used to hold ascii codes, but not always.

An int is 16 bits and holds a signed value.

You can assign a char to an int. It is just a number after all.

But be careful treating them as numbers. In your example those chars actually hold ascii codes. Study the ascii table well. if you print up_down at the end you'll see that it is actually the number 49. If you print it as a char it prints 1. That's because the print function treats char and int differently. For int it converts it to ascii first. For char it just assume that it is already ascii code and just prints it straight.

Thanks for that info, really interesting. And all the time I have had this hunch that it was the case that it was "transformed" to a two digit number so I looked what actually was in the int and yep a 2 digit number which is obviously why it all went haywire in my original code since it was not intended for that so thanks for that!

And thanks for the info vaj4088, now I know!

Thanks guys, it was really appreciated! :slight_smile:

Regards Kevin