Thanks for your time, I'm new too Arduino and really enjoying learning about all it can do, but im still pretty dumb and green. I've read some forums and tired a few things I found but haven't fix my issues yet.
First I have a Arduino MKR Wifi 1010 with a RELAY Shield. I have the Button connected to A1 on the Shield and LED Lights on Relay 1 and the Button Light on Relay 2 (On all the time for now). I'm using the A1 due to it being a terminal block so when the box is moved it wont get messed up.
Can you help make this code better if it needs, also it will randomly trigger itself to turn the relay on and I can't figure out why. I tried using the Serial Plotter to see if it the button cause it but it didnt look like it.
Thanks for your help. Here is the code :
#define BUTTON_PIN A1
byte lastButtonState = LOW;
byte ledState = LOW;
int led = 1;
int status = false;
void setup() {
// Initialize serial and wait for port to open:
Serial.begin(9600);
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(500);
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(1, OUTPUT);
pinMode(2, OUTPUT);
digitalWrite(2, LOW);
// Defined in thingProperties.h
}
void loop() {
if (digitalRead(BUTTON_PIN) == true) {
status = !status;
digitalWrite(led, status);
} while(digitalRead(BUTTON_PIN) == true);
delay(50);
}
you did not declare relays 1 and 2, I put them on pins 2 and 3 (you can change it)
when you declare a pin as input with pullup, it is read as 1 when the button is off, and 0 when the button is pressed (tne button must be tied to the Ground);
I added the activation of relay 2 as long as the button is pressed;
the delay(50) is not needed
If this helps you...
#define BUTTON_PIN A1 // PIN A1
#define RELAY_1 2 // PIN 2
#define RELAY_2 3 // PIN 3
boolean status;
void setup() {
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(RELAY_1, OUTPUT);
pinMode(RELAY_2, OUTPUT);
digitalWrite(RELAY_1, LOW);
digitalWrite(RELAY_2, LOW);
}
void loop() {
if (digitalRead(BUTTON_PIN) == false) { // BUTTON_PIN is pressed, value is LOW (or false)
status = !status; // status is inverted
digitalWrite(RELAY_1, status); // RELAY_1 is changed
while(digitalRead(BUTTON_PIN) == false) digitalWrite(RELAY_2, true);// while BUTTON_PIN is pressed, RELAY_2 is set
digitalWrite(RELAY_2, false); // BUTTON_PIN is released : RELAY_2 off
}
}
The button is tied to the ground. It's all working as it should, the light just comes on randomly.
For now I just have it on all the time, which is fine.
Removed!
I tried to use all of your code but the button became unstable when I did. So I just manually did the changes, see the code below.
#define BUTTON_PIN A1
#define RELAY_1 1 // PIN 2
#define RELAY_2 2
byte lastButtonState = LOW;
byte ledState = LOW;
int led = 1;
int status = false;
void setup() {
// Initialize serial and wait for port to open:
Serial.begin(9600);
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(500);
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(RELAY_1, OUTPUT);
pinMode(RELAY_2, OUTPUT);
digitalWrite(RELAY_2, LOW);
}
void loop() {
if (digitalRead(BUTTON_PIN) == true) {
status = !status;
digitalWrite(led, status);
} while(digitalRead(BUTTON_PIN) == true);
delay(50);
}
The relay 1 light is stable but not sure if it's the code change or just not having the issue currently.
I'm going to keep an eye on it over the next few hours. Thanks again.
Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advice on) your project See About the Installation & Troubleshooting category.