Please, I need help modifying a code.

Hello friends, I have this code of arduino, 3 buttons on and off the respective LEDs, I need to make led 2 depends on led1, ie it can only be activated if led1 is on, and led3 is on only if led2 is turned on, and when it turns off LED1 all turn off, only powering on again when the respective buttons are pressed. If anyone can help me I appreciate it.

// set pinos
const int buttonPin = 2;                   // número do pino pushbutton
const int ledPin = 3;                        // número do pino LED
const int buttonPin2 = 4;                 // número do pino pushbutton
const int ledPin2 = 5;                      // número do pino LED
const int buttonPin3 = 6;                 // número do pino pushbutton
const int ledPin3 = 7;                      // número do pino LED
 
// set variables
int estado = 0;                                   // variável para leitura do pushbutton
int guarda_estado = LOW;              // variável para armazenar valores do pushbutton
nt estado2 = 0;                                 // variável para leitura do pushbutton
int guarda_estado2 = LOW;            // variável para armazenar valores do pushbutton
nt estado3 = 0;                                // variável para leitura do pushbutton
int guarda_estado3 = LOW;            // variável para armazenar valores do pushbutton
 
void setup() {

pinMode(ledPin, OUTPUT);                                 // define o pino do Led como saída do Arduino
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(buttonPin, INPUT);                                // define pino do pushbutton como entrada do Arduino:
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
}
 
void loop(){

estado = digitalRead(buttonPin);                      // le o estado pushbutton: ligado (HIGH) ou desligado (LOW)
 if (estado == HIGH) {                                      // verifica se o botão (pushbutton) está pressionado
guarda_estado = !guarda_estado;                  // inverte valor da variável variable_buttonEstado
delay(500);                                                       //esperera o tempo de 500ms para evitar que haja várias vezes alterações
}
 if (guarda_estado == HIGH) {
digitalWrite(ledPin, HIGH);                               // liga o led
}
else {
digitalWrite(ledPin, LOW);                                // desliga o led
}

estado2 = digitalRead(buttonPin2);                      // le o estado pushbutton: ligado (HIGH) ou desligado (LOW)
 if (estado2 == HIGH) {                                         // verifica se o botão (pushbutton) está pressionado
guarda_estado2 = !guarda_estado2;                    // inverte valor da variável variable_buttonEstado
delay(500);                                                             //esperera o tempo de 500ms para evitar que haja várias vezes alterações
}
 if (guarda_estado2 == HIGH) {
digitalWrite(ledPin2, HIGH);                                  // liga o led
}
else {
digitalWrite(ledPin2, LOW);                                  // desliga o led
}

estado3 = digitalRead(buttonPin3);                      // le o estado pushbutton: ligado (HIGH) ou desligado (LOW)
 if (estado3 == HIGH) {                                         // verifica se o botão (pushbutton) está pressionado
guarda_estado3 = !guarda_estado3;                   // inverte valor da variável variable_buttonEstado
delay(500);                                                           //esperera o tempo de 500ms para evitar que haja várias vezes alterações
}
 if (guarda_estado3 == HIGH) {
digitalWrite(ledPin3, HIGH);                                  // liga o led
}
else {
digitalWrite(ledPin3, LOW);                                // desliga o led
}

}

< // this line added after being told to do so in a comment below
I haven't actually tried this but consider it. No lo he intentado pero consideralo.

// set pins
const int buttonPin1 = 2; // Pin1's pushbutton
const int buttonPin2 = 4; // Pin2's pushbutton
const int buttonPin3 = 6; // Pin3's pushbutton
const int ledPin1 = 3; // Pin1's LED
const int ledPin2 = 5; // Pin2's LED
const int ledPin3 = 7; // Pin3's LED

// set variables
int ledPin1_status = 0;
int ledPin2_status = 0;
int ledPin3_status = 0;

void setup() { //initialize pins
pinMode(buttonPin1, INPUT); // set PINn as INPUT
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(ledPin1, OUTPUT); // set LEDn as OUTPUT
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
}

