my led is supposed to light only when "count" exceeds or becomes 2 but doesn't

Hello guys,

please can u tell me WHY the LED is lighting all the time?
It is supposed to light only 1 second after count hast become or exceeded the value of 2?!

const int button1Pin = 2;
const int button2Pin = 3;
const int button3Pin = 4;
const int button4Pin = 5;
const int ledPin = 13;
int count;

void setup()
{
pinMode(button1Pin, INPUT);
pinMode(button2Pin, INPUT);
pinMode(button3Pin, INPUT);
pinMode(button4Pin, INPUT);
pinMode(ledPin, OUTPUT);
count == 0;
}

void loop()
{
int button1State, button2State, button3State, button4State;

button1State = digitalRead(button1Pin);
button2State = digitalRead(button2Pin);
button1State = digitalRead(button3Pin);
button2State = digitalRead(button4Pin);

if (count >= 2)
{
//digitalWrite(ledPin, HIGH);
delay(1000);
count == 0;
}
else
{
digitalWrite(ledPin, LOW);

if (button1State == LOW)

{
count = count + 0,2;
}

if (button2State == LOW)

{
count = count + 0,5;
}

if (button3State == LOW)

{
count = count + 1;
}

if (button4State == LOW)

{
count = count + 2;
}
}

}

Lookup the differences of '=' and '=='.

Floats are written as 2.01 not 2,01 lookup comma - operator.

Don't add floats to ints.

Use the correct variables.

If you feel it is neccessary to use floats, change the units and use ints if possible (I used 1/10).

Have a look at my suggestion:

const int button1Pin = 2;
const int button2Pin = 3;
const int button3Pin = 4;
const int button4Pin = 5;
const int ledPin =  13;
int count = 0;

void setup()
{
  pinMode(button1Pin, INPUT_PULLUP); // probably better unless you have
  pinMode(button2Pin, INPUT_PULLUP); // external pullups
  pinMode(button3Pin, INPUT_PULLUP);
  pinMode(button4Pin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  int button1State = digitalRead(button1Pin);
  int button2State = digitalRead(button2Pin);
  int button3State = digitalRead(button3Pin);
  int button4State = digitalRead(button4Pin);

  if (count >= 20)
  {
    digitalWrite(ledPin, HIGH);
    delay(1000);
    count = 0;
  }
  else
  {
    digitalWrite(ledPin, LOW);
    if (button1State == LOW)
    {
      count += 2;
    }
    if (button2State == LOW)
    {
      count += 5;
    }
    if (button3State == LOW)
    {
      count += 10;
    }
    if (button4State == LOW)
    {
      count += 20;
    }
  }
}

Thank u very much Whandall!

Has been to long ago that I have had c++ courses :smiley:

Sure your way with ints is the better way (no floats)

I have hardware pullups :slight_smile:

With your code the LED lights every time for 1 second, no matter which button I press :-/

It's your algorithm, I only brushed it up a bit and made it compile.

You probably underestimate the speed of the loop repetitions. :wink:

Take millis at the top of the loop, then on the bottom and print the difference, to time one repetition.

Printing out all the values that count goes through, I bet you will see all the steps.

Try if count >= 20000, or add a delay to the loop.

Delay of 200ms solved it :slight_smile:

The button seemed to be toggling and so sending multiple 2,5,10 values which did sum up to 20 easily :wink:

THANK U MAN :)!!

Vending machine?

Protip: when dealing with money, use cents instad of euros. This way, you only need ints (or, for large amounts of money, longs).

// if the price of something is one euro and fifty cents
int price = 1.50; // don't do this
int price = 150; // do this


// or, if the price is two euros
int price = 2; // not good
int price = 200; // better

If you want to display the number as a “normal” price, you can try something like this:

Serial.print(price/100); // whole euros
Serial.print(","); // because you want to see a comma
if ((price%100)<10) Serial.print("0"); // leading zero for cents (00 to 09)
Serial.print(price%100); // cents

Also, you should remember:

// Single equals sign:
x = y; // this sets x to be equal to y
// (if x does not already equal y, then the value of x will be changed)


// Double equals sign:
u == v; // this checks whether u is equal to v
// (it won't change the value of u, and it also won't change the value of v)

Thank you for your advice!

YES it is a vending machine :slight_smile:

Till123:
Has been to long ago that I have had c++ courses :smiley:

I never hat any C++ or C course and only a Kernighan-Ritchie book next to an IBM-8088-PC. :wink:

Till123:
Thank you for your advice!

YES it is a vending machine :slight_smile:

Oh, by the way, I just fixed a bug in my code for displaying a number. (I had too many parentheses.) The code in my previous post (reply #5 in this thread) has been fixed.