if statement

I have two nearly identical if statements. The first one compiles fine. It errors out (exit status 1
expected primary-expression before 'counter')

if (byte counter == 1)
{
digitalWrite(ledPin,HIGH);
lcd.setCursor(10,0);
lcd.print(" ON");
byte counter = counter + 1;
}

if(byte counter >= 3) <-------------HERE
{
digitalWrite(ledPin,LOW);
lcd.setCursor(10,0);
lcd.print("OFF");
int counter = 0;
}

I dont understand the difference!

You should only specify the the variable type when you declare it:

byte counter;

When you reference that variable, you do not need the variable type:

if(counter >= 3)

When you write the type, "byte" in this case, you are creating a new variable.

Inside the if statement, you don't want to create a new variable. You want to compare the one you already had some value stored in with some number.

Lose the "byte" everywhere. You only need it when you create that counter variable.

Please Read how to use the forum. Especially the part about code tags, posting a complete program and posting the complete error!

But I can tell you, BOTH statements are wrong. The byte keyword tells the compiler to create a NEW variable which is initialized to an unknown value. So pretty useless to make a comparison against that. If you want to check against an already initialized variable called 'counter', drop the byte keyword.

Always show us your ‘current’ compete sketch.
Use CTRL T to format the sketch.
Please use code tags.
Use the </> icon in the posting menu.

[code] Paste sketch here. [/code]

thanks for the quick reply. It compiled atleast. but it doesn't work as planned. heh. any suggestions? It seems to cycle thru the byte counter entirely at the press of my button

#include <LiquidCrystal.h>
byte counter;
const int keyPin = 8;
const int ledPin = 9;
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(keyPin, INPUT);

  lcd.begin(16, 2);

  lcd.print("  RINTECH LABS  ");
  lcd.setCursor(0, 1);
  lcd.print("< Initializing >");
  byte counter = 0;
  delay(3000);
  lcd.clear();

  lcd.setCursor(0, 0);
  lcd.print("  RINTECH LABS  ");
  lcd.setCursor(0, 1);
  lcd.print("<     Ready    >");
  delay(750);

  lcd.clear();
  delay(250);

  lcd.setCursor(0, 0);
  lcd.print("Sw:    Lt:OFF");
  lcd.setCursor(0, 1);
  lcd.print("Time:     mins");
}

void loop() {

  lcd.setCursor(5, 1);
  lcd.print(millis() / 60000);

  if (digitalRead(keyPin) == HIGH )
  {
    lcd.setCursor(3, 0);
    lcd.print(" ON");
    counter = counter + 1;
    // digitalWrite(ledPin,HIGH);
    //  lcd.setCursor(10,0);
    //  lcd.print(" ON");

  }
  else
  {
    lcd.setCursor(3, 0);
    lcd.print("OFF");
  }
  if (counter == 1)
  {
    digitalWrite(ledPin, HIGH);
    lcd.setCursor(10, 0);
    lcd.print(" ON");
    byte counter = counter + 1;
  }

  if ( counter >= 3)
  {
    digitalWrite(ledPin, LOW);
    lcd.setCursor(10, 0);
    lcd.print("OFF");
    int counter = 0;
  }

}

You still have the same problem in other places. This line:

avender:

    byte counter = counter + 1;

Creates a variable named counter that is local to the if statement. If you wanted to change the value of the global variable named counter, then you need to remove the "byte".

Same problem here:

avender:

    int counter = 0;

oops. missed one. thanks.

a delay fixes it. but is there any other solutions to keep the variable from cycling? (uhg I hate being a newb, lol.)

You're adding to it whenever the button IS pressed. The Arduino can run that loop thousands of times between the time you press and release the button. Instead you need to react just once when the button BECOMES pressed, or is pressed now but wasn't last time. See the "State Change Example" for how.

BTW: I notice you have your buttons reading HIGH when pressed. That's backwards of how we normally do it. Normally you would wire up so that it connects to ground and reads LOW when pressed. That way you can use the internal pull-up resistors with pinMode INPUT_PULLUP and you don't need external resistors for your button. I know newbies like for HIGH to be the same as ON but in the real world it isn't always that way.

yep... that is infinitely better.

Im not sure I completely understand how button to ground works. But thanks for the advice. :slight_smile:

avender:
Im not sure I completely understand how button to ground works. But thanks for the advice. :slight_smile:

What's to understand? Instead of connecting the button between the pin and 5V you connect between the pin and ground and then you don't have to have a resistor for the button because you can use the ones built into the chip.

FYI