I need to make a scheme on https://www.tinkercad.com/ and code that 3 different pushbuttoms interacted with their Led. While button 1 is pressed, LED1 is on. Pressing button 2 once turns LED2 on, pressing it again turns it off. While button 3 is pressed, LED3 flashes at 1 Hz. I also have this code but idk if it right.
`const int buttonPin1 = 2;
const int buttonPin2 = 3;
const int buttonPin3 = 4;
const int ledPin1 = 5;
const int ledPin2 = 6;
const int ledPin3 = 7;
bool led2State = false;
bool led3State = false;
bool led3Blink = false;
unsigned long previousMillis = 0;
const long interval = 500;
void setup() {
pinMode(buttonPin1, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
pinMode(buttonPin3, INPUT_PULLUP);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
}
void loop() {
if (digitalRead(buttonPin1) == LOW) {
digitalWrite(ledPin1, HIGH);
} else {
digitalWrite(ledPin1, LOW);
}
if (digitalRead(buttonPin2) == LOW) {
if (!led2State) {
digitalWrite(ledPin2, HIGH);
led2State = true;
} else {
digitalWrite(ledPin2, LOW);
led2State = false;
}
delay(300);
}
if (digitalRead(buttonPin3) == LOW) {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
if (led3Blink) {
digitalWrite(ledPin3, LOW);
led3Blink = false;
} else {
digitalWrite(ledPin3, HIGH);
led3Blink = true;
}
}
} else {
digitalWrite(ledPin3, LOW);
led3Blink = false;
}
}`