# Impossible to set a variable to zero

I have problems resetting my variable AS to zero as soon as in reaches the value 100
This a part of my code. Thx in advance for your help!

`````` x = analogRead (0);
lcd.setCursor(10,1);
if (x < 60) {    //rechts
++AS;
lcd.setCursor(12,0);
lcd.print(AS);
if (AS > 100)  AS = 0;
delay(100);
``````

You set AS to 0, and then increment it before printing it. How do you expect to ever see 0 printed?

@Dashi, thx I tried that to does not work.

@PaulS that is true but that would give me a 1 for AS but in my example AS increments over 100 and never resets to zero

Again this is why you are told to post all your code. The problem is not where you think it is!

Mark

sorry , Mark,

this is the whole code. It’s a bit of a mess because I’m experimenting

`````` int x;
int SG = 20;
long AS = 20;
int y;

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

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,0);
lcd.print("SG");
lcd.setCursor(9,0);
lcd.print("AS");
lcd.setCursor(0,1);
lcd.print("Key ?");

}

void loop() {

lcd.setCursor(10,1);
if (x < 60) {    //rechts
++AS;
lcd.setCursor(12,0);
lcd.print(AS);
if (AS > 100)  AS = 0;
delay(100);

}
else if (x < 200) {
lcd.print ("Up    ");// stap vooruit
}
else if (x < 400){
lcd.print ("down");//stap achteruit
}
else if (x < 600){ // links
++SG;
lcd.setCursor(4,0);
lcd.print(SG);
delay(100);
}
else if (x < 800){
lcd.print ("start");//uitvoering AS * SG
}

}
``````
``````    if (x < 60) {    //rechts
++AS;                        // AS is incremented (after a previous clear)
lcd.setCursor(12,0);
lcd.print(AS);             // and then printed
if (AS > 100)  AS = 0;  // AS is cleared
// move the lcd.print to here instead
delay(100);
}
``````

I think you’re seeing residual characters on the LCD. When 100 is sent to the LCD, then AS gets reset to 0 (or 1), the next print only prints a single character (e.g. 1) and the two zeroes from the 100 remain.

Try something like

``````  x = analogRead (0);
lcd.setCursor(10,1);
if (x < 60) {    //rechts
++AS;
lcd.setCursor(12,0);
lcd.print(AS);
lcd.print("  ");
if (AS > 100)  AS = 0;
delay(100);
``````

That was it Jack, thx a lot.

It was so confusing it looked like it kept on counting!

Guido

Geyst:
That was it Jack, thx a lot.

It was so confusing it looked like it kept on counting!

Guido

You're welcome. I know ... did the same thing once!