2 LED & 2 Button on the same time

Hello, im doing a project now
So the case is I have 2 buttons on pin 5 and pin 6, and 2 led lights on pin 8 and pin 9 (Button 5 to control led on pin 9, and button 6 to control led on pin 8)
So i make when :

  1. Button 5 is pressed, then after 2 seconds the led on pin 9 will be on for 5 seconds.
  2. Button 6 is pressed, then after 1 seconds the led on pin 8 will be on for 8 seconds.

But when i press button 5 and 6 together, the led on pin 8 is need to wait for led 9 turn off and led 8 can turn on

Now, I to make when buttons 5 and 6 are pressed together, led on pin 8 & 9 can be working at the same time without having to wait for led on pin 9 to turn off first.

This is my code now

  if (digitalRead(5) == HIGH)
   {delay(2000);
    digitalWrite(9,HIGH);
    delay(5000);
    digitalWrite(9,LOW);
    Serial.println();       }
    
  if (digitalRead(6) == HIGH)
   { delay(1000);
    digitalWrite(8,HIGH);
    delay(8000);
    digitalWrite(8,LOW);
    Serial.println();       }

}

Please help me thankyou

@pokedna Installation and trouble shooting is for getting the Arduino system running it is not for your project questions. Please read getting the most out of this forum sticky post.

Therefore I have moved your post to a more appropriate place.

You need to post a schematic of your circuit and post ALL your code.

Then you need to implement your code as a state machine. See the blink without delay in the examples section of the IDE. This question gets asked several times a day so there are plenty of examples to look at.

Yeah i already try to use millis, but it always fail to do my project. Do you have any advice?

Hi,
Can you please post your complete code?
To add code please click this link;

How have you got your buttons wired to the Arduino?
What model Arduino are you using?
Have you written some simple code to make sure your buttons and LEDs work?

Can you please post a circuit diagram of your project, not a Fritzy image?

A picture of your project would help also.

Thanks.. Tom.. :grinning: :+1: :coffee: :australia:

Im using arduino Uno, and i already try it and buttons and led are work with this code, but it need to waiting for delay

void setup() {
  Serial.begin(9600);
  pinMode(5,INPUT);
  pinMode(6,INPUT);
  pinMode(8,OUTPUT);
  pinMode(9,OUTPUT);
}

void loop() {
  if (digitalRead(5) == HIGH)
   {delay(2000);
    digitalWrite(9,HIGH);
    delay(5000);
    digitalWrite(9,LOW);
  }
    
  if (digitalRead(6) == HIGH)
   { delay(1000);
    digitalWrite(8,HIGH);
    delay(8000);
    digitalWrite(8,LOW);
   }

}

And this is the circuit diagram

Hi,
Do you have current limit resistors in each LED circuit?
The way you have them now will overload the UNO output pins.

Are you running this code on a simulator or have you build the project in real life?

Thanks.. Tom... :grinning: :+1::coffee: :australia:

Yea i will use resistor on the LED on real life

i use simulator for trying to make when buttons 5 and 6 are pressed together, led on pin 8 & 9 can be working at the same time without having to wait for led on pin 9 to turn off first.
I think i need to use millis function, but i dont understand how to edit my code into millis

Hi,
Which simulator?

Tom... :grinning: :+1: :coffee: :australia:

Tinkercad

Can you help me to edit my code to use a millis code?

Why not post your code from when you tried to use miilis() and we can start from there?

If you haven't already, you need to check BlinkWithoutDelay from the IDE and also Demonstration code for several things at the same time

Steve

Hello
This is a nice task to step to OOP.
Design an object, using struct, to organize a common data structure for button, LED and time information. A simple method called time manager will take care about the timing.
Have a nice day and enjoy coding in C++.

In real life you can't press two buttons together. There will always be a time difference between the two signals.

Yes, read the tutorials.
See my
http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html
Or Robin2's several things at once
http://forum.arduino.cc/index.php?topic=223286.0

Yup, i have an example to explain this, actually i want to make that without any delay, i will try to read your tutorial. Thanks

"Newbies" - just as likely following the truly awful on-line "tutorials" - tend to wire switches to the 5 V line and use pull-down resistors but good engineering practice is to connect the buttons to ground and use pull-up resistors if the internal pull-ups using pinMode of INPUT_PULLUP are not adequate.

