Go Down

Topic: if statement (Read 184 times) previous topic - next topic

avender

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!

pert

You should only specify the the variable type when you declare it:
Code: [Select]
byte counter;
When you reference that variable, you do not need the variable type:
Code: [Select]
if(counter >= 3)

Delta_G

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.  
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

septillion

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.
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

larryd

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]


No technical PMs.
The last thing you did is where you should start looking.

avender

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


Code: [Select]

#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;
  }

}

pert

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

Code: [Select]

    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:
Code: [Select]

    int counter = 0;


avender

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

Delta_G

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. 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

avender

yep... that is infinitely better.

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

Delta_G

Im not sure I completely understand how button to ground works. But thanks for the advice. ^_^
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. 


http://www.gammon.com.au/switches
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

larryd

No technical PMs.
The last thing you did is where you should start looking.

Go Up