void loop() {
if (digitalRead(buttonPin1) == HIGH) { // check if buttonPin1 was pressed (be sure to use a resistor?)
if (ledPin1_status == 0) { // check if ledPin1 was not already pressed
digitalWrite(ledPin1, HIGH); // turn ledPin1 on
ledPin1_status = 1; // 1 = on, set ledPin1_status to 1
delay(500); // wait half a second to avoid blinking
}
else {
digitalWrite(ledPin1, LOW); // turn ledPin1 off
ledPin1_status = 0; // 0 = off, set Pin1_status to 0
digitalWrite(ledPin2, LOW); // turn ledPin2 off
ledPin2_status = 0; // 0 = off, set Pin2_status to 0
digitalWrite(ledPin3, LOW); // turn ledPin2 off
ledPin3_status = 0; // 0 = off, set Pin2_status to 0
delay(500); // wait half a second to avoid blinking
}
}

if (digitalRead(buttonPin2) == HIGH) { // check if buttonPin2 was pressed (be sure to use a resistor?)
if (ledPin1_status == 1) { // check if ledPin1 was already pressed
digitalWrite(ledPin2, HIGH); // turn ledPin2 on
ledPin2_status = 1; // 1 = on, set ledPin2_status to 1
delay(500); // wait half a second to avoid blinking
}
else { // you may not want this "else" code
digitalWrite(ledPin2, LOW); // turn ledPin2 off
ledPin2_status = 0; // 0 = off, set Pin2_status to 0
digitalWrite(ledPin3, LOW); // turn ledPin3 off
ledPin3_status = 0; // 0 = off, set Pin3_status to 0
delay(500); // wait half a second to avoid blinking
}
}

if (digitalRead(buttonPin3) == HIGH) { // check if buttonPin3 was pressed (be sure to use a resistor?)
if (ledPin2_status == 1) { // check if ledPin2 was already pressed
digitalWrite(ledPin3, HIGH); // turn ledPin3 on
ledPin3_status = 1; // 1 = on, set ledPin3_status to 1
delay(500); // wait half a second to avoid blinking
}
else { // you may not want this "else" code
digitalWrite(ledPin3, LOW); // turn ledPin3 off
ledPin3_status = 0; // 0 = off, set Pin3_status to 0
delay(500); // wait half a second to avoid blinking
}
}

}
/> // this line added after being told to do so in a comment below

Please read the "how to use this forum" post and edit your original post accordingly.

delay(500);

This is going to make it very difficult for the Arduino to respond to button pushes. You need to remove these delays.

ronart:
I haven't actually tried this but consider it. No lo he intentado pero consideralo.

// set pins
const int buttonPin1 = 2; // Pin1's pushbutton

Thanks a lot for the answer, and almost what I'm trying to do, but I need the led2 to be turned off by the button2 and the led3 can also be turned off by the button3, and not only when led1 is turned off.

ronart:
if (digitalRead(buttonPin3) == HIGH) { // check if buttonPin3 was pressed (be sure to use a resistor?)
if (ledPin2_status == 1) { // check if ledPin2 was already pressed
digitalWrite(ledPin3, HIGH); // turn ledPin3 on
ledPin3_status = 1; // 1 = on, set ledPin3_status to 1
delay(500); // wait half a second to avoid blinking
}
else { // you may not want this "else" code
digitalWrite(ledPin3, LOW); // turn ledPin3 off
ledPin3_status = 0; // 0 = off, set Pin3_status to 0
delay(500); // wait half a second to avoid blinking
}
}

}

I've duplicated this part of the code with some modifications, and it seems to have worked. It helped me a lot, thank you.

To make it easy for people to help you please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

Your code is too long for me to study quickly without copying to a text editor.

Also please use the AutoFormat tool to indent your code for easier reading.

What happens when you run the program, and how is that different from what you want?

...R

Ah, Robin, a Quick Reply does not show the toolbar at the top (the toolbar with the "< />" symbol) and thus no AutoFormat option either. I'll keep that in mind for future replies (this was my first reply showing code). Future code postings will be done using the "regular" [REPLY] button.

khlandestyno, would you post your latest code? and maybe a restatement of your goal? Gracias de antemano, Thanks In Advance, -- Ronaldo (not Cristiano)

Oops, wait, hitting [Preview] does show the toolbar. ¡Por Dios! Oh Geez!

ronart:
Oops, wait, hitting [Preview] does show the toolbar. ¡Por Dios! Oh Geez!

In my Forum Settings I have selected Use full editor in Quick Reply

...R

