code problem

Hello,

I have a problem with my code. I dont know what i did wrong and my code isnt working pls help me

i cant use both if and else the second wont work.

int const knop = A5;
int const knop2 = A4;
int count = 0;

void setup() {
pinMode (knop, INPUT_PULLUP);
pinMode (knop2, INPUT_PULLUP);
Serial.begin(9600);

}

void loop() {

if (digitalRead(knop) == HIGH)
{
while (digitalRead(knop) == HIGH);
while (digitalRead(knop) == LOW);
count++;
delay(100);
Serial.println(“De knop is al”);
Serial.println(count);
Serial.println(“keer ingedrukt.”);
Serial.println(" “);
}
else if (digitalRead(knop2) == HIGH)
{
while (digitalRead(knop2) == HIGH);
while (digitalRead(knop2) == LOW);
count++;
delay(100);
Serial.println(“De knop is al”);
Serial.println(count);
Serial.println(“keer ingedrukt.”);
Serial.println(” ");
}
}

sketch_sep12a.ino (776 Bytes)

  while (digitalRead(knop) == HIGH);
  while (digitalRead(knop) == LOW);

Do you understand exactly what these 2 lines of code do ?

Please follow the advice on posting code in Read this before posting a programming question

In particular note the advice to Auto format code in the IDE and using code tags when posting code here

Hi bramcelis,

to get you started analysing what your coe is really doing here is your code with additional debug-output

int const knop = A5;
int const knop2 = A4;
int count = 0;

void setup() {
  pinMode (knop, INPUT_PULLUP);
  pinMode (knop2, INPUT_PULLUP);
  Serial.begin(9600);
  Serial.println("Setup-start");

}

void loop() {

  Serial.println("right before if (digitalRead(knop) == HIGH)");
  if (digitalRead(knop) == HIGH)
  {
    Serial.println("right before while (digitalRead(knop) == HIGH);");
    while (digitalRead(knop) == HIGH);
    Serial.println("right before while (digitalRead(knop) == LOW);");
    while (digitalRead(knop) == LOW);
    count++;
    delay(100);
    Serial.println("De knop is al");
    Serial.println(count);
    Serial.println("keer ingedrukt.");
    Serial.println(" ");
  }
  else if (digitalRead(knop2) == HIGH)
  {
    Serial.println("right before while (digitalRead(knop2) == HIGH);");
    while (digitalRead(knop2) == HIGH);
    Serial.println("right before while (digitalRead(knop2) == LOW);");
    while (digitalRead(knop2) == LOW);
    count++;
    delay(100);
    Serial.println("De knop is al");
    Serial.println(count);
    Serial.println("keer ingedrukt.");
    Serial.println(" ");
  }
}

upload this codeversion and analyse the output in the serial-monitor.
This will help to understand what your code is doing:

Your code always does what you have programmed. Though it is something different than you expected you don't (yet) understand your program.

Another programmers-wisdon: In 98% of all cases the bug has its hands on the keyboard and is staring at the screen ;-)))

best regards Stefan