i really need help

hello, im working on a program that does 3 things.
the first one is to turn on my pc after 5 seconds when the arduino gets power(one cable goes to 3.3v and the other to pin 13). this works fine. i have added a switch(called dip in the code) to this too so i can turn off the pc, have not tested it yet but it should work. there is also an led (called ledbutton in the code) that shows when the button is pressed.

the second part is to turn off an led with a switch, the led needs to be on when the arduino gets power and then i want to be able to turn it off or on when i press it. this does not work at all, not matter how i press the button the led does not turn off. the led in question is just called ledh and is connected to ground on -. the button to switch it is called button.

the third part of the program sort of works but has some flaws. i want the code to switch between two leds (A/B switch). the leds are called leda and ledb in the code and are connected on + to 5v and to their pins on the - side.
the first flaw is that i want the leds to switch when the button is pressed DOWN, not when its going up.(the button used is called secondbutton in the code) im not a programmer so i dont know how to do this, i surprised i even were able to write the code i did. the second flaw is the even if i write in the void setup "digitalWrite(leda, HIGH); digitalWrite(ledb, HIGH); the leds still turn on? this is not a big problem as they need to be on but im just curious as to why this is.

the last question i have is that i used the same code for part 2 and 3 and part 3 switches the leds but part 2 does not turn the led off. if anyone could shed some light on why i would be thankful.

so just to put all questions up so you dont have to read it all again:
why does ledh not turn off and on?
how do i set the leds to switch and stay switched when the button is pressed down?(so i want a toggle switch that switches the leds when its high)
Why does the code work for part 3 but not part 2 when its the same cde just with different “names” attached to them?

i would be grateful if you could help me edit the code to make it work as im not a programmer and have been stuck on this for hours.

here is the code:

int button=2;
int secondbutton=3;
int value;
int power;
int stateh=0;
int ledh=8;
int statea=0;
int leda=9;
int ledb=10;
int buttonhnew;
int buttonhold=1;
int buttonanew;
int buttonaold=1;
int dip=4;
int voltage;
int on=13;
int ledbutton=7;

void setup() {
// put your setup code here, to run once:
pinMode(button, INPUT_PULLUP);
pinMode(secondbutton, INPUT_PULLUP);
pinMode(dip, INPUT_PULLUP);
pinMode(ledh, OUTPUT);
pinMode(leda, OUTPUT);
pinMode(ledb, OUTPUT);
pinMode(on, OUTPUT);
pinMode(ledbutton, OUTPUT);
digitalWrite(dip, HIGH);
digitalWrite(ledh, HIGH);
digitalWrite(leda, HIGH);
digitalWrite(ledb, HIGH);
digitalWrite(on, HIGH);
delay(5000);
digitalWrite(ledbutton, HIGH);
digitalWrite(on, LOW);
delay(1000);
digitalWrite(ledbutton, LOW);
digitalWrite(on, HIGH);
Serial.begin(9600);

}

void loop() {
// put your main code here, to run repeatedly:
value=digitalRead(button);
Serial.print(“harmony”);
Serial.println(value);
power=digitalRead(secondbutton);
Serial.print(“A/B”);
Serial.println(power);
voltage=digitalRead(dip);
Serial.print(“pc jumper”);
Serial.println(voltage);
buttonhnew=digitalRead(button);
if(buttonhold==0 && buttonhnew==1){
if(stateh==0){
digitalWrite(ledh, HIGH);
stateh=1;
}
else{
digitalWrite(ledh, LOW);
stateh=0;

}
}
buttonaold=buttonanew;
buttonanew=digitalRead(secondbutton);
if(buttonaold==0 && buttonanew==1){
if(statea==0){
digitalWrite(leda, LOW);
digitalWrite(ledb, HIGH);
statea=1;
}
else{
digitalWrite(leda, HIGH);
digitalWrite(ledb, LOW);
statea=0;

}
}
buttonaold=buttonanew;
if(digitalRead(dip) == LOW
) {
digitalWrite(ledbutton, HIGH);
digitalWrite(on, HIGH);
delay(1000);
digitalWrite(ledbutton, LOW);
digitalWrite(on, LOW);
}

}

combinedardo.ino (1.82 KB)

  buttonaold = buttonanew;
  buttonanew = digitalRead(secondbutton);

Should it perhaps be

  buttonhold = buttonhnew;
  buttonanew = digitalRead(secondbutton);

Please edit your initial post to enclose the code in code tags.

  buttonaold = buttonanew;

is duplicated a few lines later.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.