unsigned long waktuAwal = 0;
unsigned long waktuAwal2 = 0;

int LEDState = LOW; //initial state of led
int LEDState2 = LOW; //initial state of led

void setup() {
  Serial.begin(9600);
  pinMode(9, OUTPUT);
  pinMode(5, INPUT_PULLUP);
  pinMode(8, OUTPUT);
  pinMode(6, INPUT_PULLUP);
 }

void loop() {

  
  if (digitalRead(5) == HIGH) {   
       	unsigned long waktuSekarang = millis ();
    if( (waktuSekarang - waktuAwal) >= 0) {
      LEDState = HIGH;
       digitalWrite(9,LEDState);
      waktuAwal = waktuSekarang;}
  }
  if(LEDState == HIGH) {
    unsigned long waktuSekarang2 = millis ();
    if( (waktuSekarang2 - waktuAwal) >= 200) {
      LEDState = LOW;
       digitalWrite(9,LEDState);
      waktuAwal = waktuSekarang2;}
  }
  
    if (digitalRead(6) == HIGH) { 
          unsigned long waktuSekarangg = millis ();
    if( (waktuSekarangg - waktuAwal2) >= 0) {
      LEDState2 = HIGH;
       digitalWrite(8,LEDState2);
      waktuAwal2 = waktuSekarangg;}
  }
  if(LEDState2 == HIGH) {
    unsigned long waktuSekarang3 = millis ();
    if( (waktuSekarang3 - waktuAwal2) >= 200) {
      LEDState2 = LOW;
       digitalWrite(8,LEDState2);
      waktuAwal2 = waktuSekarang3;}
  }
 
}

I try to make it, but i don't know how to make a delay after i click the button, if i set the ">= 0" to ">= 200"
It's doesn't work too, and i need to press button for 2 second to make it active

Do you have any advice?

Ohh.. Okay thankyou
Acctually i just simulate the machine with button, on the real machine i will use barcode scanner to control a LED and a servo. But the task is similar like my button task

Yes, this is because you are not doing it correctly.

This will keep on setting the waktuSekarang variable to the current millis time for as long as the the button is being pressed. This means that the code

Will never be run, because once the button ceases to be pressed that section of code can no longer be reached.

What you need to do is to only set the waktuSekarang variable when the button becomes pressed for the first time. This is shown in the "State Change" example in the IDE. Basically you need to have a variable that holds the last state of the push button and reset the variable waktuSekarang is high AND the last time you looked it was low.
So:-

currentState5 = digitalRead(5);
if (currentState5 == HIGH && lastState5 == LOW) {   
   waktuSekarang = millis (); // note do not define this variable here as it will be wiped out the next time round the loop, it should be defined globally
} // end of test for button becoming pressed
lastState5 = currentState5; // for next time round the loop

if( (millis() - waktuSekarang >= waktuAwal) ) { // now when the required amount of time has passed do stuff
      LEDState = HIGH;
       digitalWrite(9, LEDState);
      // waktuAwal = waktuSekarang;} // waktuAwal should be the length of delay you want so you should not change it here so remove this line
  }

You make the same mistakes in the code that follows this, so waktuSekarang2 should be declared as a global variable and the if statement should test a lastLEDState variable along with only setting the waktuSekarang2 variable and nothing else. Then the test

should be changed to reflect the way we did the comparer in the first part of the code with the start value being subtracted from millis and seeing if it is greater than the time you want.

Not good enough, you will read my tutorial, or one of the many others on the topic and you will keep reading it until you understand what is going on. This is not simple stuff but if you want to do what you say you do then you have to learn this stuff.

if (digitalRead(5) == HIGH)
   {delay(2000);
    digitalWrite(9,HIGH);
else{
    delay(5000);}

    digitalWrite(9,LOW);
    Serial.println();       }
    
  if (digitalRead(6) == HIGH)
   { delay(1000);
    digitalWrite(8,HIGH);
else{
    delay(8000);

    digitalWrite(8,LOW);
    Serial.println();       }

}

Try this.
Actually it's programming logic.
In original code it's never possible to two led glow at same time. Because after only first led turned off second button is checked.

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