button problem

i made this code but it doesn’t seem to work, anything in mind what could be wrong ?

const int buttonPin = 28;
const int ledPin =  9;

int buttonState = 0;

void setup() {
  pinMode(ledPin, OUTPUT);      
  pinMode(buttonPin, INPUT);     
}

void loop(){
  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH) {     
    digitalWrite(ledPin, HIGH);
    delay(1000);
    digitalWrite(ledPin, LOW);
  } 
  else {
    digitalWrite(ledPin, HIGH);
    delay(5000);
    digitalWrite(ledPin, LOW);
  }
}

Hi bogdan666,

Would it be possible to elaborate as to what exactly does not work in the code you have provided ?
Any error messages ?

dan

no error mesages. So what i want is actually 2 pins input and only one output, if first pin gets 5 volts then the led should light up for 2 sec, if the second pin gets 5 volts it should light up for 5 seconds.

i tried to make it different but it still doesn't work
i am a level lower beneath beginer so, sorry for this code :expressionless:

const int pin1 = A5;
const int pin2 = A4;
const int ledPin =  9;

int buttonState = 0;

void setup() {
  pinMode(ledPin, OUTPUT);      
  pinMode(pin1, INPUT); 
  pinMode(pin2, INPUT);
}
void loop(){
  buttonState = digitalRead(pin1);
  if (buttonState == HIGH) {     
    digitalWrite(ledPin, HIGH); 
   delay(2000); 
  } 
  else {
    digitalWrite(ledPin, LOW); 
  }
  {
    buttonState = digitalRead(pin2);
  if (buttonState == HIGH) {     
    digitalWrite(ledPin, HIGH); 
   delay(5000); 
  } 
  else {
    digitalWrite(ledPin, LOW); 
  }
}

In order to cover all conditions, would it be possible to indicate what should happen if both switches are turned on ?
May I suggest, that both led's should be flashing while both switches are activated ?

  • dan

Hi,

When using delay() the arduino stops doing things (i.e.: reading button's state). Could this be your problem?

Regards

I think the OP should elaborate with a few more details. "It doesn't work" could mean a lot of things. What was the expectation? What actually happens?

sorry, my bad, it works. :expressionless: if i am already here can i ask another question ?
Can i make 2 led's light up with only one input ? So if i press once led no. 1 light up for a number of seconds and if i press it again, led no. 2 light up, like you woud do with a garage door with a single button :slight_smile:

Glad that your code works. I hope it is using millis and that your sketch is based on the "Link an LED without delay" code example! .

you can make the led's turn on based on anything !

You can definiAll if you need to is to define clearly the conditions upon which they will turn on.

Try and write the code as if you were the microcontroller chip yourself.
Attempt to write "pseudo code" in your "real words"

"what if this switch is turned on, " : turn on LED ?

it's kind of hard for me to expres myself, english beeing my second language, my code is actually the "button example" from arduino program.
what i want for my second code is:
button pressed=light up led no 1 for 5 seconds
button pressed=light up led no 2 for 2 seconds
button pressed=light up led no 1 for 5 seconds
button pressed=light up led no 2 for 2 seconds...

so no matter how many times i press the button it will allways light up the otter led.

bogdan666:
what i want for my second code is:
button pressed=light up led no 1 for 5 seconds
button pressed=light up led no 2 for 2 seconds
button pressed=light up led no 1 for 5 seconds
button pressed=light up led no 2 for 2 seconds...

And what if button is pressed 3 times in 1 second ?

You need to be a little more specific. Your English is fine but you have not given us enough detail to understand what you want the Arduino to do.

Let's split "button pressed" into two actions - the button is pressed and held down is one action and the button release is the second action.

button pressed - start a timer, light up LED1
button released - do nothing
wait until the timer measures 5 seconds, then turn LED1 off.
button pressed - remembering that we have just done LED1, start another timer and light up LED2
button released - do nothing
wait until the timer measures 2 seconds, then turn LED2 off

The key here is "remembering" that you have done LED1 and LED2 will be the next one to light.

Even doing it this way, there's not enough information to write a program yet. What if the button is initially held down for 6 seconds? Do you want LED1 to stay lit for 6 seconds? Do you want it to stay lit for 6 seconds PLUS a further 5 seconds after that?

What if I push the button twice during the first 5 seconds? Does LED2 start its 2-second time while LED1 is still lit?

The way out of this mess is to draw a diagram. Use actual pen and paper. Draw a circle at the top and write "Start" in that circle. What are the things that might change to move the Arduino out of the "Start" state? The button becoming pressed is the obvious one. Then you draw an arrow to a new circle labelled "LED1 on" and you label the arrow "button pressed."

So now how does the Arduino get to the next state? What causes it to leave the "LED1 on" state and move on? In your description you have only specified "5 seconds" so the arrow leading out of "LED1 on" should be labelled "5 seconds." The next state should be labelled "LED1 off". In this state, it's waiting for the button push which will move it on to the "LED2 on" state.

Once you've got the basic states drawn on the page, then you will find you need to add more arrows and possibly more states. Even a very simple thing like this could have 20 states if you allow more than one LED to be on at a time.

let's assume that you can not press the button only for 1 second, if it is pressed again in the time that the led is light up it will do nothing, once the button is pressed the led will light up for 5 seconds and there is nothing to stop it, led 2 can not be light up until led 1 is off. So in the end only one led can be light up, either 1 or 2 but not both.

So ? Any ideea ?

I'm sure I will get balked at about suggesting this, but if you don't need anything else to happen during the time the led is lit up, delay() is perfectly acceptable in my opinion. Button presses, as well as any other input, will be missed until after the led on period has passed and the loop will move on to the next function/iteration from there.

to all the haters, while blink without delay is great, I think in this instance OP can shorten their code
and achieve the desired results.

Just my 2 cents,

Bill

I'm waiting to see the state diagram or a description that fully defines the actions of the system. You can't just say "1 and then 2 and then the others the same." The whole thing needs to be written out.

i don't understand what else do you need, i only want one button and two led's wich can never be light up at the same time and always one after another (at the press of the buttonof course).