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.
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?
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.
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*/
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
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