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 :
Button 5 is pressed, then after 2 seconds the led on pin 9 will be on for 5 seconds.
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.
@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.
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?
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
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++.
"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
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.
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.