Thank you, Robin2. I hovered over my avatar icon at the top right and clicked on Profile from the dropdown menu. Then, clicked on Forum Settings [Edit]. Next, hovering over the Settings tab, I clicked on Look and Layout from the dropdown menu. Finally, I checked the box next to Use Full Editor in Quick Reply at the bottom of the screen, and clicked [Change Profile] to save the new setting.

This reply is being done using Quick Reply which now shows the toolbar with all the options needed.

I think my suggestion about using a resistor is misplaced. A resistor is not needed at the pin; a resistor is needed at the LED. Someone please correct me if this statement is wrong or tell me this is correct. TIA, -- Ron

ronart:
I think my suggestion about using a resistor is misplaced. A resistor is not needed at the pin; a resistor is needed at the LED. Someone please correct me if this statement is wrong or tell me this is correct. TIA, -- Ron

What does it matter where the resistor is?

ronart:
khlandestyno, would you post your latest code? and maybe a restatement of your goal? Gracias de antemano, Thanks In Advance, -- Ronaldo (not Cristiano)

Hello, this is the last code I'm using.
I added one more button and one led.
What I need to do is:
led1 on / off whenever button1 is pressed
led2 on / off whenever button2 is pressed but only if led1 on
led3 on / off whenever button3 is pressed but only if led2 on
led4 on / off whenever button4 is pressed but only if led1 on

Testing in the protoboard is working, but with a slow response when the LEDs are turned off.
I found this very extensive code, I believe it can be improved by making it simpler, but I still do not know how.

// set pins
const int buttonPin1 = 2;                 // Pin1's pushbutton
const int buttonPin2 = 4;                 // Pin2's pushbutton
const int buttonPin3 = 6;                 // Pin3's pushbutton
const int buttonPin4 = 8;                 // Pin4's pushbutton
const int ledPin1 = 3;                    // Pin1's LED
const int ledPin2 = 5;                    // Pin2's LED
const int ledPin3 = 7;                    // Pin3's LED
const int ledPin4 = 9;                    // Pin4's LED
 
// set variables
int ledPin1_status = 0;
int ledPin2_status = 0;
int ledPin3_status = 0;
int ledPin4_status = 0;
 
void setup() {  //initialize pins
 pinMode(buttonPin1, INPUT); // set PINn as INPUT
 pinMode(buttonPin2, INPUT);
 pinMode(buttonPin3, INPUT);
 pinMode(buttonPin4, INPUT);
 pinMode(ledPin1, OUTPUT);   // set LEDn as OUTPUT
 pinMode(ledPin2, OUTPUT);
 pinMode(ledPin3, OUTPUT);
 pinMode(ledPin4, OUTPUT);
}
 
