Hi, I'm trying to create a system comprising 2 push buttons. When PB 1 = HIGH && PB2= LOW, the LED should light up for 1s and then turn back to LOW. This is my existing code :
const int buttonPin1 = 2;
const int buttonPin2 = 3;
const int ledPin = 11;
unsigned long buttonState1 = 0;
unsigned long buttonState2 = 0;
unsigned long previousMillis = 0;
const long interval = 999;
int ledState = 0;
unsigned long currentMillis = 0;
This won't work because as soon as this if statement is not true you will set the pin LOW again.
You need to change your logic so that you have 2 states... representing when the led is On and when it is Off (can use a boolean ledOn). The timer only runs when on. You only check the buttons when off.
Hi, I tested out this code. Turns out that even for other false conditions (eg: Both buttons HIGH) the LED is lighting up for a little bit.
In addition, when the condition is true, buttonState1 == HIGH && buttonState2 == LOW, the LED is HIGH for all time(I need it to be HIGH for 1sec). When I make buttonState1 == LOW && buttonState2 == LOW, only then the LED is HIGH for 1sec and then goes LOW(which is incorrect for this LOW&&LOW condition). How can we solve this?
const int buttonPin1 = 2;
const int buttonPin2 = 3;
const int ledPin = 11;
unsigned long buttonState1 = 0;
unsigned long buttonState2 = 0;
unsigned long previousMillis = 0;
const long interval = 999;
int ledState = 0;
unsigned long currentMillis = 0;
Hi, verified my wiring, but here's what I found. At all times when the buttonState1 == HIGH && buttonState2 == LOW, the serial monitor prints millis value for 1 sec, but it continues. Is this the reason why the LED is always HIGH? If yes, how can we troubleshoot this?
If ((buttonState1 == HIGH && buttonState2 == LOW))
...then this code will continue to execute...
So it will be high for 1 second... check the buttons again... and if the statement above is still true then it will set high for another second.... and so on.
your truth table is incorrect. You have 0,0 repeated, and no 1,0 entry. I think it should be 3).
as I said in the previous post. If 3) is true then the LED will be on for 1 second. Then it will check the values again... if 3) is still true it will light the Led for a further second... this will continue until 3) is not true.
Yes now I understand the logic where it checks the button state which causes the led to be continually high as long as the condition is true.
On the other case, this is the flow of event that I want to achieve. That is why 0,0 is repeated. Can we produce a code which complies to my truth table, or is it impossible to create a logic based on it?
Okay, now I have revised my logic to create a possible one. This is the mission I want to achieve.
When red tower light is HIGH, S3 is HIGH and S1 is LOW. Then, the machine operator will push the pneumatic cylinder rod till it reaches the S1 position. When the position returns to HOME, which is S1 HIGH and S3 LOW, then the red tower light will become LOW.
At this point, the machine operator will press the reset button on the machine to reset the position of the pneumatic cylinder, which is controlled by 2 solenoid valves(not related to our case)
During reset is being triggered, a 1s pulse should be sent from Nano to the control box. Inside it there's a latching circuit using a 24V relay. That 1s pulse will then reset the relay.
Right now, without Nano, the machine is working by the operator pressing reset twice, once in the machine and once in our control box. Goal of this mission is to use 1 reset (on the machine) to reset both machine and our control box.
Not sure you understand the concept. You have 3 inputs, you should have 8 combinations of inputs. None of the combinations would be the same (like Seq2 & Seq 4 above).
// Pin 5 has an LED connected on most Arduino boards.
const int led = 5;
const int S1 = 2; // the number of the pushbutton pin
const int S3 = 3;
const int red = 8;
const int green = 11;
// delay in milliseconds between blinks of the LED
unsigned long previousMillis = 0;
const long interval = 1000;
int buttonState = 0; // variable for reading the pushbutton status
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int lastButtonState = 0; // previous state of the button
int lastButtonState2 = 0; // previous state of the button
int lastButtonState3 = 0; // previous state of the button
int lastButtonState4 = 0; // previous state of the button
// state of the LED = LOW is off, HIGH is on
boolean activate = false,flag1,flag2,flag3;
void setup()
{
// initialize the digital pin as an output.
Serial.begin(9600);
pinMode(led, OUTPUT);
pinMode(S1, INPUT);
pinMode(S3, INPUT);
pinMode(red, INPUT);
pinMode(green, INPUT);
previousMillis = 0;
}
void loop()
{
buttonState = digitalRead(S1);
buttonState2 = digitalRead(S3);
buttonState3 = digitalRead(green);
buttonState4 = digitalRead(red);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH)
{
Serial.println("s1:on");
flag1=true;
}
else
{
Serial.println("s1:off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
// compare the buttonState2 to its previous state
if (buttonState2 != lastButtonState2) {
// if the state has changed, increment the counter
if (buttonState2 == HIGH)
{
Serial.println("s3:on");
}
else
{
Serial.println("s3:off");
if (flag1) flag2=true;
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastButtonState2 = buttonState2;
/**/
// compare the buttonState2 to its previous state
if (buttonState4 != lastButtonState4) {
// if the state has changed, increment the counter
if (buttonState4 == HIGH)
Serial.println("red:on");
}
else
{
Serial.println("red:off");
}
// Delay a little bit to avoid bouncing
delay(50);
// save the current state as the last state, for next time through the loop
lastButtonState4 = buttonState4;
/**/
// compare the buttonState2 to its previous state
if (buttonState3 != lastButtonState3) {
// if the state has changed, increment the counter
if (buttonState3 == HIGH)
{
Serial.println("green:on");
if (flag2)
{
activate = true;
digitalWrite(led, HIGH);
previousMillis = millis();
}
}
else
{
Serial.println("green:off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastButtonState3 = buttonState3;
if(activate)
{
// check 1 sec
if(millis()- previousMillis > interval)
{
digitalWrite(led, LOW);
activate = false;
flag1=false;
flag2=false;
}
}
}