Programming a Green LED to turn after 3 button presses

I don’t understand what I am doing wrong. I don’t think my counter is working. Everything seems to be going wrong.

I need the green led to start on and turn off after 3 button presses

Yellow starts on and turns off with the button is pressed down then when u let go it goes back to lighting up the yellow led

I have a red LED that starts off and turns on while the button is pressed then turns off again when you let go.

I have a few errors as well

fbchnfgnh.png

You have :
digitalRead(button==HIGH)

It should be:
digitalRead(button)==HIGH

When posting code (or error messages) here, always post it as text, not as a screenshot.

Sorry bout that
Code:
const int greenLed = 3;
const int yellowLed = 9;
const int redLed = 7;
const int button = 4;
int buttonArray[3];
int loopCounter = 0;

void setup()
{
pinMode(greenLed, OUTPUT);
pinMode(yellowLed, OUTPUT);
pinMode(redLed, OUTPUT);
pinMode(button, INPUT);
}

void loop()
{
digitalWrite(yellowLed, HIGH);
loopCounter = 0;

do{

if (digitalRead(button) == LOW) {

buttonArray[loopCounter] = 1;
digitalWrite(yellowLed, LOW);
digitalWrite(redLed, HIGH);
loopCounter++;
}
else if digitalRead(button == HIGH) {

buttonArray[loopCounter] = 2;
digitalWrite(yellowLed, HIGH);
digitalWrite(redLed, LOW);
loopCounter++;
}
}while (loopCounter < 3);

loopCounter = 0
do{
if (digitalRead(button) == HIGH){

digitalWrite(greenLed, HIGH);
}
}while (loopCounter < 2);

Error: In function ‘void loop()’:
30:16: error: expected ‘(’ before ‘digitalRead’
40:3: error: expected ‘;’ before ‘do’
45:25: error: expected ‘}’ at end of input

chrisbybelden:
Error: In function 'void loop()':
30:16: error: expected '(' before 'digitalRead'
40:3: error: expected ';' before 'do'
45:25: error: expected '}' at end of input

Please post your code using code tags.

The compiler is telling you exactly what is wrong. Correct the compilation errors and then we can help you debug your program.

Apart from that, you still have the error which was pointed out to you in #1:

else if digitalRead(button == HIGH) {

const int greenLed = 3;
const int yellowLed = 9;
const int redLed = 7;
const int button = 4;
int buttonArray[3];
int loopCounter = 0;

void setup()
{
pinMode(greenLed, OUTPUT);
pinMode(yellowLed, OUTPUT);
pinMode(redLed, OUTPUT);
pinMode(button, INPUT);
}

void loop()
{
digitalWrite(yellowLed, HIGH);
loopCounter = 0;

do{

if (digitalRead(button) == LOW) {

buttonArray[loopCounter] = 1;
digitalWrite(yellowLed, LOW);
digitalWrite(redLed, HIGH);
loopCounter++;
}
else if (digitalRead(button) == HIGH) {

buttonArray[loopCounter] = 2;
digitalWrite(yellowLed, HIGH);
digitalWrite(redLed, LOW);
loopCounter++;
}
}while (loopCounter < 3);

do{
if (digitalRead(button) == HIGH) {

digitalWrite(greenLed, HIGH);
}
}while (loopCounter < 2);
}

I no longer have any errors. I still can't get the led to turn off after 3 turns. Sorry, I am new to all this tbh... If anyone sees any redundancies or if I am somehow messing up the loop counter. That would definitely help me get better.

Ideally, you would present your code like this, correctly indented and within code tags:

const int greenLed = 3;
const int yellowLed = 9;
const int redLed = 7;
const int button = 4;
int buttonArray[3];
int loopCounter = 0;

void setup()
{
  pinMode(greenLed, OUTPUT);
  pinMode(yellowLed, OUTPUT);
  pinMode(redLed, OUTPUT);
  pinMode(button, INPUT);
}

void loop()
{
  digitalWrite(yellowLed, HIGH);
  loopCounter = 0;

  do {

    if (digitalRead(button) == LOW) {

      buttonArray[loopCounter] = 1;
      digitalWrite(yellowLed, LOW);
      digitalWrite(redLed, HIGH);
      loopCounter++;
    }
    else if (digitalRead(button) == HIGH) {

      buttonArray[loopCounter] = 2;
      digitalWrite(yellowLed, HIGH);
      digitalWrite(redLed, LOW);
      loopCounter++;
    }
  } while (loopCounter < 3);


  do {
    if (digitalRead(button) == HIGH) {

      digitalWrite(greenLed, HIGH);
    }
  } while (loopCounter < 2);
}

You are making a basic error of using a do…while construct within the loop() function. Only under special circumstances should you do that. If you want to count button presses, have a specific counter for the task, called say button_presses , which you increment every time you detect that a button has changed it state from not being pressed to being pressed.