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)

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.)

yep... that is infinitely better.

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

FYI