I need to make a scheme and code that 3 different pushbuttoms everyone 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 will be thankfull to anybody who helps
Welcome to the forum
How much code have you written so far ?
For button 1 / led 1 a simple digitalRead() / digitalWrite() will do. For button 2 / led 2 I suggest that you study the state change detection example in the IDE. And for button 3 / led 3 you can look at the blink without delay example and the many topics how to do multiple things at the same time and using millis() based timing.
I got this, but dont know if it works:
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;
}
}
Please edit your post and add code tags to the code to make it easier to read and copy for examination
This is the easiest way to tidy up the code and add the code tags
Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.
This is your program with code tags (</>). Is it working the way you want?
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;
}
}
I cant verify if its correct without scheme
Does it mean that you have no ---
Arduino UNO,
Buttons,
LEDs,
Breadbooard, and
jumpers.
Or
Do you want a schematic which will help you to connect the Buttons and LEDs with UNO?
From where have you got the sketch of post #4?
Why? For whom? What Arduino projects have you successfully created to date?
a7
Works. If you keep your button presses short on button 2.
- button one - LED1 on while button is pressed
- button two - LED2 toggles on/off with each press
- button three - LED3 blinks while button is pressed
Wire the LEDs with series current limiting resistors between the pins and ground.
Wire the buttons between the pins and ground
HTH
a7
I need to make a scheme on tinkercad.com
I need to make a scheme on tinkercad.com
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.