For loop into a function

For an orientation course at school we get taught to work with Arduino. I have the following issue with a challenge.

I have to setup 1 LED with 2 buttons. if button 1 is pressed it needs to blink 3 times, with button 2 it has to blink 5 times.

This is the simulation:

This is the code:

int buttonPin = 2; // the number of the pushbutton pin
int buttonPin2 = 8;
int ledPin = 13; // the number of the LED pin

int buttonState = 1; // variable for reading the pushbutton status
int buttonState2 = 3;

void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
for (int i = 1; i <= 3; i++) {
digitalWrite(ledPin, HIGH); // turn the LED on
delay(50); // wait
digitalWrite(ledPin, LOW); // turn the LED off
delay(500); // wait
}
}

buttonState2 = digitalRead(buttonPin2);
if (buttonState == LOW) {
for (int i = 3; i <= 5; i++) {
digitalWrite(ledPin, HIGH); // turn the LED on
delay(50); // wait
digitalWrite(ledPin, LOW); // turn the LED off
delay(500); // wait
}
}

}

/* Turning For loop into a function with a variable of 3 or 5*/

The last part in this code wasn't working. It now blink 6 times?? the assignment and my teacher also says to use a function() for this to make the code smaller.

I have no idea how to fix this? Can somebody help and explain? Please don't make the code harder than it needs to be.

Thanks!Preformatted text

In this section of code I believe you should be checking buttonState2 instead of buttonState :

  buttonState2 = digitalRead(buttonPin2);
  if (buttonState == LOW) {
    for (int i = 3; i <= 5; i++) {
      digitalWrite(ledPin, HIGH); // turn the LED on
      delay(50); // wait
      digitalWrite(ledPin, LOW); // turn the LED off
      delay(500); // wait
    }
  }

Before you get into adding more functions, you might correct this code and test it.
Both setup() and loop() are functions. You want to add new function code to do something that is already being done in your code. Can you pick out what parts that might be?

Paul

It is conventional to start the loop counter at 0 if it is only used to repeat the body of the loop. Like this:

   // Repeat 5 times
    for (int i = 0; i < 5; i++) {

I want an new function so that when i press button 1 is blinks 3 times and when button 2 is pressed it need to blink 5 times.

Thanks for your code, unfortunately only button one (left buton) blinks it 3 times now. The right button still does nothing

IT might begin to do something if you fix your program!
Paul

Did you make the change I pointed out in reply #2? That is why the right button does not do anything. Also, after you correct that you will have to fix the for loop to make it blink 5 times (as I indicated in reply #4).

After you make those changes look at the code and see what code is similar in order to make a function for blinking.

That loops four times

Please remember to use code tags when posting code

This might explain why the right button doesn't do anything. Also that repeats 6 times.

But it does nothing six times

And very efficiently.

1 Like
int buttonPin = 2; // the number of the pushbutton pin
int buttonPin2 = 8;
int ledPin = 13; // the number of the LED pin

int buttonState = 1; // variable for reading the pushbutton status
int buttonState2 = 3;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);
}
void loop() {
  buttonState = digitalRead(buttonPin);
  if (buttonState == LOW) {
    for (int i = 0; i <= 3; i++) {
      digitalWrite(ledPin, HIGH); // turn the LED on
      delay(50); // wait
      digitalWrite(ledPin, LOW); // turn the LED off
      delay(500); // wait
    }
  }

buttonState2 = digitalRead(buttonPin2);
if (buttonState2 == LOW) {
for (int i = 0; i <= 5; i++) {
    }
  }

}

/* Turning For loop into a function with a  variable of  3 or 5*/

Why not initialise it in setup?
Actually, why does it have global scope at all?

Please remember to use code tags when posting code.

Why are these initialized to 1 and 3? You really don't need them both the way you have it coded.

I put the whole code in #13 correctly this time :), Everything worked perfectly for button 1. so i think the above should be correct. After i started with buttonstate 2 it went wrong

You could do it like this:

void loop() {
  int buttonState = digitalRead(buttonPin);
  if (buttonState == LOW) {
    // blink LED 3 times
  }

  buttonState = digitalRead(buttonPin2);
  if (buttonState == LOW) {
    // blink LED 5 times
  }
}

int buttonPin = 2; // the number of the pushbutton pin
int buttonPin2 = 8;
int ledPin = 13; // the number of the LED pin

int buttonState = 1; // variable for reading the pushbutton status
int buttonState2 = 3;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);
}
void loop() {
  int buttonState = digitalRead(buttonPin);
  if (buttonState == LOW) {
    // blink LED 3 times
  }

  buttonState = digitalRead(buttonPin2);
  if (buttonState == LOW) {
    // blink LED 5 times
  }
}

/* Turning For loop into a function with a  variable of  3 or 5*/

I have it like this now, both buttons not working I use Tinkercad as a simulation

This is the Arduino forum, not the Tinkercad forum

I use that as the simulation program

So how do you know the imaginary buttons are not working?