So I wrote a program to toggle a momentary push button switch, ON - OFF to a relay module with activation of additional outputs to LOW to control LED indicators built in to the push button. The problem I am seeing is that when I operate the pushbutton, the relay turns on and off as intended but when the circuit is in the "OFF" position, the LED stays lit up. It is dim, but stays lit. When the circuit is energized, it commands HIGH output to the relay module and LOW output to the LED indicator.
Currently I am using the VIN as the positive input with a buck converter set to 11.5 Volts. I get full LED indicator output at 11.5 Volts when the circuit is active but have residual of I believe it was 6.5 Volts (-) when the circuit is OFF. Other pins also share this 6.5 Volt (-)
How do I get my 35 or 37 I/O Pin to be set to HIGH or (+) when the circuit is to be in the OFF position?
Arduino MEGA 2560
The code written is as shown here;
#define pushButton1 2 //Push Button for Accessory port side on D2
#define relay1 3 //High Output to Relay for pushButton1 on 0
#define blueHalo1 35 //Low Output to Blue Halo on pushButton1 on 34
#define redHalo1 37 //Low Output to Red Halo on pushButton1 on 36
int state = 0;
int old = 0;
int buttonPoll = 0;
void setup() {
pinMode(pushButton1, INPUT_PULLUP); //Push Button set as Input with internal pullup
pinMode(relay1, OUTPUT); //Relay1 Set to output to Relay Module
pinMode(blueHalo1, OUTPUT); //blueHalo1 set as output to activate Blue Halo on pushButton1
pinMode(redHalo1, OUTPUT); //redHalo1 set as output to activate Red Halo on pushButton1
digitalWrite(relay1, LOW); //set initial state of realy1 to OFF
digitalWrite(blueHalo1, HIGH); //set initial state of blueHalo1 to OFF
digitalWrite(redHalo1, HIGH); //set initial state of redHalo1 to OFF
}
void loop() {
buttonPoll = digitalRead(pushButton1);
if(buttonPoll == 1){
delay(50);
buttonPoll = digitalRead(pushButton1);
if(buttonPoll == 0){
state = old + 1;
}}
else{
delay(100);
}
switch (state) {
case 1:
digitalWrite(redHalo1, LOW);
digitalWrite(blueHalo1, HIGH);
digitalWrite(relay1, HIGH);
old = state;
break;
default:
digitalWrite(redHalo1, HIGH);
digitalWrite(blueHalo1, HIGH);
digitalWrite(relay1, LOW);
old = 0;
break;
}
}
You can SEE your circuit. We can't! Please show a schematic or at least a block diagram showing ALL you devices, including the power supplies and showing all the connections.
Any suggestions for doing this? There isn't much involved in this test run... I don't have one drawn but will try to do whatever helps.
Buck converter to power Arduino. Also provides power to the LED Indicators. Then it's just ground through the push button to pin 2 with output to pin 3 connected to relay module.
Output to low from pin 37 to activate "RED" led indicator on push button.
#define pushButton1 2 //Push Button for Accessory port side on D2
#define relay1 3 //High Output to Relay for pushButton1 on 0
#define blueHalo1 35 //Low Output to Blue Halo on pushButton1 on 34
#define redHalo1 37 //Low Output to Red Halo on pushButton1 on 36
bool state = 0;
//-------------------------------------------------------------
void setup() {
pinMode(pushButton1, INPUT_PULLUP); //Push Button set as Input with internal pullup
pinMode(relay1, OUTPUT); //Relay1 Set to output to Relay Module
pinMode(blueHalo1, OUTPUT); //blueHalo1 set as output to activate Blue Halo on pushButton1
pinMode(redHalo1, OUTPUT); //redHalo1 set as output to activate Red Halo on pushButton1
digitalWrite(relay1, LOW); //set initial state of realy1 to OFF
digitalWrite(blueHalo1, HIGH); //set initial state of blueHalo1 to OFF
digitalWrite(redHalo1, HIGH); //set initial state of redHalo1 to OFF
}
//-------------------------------------------------------------
void loop() {
if (digitalRead(pushButton1) == LOW) {
delay(50);
if (digitalRead(pushButton1) == LOW) {
state = !state;
while (digitalRead(pushButton1) == LOW) {}
}
}
else {
delay(100);
}
switch (state) {
case 1:
digitalWrite(redHalo1, LOW);
digitalWrite(blueHalo1, HIGH);
digitalWrite(relay1, HIGH);
break;
default:
digitalWrite(redHalo1, HIGH);
digitalWrite(blueHalo1, HIGH);
digitalWrite(relay1, LOW);
break;
}
}
A push button is an input device, you can only toggle an output device because toggle means to turn on them off.
Also what has this got to do with the title of the topic?
I think the problem is more to do with the way you have wired things, not the code.
Describing a wiring in words simply doesn't cut it in electronics. We need to see what you actually have. The only way to do that is by using a schematic.
The little silver box is a buck converter. The black and yellow wires are positive and negative. Black being negative.
The negative feeds the Arduino mega and the push button so that the push button can send LOW pulse to input pin number 2. It also feeds the relay module.
The positive feeds the relay module, the Arduino mega and is also connected to the common positive or Anodes for the LED indicator as the indicator is an RGB.
Currently it is only connected to the cathode side of the red LED which comes from the output pin number 37
I've assigned pin number 3 to be the output to the relay via HIGH, and the output pin of number 37 to be LOW when the circuit is activated and for it to be HIGH when it is deactivated.
Also in the code I have the VOID SETUP to start with pin number 37 in the HIGH position or should I say the off position for the Red LED
The moment everything powers up, I have a dim red LED indicator and when the push button is pressed, it gets brighter and then when it's pressed again it goes dim but it never turns off entirely.
So the push button is an RGB but I'm only trying to focus on four wires for simplicity.
It is a 12 volt push button so the LEDs / RGB's are looking for 12 volts. The three LEDs share a common anode or 12V positive and you activate one of them when you connect it to ground or LOW.
So I have 12 volt positive going to the anode for red and the ground feed is to come from output number 37 on the Arduino. Problem is it's always outputting some form of ground which causes it to have a dim red LED lit. On the switch is activated the LED goes to full brightness.
So the push button in this diagram / drawing, I separated the LED from it.
Push button has an input and an output connecting ground.
The red LED is for status indication. When I press the push button, I want it to turn on and off the relay module. When the relay module is turned on I want the red LED to light up. When the relay module is off I want the red LED to turn off. But for some reason I have "residual" LOW at the output pin that feeds the LED indicator instead of HIGH like is written in the code.
So the indicator is always red starting at the time the Arduino is powered up. It's dim at 6.5 volts (-) but I'm asking it to be HIGH (+).
When I press the push button, the output pin gives full 12V (-) and the red indicator is at full brightness, but when pressed again does not turn off and stays at the dim 6.5 volts (-)
When I press the button, the relay activates. When I press it again, the relay deactivates.
Simultaneously, the red LED indicator goes fully bright. When I press the button a second time it does not turn off fully and goes to about half the voltage.
When pushbutton is pressed 1 time, pin 37 has full 11.5V (-). When pressed a second time it switches to 6.5V (-). Why is that? Shouldn't it be pulled high (+) so the redLED turns OFF?