I'm pretty good with electronics but my programming skills are pretty basic. I want to be able to use one button to turn on two relays but I want one relay to stay on while the button is latched and the other relay to turn off after one second.
const int buttonPin = 2; // the number of the pushbutton pin
//const int buttonPin2 = 3; // the number of the pushbutton pin
const int relay1 = 12; // the number of the LED pin
const int relay2 = 11; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
const long interval = 1000;
void setup() {
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(buttonPin, INPUT);
//pinMode(buttonPin2, INPUT);
}
void loop() {
// read the state of the pushbutton value:
unsigned long currentMils = millis();
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
if (currentMils = interval) {
digitalWrite(relay2, LOW);
}
} else {
// turn LED off:
digitalWrite(relay1, LOW);
}
}
const int buttonPin = 2; // the number of the pushbutton pin
//const int buttonPin2 = 3; // the number of the pushbutton pin
const int relay1 = 12; // the number of the LED pin
const int relay2 = 11; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
const long interval = 1000;
void setup() {
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(buttonPin, INPUT);
//pinMode(buttonPin2, 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 LEDs on:
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
// Timeout
delay(interval);
// Back off
digitalWrite(relay2, LOW);
}
else
{
// turn LED off:
digitalWrite(relay1, LOW);
}
}
Looks like if you keep the button down, the relay that is supposed to turn off after some time will be continuously retriggerable, perhaps just appearing to stay on.
Yup, that's expected. Hence my comment that it's not perfect, just improved. Incomplete requirements that require guessing at what the poster wants are ignored.
@autonomousKaos you picked a good problem for running into some basic issues with doing things because a button got, or stayed, pressed.
I usually recommend that noobs take the trouble to learn how to debounce a pushbutton, and recognize and act when it gets pushed, not because it is (still) being pushed.
And of course you need to master the blink without delay paradigm.
For the button handling, I post a sketch showing how a library, here ezButton, can be used to solve up a bigger half of this hole problem.
I can't test this - right now the power is blinking on and off due to who knows why? and I am leaving the big rig off until this settles.
But close:
// not cheating - use button library
# include <ezButton.h>
// wire the button between the pin and ground
ezButton myButton(2); // ez button on pin 2
void setup() {
Serial.begin(115200);
Serial.println("Easy ezButton Examlpe"):
myButton.setDebounceTime(20);
}
void loop() {
myButton.loop(); // check on what's up with the button
if (myButton.isPressed()) {
Serial.println("I see the button go down");
}
if (myButton.isReleased()) {
Serial.println("I see the button go up");
}
}
Imma bet you can use that example and figure out where you might add statements to turn on and off relays, and start and check timers.
This is a reply to all, I'm sharing my tinkercad project and you can try you're changes on it and let me know if you get anything different from my experience. Just to clarify, I want the LED to stay on while the button is latched and the piezo busser to come on at the same time but turn off after 1 second. Thank you both for your help, Your ideas are helpful with my thought process.
It may be that your original sketch behaves the same as an offered "improvement".
Yours would have the output you want following the pushbutton doing just that, or appearing to, and the other output would have been rapidly switching between HIGH and LOW, being HIGH for considerably longer than LOW and thus too appearing to be on alla time.
Rather than have us try our changes on your code, it would make more sense for you to apply our advices to your next steps, be they tinkering with the code or reading about
Let's say we're playing hokey and you hit a guy in his face with you're stick. The ref gives you a penalty and the penalty light and a buzzer come on at the same when the ref pushes his pause button. I don't want the buzzer to be on the whole length of time he's pressing pause button.
Thank you everyone for the help @jimLee that was exactly what I was looking for now I get to do research on callback functions @paulpaulson that example is a little above my level but thank you and I will definitely be taking that on in the near future, again thank you all for the help