Hi all this is my first post, I got my first Arduino yesterday and have been playing around with it. It's very interesting what you can do with it and I have a particular project in mind once I get some experience. However I'm having trouble with a simple arithmetic function.
Basically I have an lcd screen hooked up and some buttons, when I hit the up or down keys I either increment or decrease the value of the variable by one and print the result on the lcd which works fine. However when the variable gets to be greater than 5 or less than 1 I want to set the number to jump back to 1 or 5 respectively sounds simple right?
This is what I have done to achieve this however when I run it and I get to above 5 it continues to 6 then sets the value to 2 and when I decrease past 1 it continues to 0 then goes back to 4. What is going on here?
int ScrollUp(){
delay(10);
m++;
lcd.setCursor(0, 0);
lcd.print("^");
lcd.setCursor(0, 1);
lcd.print(m);
if (m > 5){
m = 1;
}
}
int ScrollDown(){
delay(10);
m--;
lcd.setCursor(0, 0);
lcd.print("v");
lcd.setCursor(0, 1);
lcd.print(m);
if (m < 1){
m = 5;
}
}
I should note that I can fix the problem by entering these values but it doesn't make sense.
hack fix
int ScrollUp(){
delay(10);
m++;
lcd.setCursor(0, 0);
lcd.print("^");
lcd.setCursor(0, 1);
lcd.print(m);
if (m > 4){
m = 0;
}
}
int ScrollDown(){
delay(10);
m--;
lcd.setCursor(0, 0);
lcd.print("v");
lcd.setCursor(0, 1);
lcd.print(m);
if (m < 2){
m = 6;
}
}
Full code
// include the library code:
#include <LiquidCrystal.h>
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 8, en = 9, d4 = 4, d5 = 5, d6 = 6, d7 = 7;// custom pin out for my LCD!!!
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int m = 1;
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
//lcd.setCursor(0, 1);
//lcd.print("w");
}
void loop() {
int x;
x = analogRead (0);
lcd.setCursor(0,0);
if (x < 80){
lcd.clear();
delay(400);
lcd.print("right key");
}else if (x < 200){
lcd.clear();
delay(400);
//lcd.print("up key");
ScrollUp();
} else if (x < 400){
lcd.clear();
//lcd.print("down key");
delay(400);
ScrollDown();
}else if (x < 600){
lcd.clear();
delay(400);
lcd.print("left key");
}else if (x < 800){
lcd.clear();
delay(400);
//lcd.print("select key");
SelectKey();
}
}
int ScrollUp(){
delay(10);
m++;
lcd.setCursor(0, 0);
lcd.print("^");
lcd.setCursor(0, 1);
lcd.print(m);
if (m > 5){
m = 1;
}
}
int ScrollDown(){
delay(10);
m--;
lcd.setCursor(0, 0);
lcd.print("v");
lcd.setCursor(0, 1);
lcd.print(m);
if (m < 1){
m = 5;
}
}
int SelectKey(){
delay(10);
lcd.setCursor(0, 0);
lcd.print("next menu");
lcd.setCursor(0, 1);
lcd.print(m);
}
int ScrollUp(){
delay(10);
m++;
lcd.setCursor(0, 0);
lcd.print("^");
lcd.setCursor(0, 1);
lcd.print(m);
if (m > 5){
m = 1;
}
}
You are printing the value of m to the lcd before rolling it over. So when you enter this function and m is 5, it will print 5 to the lcd. If you press the Up button again, m will now be 6, it will be printed to the screen and then the if condition will be matched, resetting m to 1. Next time round m will be incremented to 2, displaying this on the screen. You need to adjust your if condition to >= 5, or leave it as is and move it to before you print out the value of m.
Do your check immediately after your increment
m++;
if (m>5){
m=1;
}
Thanks for the help guys I realised what was wrong while I was at work thanks for confirming my suspicions I'll give it a try when I finish work.