void loop() {
 if (digitalRead(buttonPin1) == HIGH) { // check if buttonPin1 was pressed (be sure to use a resistor?)
   if (ledPin1_status == 0) {           // check if ledPin1 was not already pressed
     digitalWrite(ledPin1, HIGH);       // turn ledPin1 on
     ledPin1_status = 1;                // 1 = on, set ledPin1_status to 1
     delay(500);                        // wait half a second to avoid blinking
   }
   else {
     digitalWrite(ledPin1, LOW);        // turn ledPin1 off
     ledPin1_status = 0;                // 0 = off, set Pin1_status to 0
     digitalWrite(ledPin2, LOW);        // turn ledPin2 off
     ledPin2_status = 0;                // 0 = off, set Pin2_status to 0
     digitalWrite(ledPin3, LOW);        // turn ledPin2 off
     ledPin3_status = 0;                // 0 = off, set Pin2_status to 0
     delay(500);                        // wait half a second to avoid blinking
   }
 }

 if (digitalRead(buttonPin2) == HIGH) { // check if buttonPin2 was pressed (be sure to use a resistor?)
   if (ledPin1_status == 1) {           // check if ledPin1 was already pressed
     digitalWrite(ledPin2, HIGH);       // turn ledPin2 on
     ledPin2_status = 1;                // 1 = on, set ledPin2_status to 1
     delay(500);                        // wait half a second to avoid blinking
   }
   else {                    // you may not want this "else" code
     digitalWrite(ledPin2, LOW);        // turn ledPin2 off
     ledPin2_status = 0;                // 0 = off, set Pin2_status to 0
     digitalWrite(ledPin3, LOW);        // turn ledPin3 off
     ledPin3_status = 0;                // 0 = off, set Pin3_status to 0
     delay(500);                        // wait half a second to avoid blinking
   }
 }

if (digitalRead(buttonPin2) == HIGH) { // check if buttonPin2 was pressed (be sure to use a resistor?)
   if (ledPin2_status == 1) {           // check if ledPin1 was already pressed
     digitalWrite(ledPin2, LOW);       // turn ledPin2 on
     ledPin2_status = 1;                // 1 = on, set ledPin2_status to 1
     delay(500);                        // wait half a second to avoid blinking
   }
   else {                    // you may not want this "else" code
     digitalWrite(ledPin2, LOW);        // turn ledPin2 off
     ledPin2_status = 0;                // 0 = off, set Pin2_status to 0
     digitalWrite(ledPin3, LOW);        // turn ledPin3 off
     ledPin3_status = 0;                // 0 = off, set Pin3_status to 0
     delay(500);                        // wait half a second to avoid blinking
   }
 }
 
 if (digitalRead(buttonPin3) == HIGH) { // check if buttonPin3 was pressed (be sure to use a resistor?)
   if (ledPin2_status == 1) {           // check if ledPin2 was already pressed
     digitalWrite(ledPin3, HIGH);       // turn ledPin3 on
     ledPin3_status = 1;                // 1 = on, set ledPin3_status to 1
     delay(500);                        // wait half a second to avoid blinking
   }
   else {                    // you may not want this "else" code
     digitalWrite(ledPin3, LOW);        // turn ledPin3 off
     ledPin3_status = 0;                // 0 = off, set Pin3_status to 0
     delay(500);                        // wait half a second to avoid blinking
   }
 }
if (digitalRead(buttonPin3) == HIGH) { // check if buttonPin2 was pressed (be sure to use a resistor?)
   if (ledPin3_status == 1) {           // check if ledPin1 was already pressed
     digitalWrite(ledPin3, LOW);       // turn ledPin2 on
     ledPin2_status = 1;                // 1 = on, set ledPin2_status to 1
     delay(500);                        // wait half a second to avoid blinking
   }
   else {                    // you may not want this "else" code
     digitalWrite(ledPin3, LOW);        // turn ledPin2 off
     ledPin3_status = 0;                // 0 = off, set Pin2_status to 0
     digitalWrite(ledPin3, LOW);        // turn ledPin3 off
     ledPin3_status = 0;                // 0 = off, set Pin3_status to 0
     delay(500);                        // wait half a second to avoid blinking
   }
 }
if (digitalRead(buttonPin4) == HIGH) { // check if buttonPin2 was pressed (be sure to use a resistor?)
   if (ledPin1_status == 1) {           // check if ledPin1 was already pressed
     digitalWrite(ledPin4, HIGH);       // turn ledPin2 on
     ledPin2_status = 1;                // 1 = on, set ledPin2_status to 1
     delay(500);                        // wait half a second to avoid blinking
   }
   else {                    // you may not want this "else" code
     digitalWrite(ledPin4, LOW);        // turn ledPin2 off
     ledPin4_status = 0;                // 0 = off, set Pin2_status to 0
     digitalWrite(ledPin4, LOW);        // turn ledPin3 off
     ledPin3_status = 0;                // 0 = off, set Pin3_status to 0
     delay(500);                        // wait half a second to avoid blinking
   }
 }
 if (digitalRead(buttonPin4) == HIGH) { // check if buttonPin2 was pressed (be sure to use a resistor?)
   if (ledPin4_status == 1) {           // check if ledPin1 was already pressed
     digitalWrite(ledPin4, LOW);       // turn ledPin2 on
     ledPin4_status = 1;                // 1 = on, set ledPin2_status to 1
     delay(500);                        // wait half a second to avoid blinking
   }
   else {                    // you may not want this "else" code
     digitalWrite(ledPin4, LOW);        // turn ledPin2 off
     ledPin4_status = 0;                // 0 = off, set Pin2_status to 0
     digitalWrite(ledPin4, LOW);        // turn ledPin3 off
     ledPin3_status = 0;                // 0 = off, set Pin3_status to 0
     delay(500);                        // wait half a second to avoid blinking
   }
 }
}

khlanestyno, thanks for posting your goal, although now the new 4th BUTTON and LED do exactly the same as BUTTON and LED 2. Did you mean that? Or did you mean turn "LED 4 on / off when BUTTON 4 is pressed but only if LED 3 is on"? I'll look at the code with this second way in mind, unless you restate that what your wrote was really what you meant. I'll post the new code soon. -- Ron

