Go Down

Topic: Multiple leds on button command (Read 1 time) previous topic - next topic

AlexanderG

Hi, what is wrong with this code, i don't understand it. Can somebody help me?



\\
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;
const int ledpin =  12;

// the number of the LED pin

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

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, 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 == HIGH) {     
    // turn LED on:   
    digitalWrite(ledPin, HIGH); 
  }
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}
\\

Thanks ;)

PaulS

Quote
Hi, what is wrong with this code, i don't understand it.

What does it do that you don't want? What does it not do that you do want?

Code: [Select]
const int ledPin =  13;
const int ledpin =  12;

You can't have two variables with the same name.

There is no digitalWrite() statement in setup() to turn on the pullup resistor on the switch pin. This means that you need external pull-up or pull-down resistors. The check for HIGH means that you need an external pull-down resistor with the switch. Do you have one?

AlexanderG

Well no, i don't think so. I undestand that you can't have two same varibles in the same line.
I am not so good at this so i have made i knew code here, but that doesent work either.
What am i doing wrong here???

*
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin
const int ledPin2 = 12;
// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);     
  pinMode(ledPin2, 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 == HIGH);

digitalWrite(ledPin, HIGH); {
delay (1000);
digitalWrite(ledPin2, HIGH);
delay (1000);
}
else {

delay(10000);

digitalWrite(ledPin, LOW);
digitalWrite(ledPin2, LOW);
}

}

*

AlexanderG

its says " 'else' without a previous 'if' ". But the thing is that i already have an 'if' command before the 'else'
command. So what is wrong with it???

I want to be able to push on the button so that two leds will be activated, and after an 10 sec they will both shut down automatically. If can get this code to work i would be able to get an watch timer in it.

please help me...

MHDfo

The opening bracket is in wrong place :

if (buttonState == HIGH){

digitalWrite(ledPin, HIGH);
delay (1000);
digitalWrite(ledPin2, HIGH);
delay (1000);
}
else {

delay(10000);

digitalWrite(ledPin, LOW);
digitalWrite(ledPin2, LOW);
}

}


instead of



if (buttonState == HIGH);

digitalWrite(ledPin, HIGH); {
delay (1000);
digitalWrite(ledPin2, HIGH);
delay (1000);
}
else {

delay(10000);

digitalWrite(ledPin, LOW);
digitalWrite(ledPin2, LOW);
}

}

MHDfo

If you put an ";" at the end of a an If instruction line it "close" the if else structure. So in your code, the Else is not link to the previous if.

AlexanderG

thanks alot, the code is working but the leds wont start like they are suppose to do, maybe i have done it wrong somehow, but thanks anyway. ;)

PaulS

If you comment out (or remove) all the code dealing with the switch(es), do the LEDs work? If so, the switches or the code dealing with the switches are the problem. If not, the wiring of the LEDs is a problem.

When developing hardware and software, it is a good idea to develop a simple sketch to validate that the hardware is working, before trying to develop the application that uses the hardware.

Write a sketch to light the LED. Write a sketch to test the switch. When they both work, then you can combine them.

Go Up