led is not staying on

hi, i got some code for my arduino and it works as intended.. kind of. i have two leds connected and when the switch is is off the green is on and the red is off. when the switch is pressed the green turns off and the red turns on until the switch is released. When it is release the green turns on and the red turns off. The problem is that i want the red to stay on when the switch is released and the green to turn off until i press it again at witch point the green turns on and the red off. And then just loop that. Could someone be kind enough to tell me what code to change or better yet, change it and repost it to me? I'm using a momentary switch btw. cheers!

here is the code

/*

*/

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPingreen = 9; // the number of the LED pin
const int ledPinred = 8;

// variables will change:
int buttonState = 0; // variable for reading the pushbutton status

void setup() {
// initialize the LED pin as an output:
pinMode(ledPingreen, OUTPUT);
pinMode(ledPinred, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}

void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == LOW) {
// turn LED on:
digitalWrite(ledPingreen, LOW);
digitalWrite(ledPinred, HIGH);
} else {
// turn LED off:
digitalWrite(ledPingreen, HIGH);
digitalWrite(ledPinred, LOW);
}
}

  if (buttonState == LOW) {
    // turn LED on:
    digitalWrite(ledPingreen, LOW);
    digitalWrite(ledPinred, HIGH);   
  } else {
    // turn LED off:
    digitalWrite(ledPingreen, HIGH);
    digitalWrite(ledPinred, LOW);   
  }

try

if (buttonState == LOW) {
// turn LED on:
digitalWrite(ledPingreen, !digitalRead(ledPingreen));
digitalWrite(ledPinred, !digitalRead(ledPinred));
    delay(500);
}

Add
digitalWrite(ledPingreen, HIGH); to setup

can you write everything in one code so i can just copy and paste? i suck at programming

“ can you write everything in one code so i can just copy and paste?”

OMG !

im a singer/songwriter, just need it to work with my pedal board, so I'm sorry if i can't program mate, bet you can't play a major scale

could not get it to work:/

patricklou15:
could not get it to work:/

Show us your attempt.

Use CTRL T to format your code.
Attach your ‘complete’ sketch between code tags
[code]Paste your sketch here[/code]

patricklou15:
im a singer/songwriter, just need it to work with my pedal board, so I'm sorry if i can't program mate, bet you can't play a major scale

So I guess you will write me a catchy song if I edit it? And yeah, I will complain if the song isn't to my liking.

But really, our aim here is to help people understand programming Arduino, to aid them in there project. Not to do there project for them. So no, I can't play a major scale, that's why I don't attempt it. And if it was something I would like to be able, I would just spend a lot of time learning it :slight_smile:

I can play a major scale, I can even play a melodic minor. And I can program. I had to learn all of them. Learning is good for you.

Give it a try. Post your attempt and we'll help further from there.

Steve

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