p.s., I am assuming that higher numbered LEDs are turned off when lower numbered LEDs are turned off.

khlandestyno, try this. Let me know. -- Ron

// set pins
const int buttonPin1 = 2;                 // Pin1's pushbutton
const int buttonPin2 = 4;                 // Pin2's pushbutton
const int buttonPin3 = 6;                 // Pin3's pushbutton
const int buttonPin4 = 8;                 // Pin4's pushbutton
const int ledPin1 = 3;                    // Pin1's LED
const int ledPin2 = 5;                    // Pin2's LED
const int ledPin3 = 7;                    // Pin3's LED
const int ledPin4 = 9;                    // Pin4's LED
 
// set variables
int ledPin1_status = 0;
int ledPin2_status = 0;
int ledPin3_status = 0;
int ledPin4_status = 0;
 
void setup() {  //initialize pins
 pinMode(buttonPin1, INPUT); // set PINn as INPUT
 pinMode(buttonPin2, INPUT);
 pinMode(buttonPin3, INPUT);
 pinMode(buttonPin4, INPUT);
 pinMode(ledPin1, OUTPUT);   // set LEDn as OUTPUT
 pinMode(ledPin2, OUTPUT);
 pinMode(ledPin3, OUTPUT);
 pinMode(ledPin4, OUTPUT);
}
 
void loop() {
 delay(500); // wait half a second to avoid false repeat triggering

 if (digitalRead(buttonPin1) == HIGH) { // check if buttonPin1 was pressed
   if (ledPin1_status == 0) {           // check if ledPin1 was not already pressed
     digitalWrite(ledPin1, HIGH);       // turn ledPin1 on (be sure to use a resistor)
     ledPin1_status = 1;                // 1 = on, set ledPin1_status to 1
   }
   else {
     digitalWrite(ledPin1, LOW);        // turn ledPin1 off
     ledPin1_status = 0;                // 0 = off, set Pin1_status to 0
   }
 }

 if (digitalRead(buttonPin2) == HIGH) { // check if buttonPin2 was pressed
  if (ledPin1_status == 1) {            // check if ledPin1 is on
   if (ledPin2_status == 0) {           // check if ledPin2 is off
    digitalWrite(ledPin2, HIGH);        // turn ledPin2 on (be sure to use a resistor)
    ledPin2_status = 1;                 // 1 = on, set ledPin2_status to 1
   }
   else {
    digitalWrite(ledPin2, LOW);         // turn ledPin2 off
    ledPin2_status = 0;                 // 0 = off, set Pin2_status to 0
   }
  }
 }

 if (digitalRead(buttonPin3) == HIGH) { // check if buttonPin3 was pressed
  if (ledPin2_status == 2) {            // check if ledPin2 is on
   if (ledPin3_status == 0) {           // check if ledPin3 is off
    digitalWrite(ledPin3, HIGH);        // turn ledPin3 on (be sure to use a resistor)
    ledPin3_status = 1;                 // 1 = on, set ledPin3_status to 1
   }
   else {
    digitalWrite(ledPin3, LOW);         // turn ledPin3 off
    ledPin3_status = 0;                 // 0 = off, set Pin3_status to 0
   }
  }
 }

 if (digitalRead(buttonPin4) == HIGH) { // check if buttonPin4 was pressed
  if (ledPin1_status == 1) {            // check if ledPin1 is on
   if (ledPin4_status == 0) {           // check if ledPin4 is off
    digitalWrite(ledPin4, HIGH);        // turn ledPin4 on (be sure to use a resistor)
    ledPin4_status = 1;                 // 1 = on, set ledPin4_status to 1
   }
   else {
    digitalWrite(ledPin4, LOW);            // turn ledPin4 off
    ledPin4_status = 0;                      // 0 = off, set Pin4_status to 0
   }
  }
 }

}

TolpuddleSartre, LEDs can only take so much current, this I have read in several places. Resistors limit that current, keeping the LED from burning out. Now, what value resistor? That depends on the LED's rating. I'm not the expert and there are plenty of sources on this, so "Giggle" it. -- Ron

ronart:
TolpuddleSartre, LEDs can only take so much current,

I think you have missed the point @TolpuddleSartre was making.

...R