Push Button Doesn't Work

Hello All,

PLEASE HELP ME LOL, I built a garage door opener that works off the HC-05 bluetooth module. Communication seems to work fine but the button doesn't work at all. I've tried several buttons and still nothing. I was wondering if anyone could take a look at my code and see where I went wrong. I've posted the code as well as some pictures so you know what I'm up against. I think it's code related but just in case, I posted the pictures of the schematics. Thanks in advance.

MASTER SIDE CODE

#include <TimerOne.h>
#include <SoftwareSerial.h>
#define redLed    6 //change to pin 6
#define greenLed  9 //change to pin 9
#define blueLed   10 //change to pin 10
#define GarSwitch 4
#define relay     5

char ch;
String HC05_Awake = "ON";

SoftwareSerial mySerial(7, 8); // Rx | Tx

void setup() {

  Timer1.initialize(4000000);
  Timer1.attachInterrupt(KeepAlive);
  Serial.begin(115200);
  mySerial.begin(38400);
  pinMode(GarSwitch, INPUT_PULLUP);
  pinMode(relay, OUTPUT);
  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(blueLed, OUTPUT);
  digitalWrite(redLed, LOW);
  digitalWrite(greenLed, LOW);
  digitalWrite(blueLed, LOW);
  digitalWrite(relay, HIGH);
}

void loop() {

  // Sending
  int Garage = digitalRead(GarSwitch);

  if (Garage == HIGH) {
    mySerial.print('a');
    digitalWrite(blueLed, LOW);
    digitalWrite(redLed, HIGH);
    digitalWrite(greenLed, LOW);
  } else {
    digitalWrite(blueLed, LOW);
    digitalWrite(greenLed, HIGH);
    digitalWrite(redLed, LOW);

  }

  // Receiving
  if (mySerial.available()) {
    char ch = mySerial.read();
    Serial.write(ch);    

  if (ch == 'b') {
    digitalWrite(relay, LOW);
    digitalWrite(blueLed, HIGH);
    digitalWrite(greenLed, LOW);
    digitalWrite(redLed, LOW);
    delay(25);
    digitalWrite(relay, HIGH);
    digitalWrite(redLed, LOW);    
    digitalWrite(greenLed, HIGH);
    digitalWrite(blueLed, LOW);  
  } else {
    digitalWrite(relay, HIGH); 
  
    }
  }
}

void KeepAlive() {
if(HC05_Awake = "ON") {
mySerial.print('m');
Serial.print('m');
}

}

SLAVE SIDE CODE

#include <SoftwareSerial.h>
SoftwareSerial mySerial(7, 8); // Rx | Tx
#define redLed    5
#define greenLed  6
#define blueLed   10
int Button = 4;
#define pwrLed    A0
char ch;

void setup()
{
  Serial.begin(115200);
  mySerial.begin(38400);

  pinMode(pwrLed, OUTPUT);
  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(blueLed, OUTPUT);
  pinMode(Button, INPUT_PULLUP);
  digitalWrite(pwrLed, HIGH);
}

void loop()
{
  // Receiving
  if (mySerial.available()) {
    char ch = mySerial.read();
    Serial.write(ch);

  if (ch == 'a') {
    analogWrite(redLed, 255);
    analogWrite(greenLed, 0);
    analogWrite(blueLed, 0);
    delay(25);
  }
  else
  {
    analogWrite(redLed, 0);
    analogWrite(greenLed, 50);
  }
  }



  // Sending
  int button = digitalRead(Button);

  if (button == HIGH)
  {
    mySerial.print('b');
    analogWrite(greenLed, 0);
    analogWrite(redLed, 100);
    analogWrite(blueLed, 100);
  } else {
analogWrite(blueLed, 0);
analogWrite(greenLed,20);
analogWrite(redLed, 0);

  }
}

MASTER SCHEMATIC - A

MASTER SCHEMATIC - B

SLAVE SCHEMATIC

        mySerial.print('a');

What do you expect this to do ?

=, or ==?

It would help to see the actual schematics, not just the PCB layouts.

It also helps if you write a few words about how the system is supposed to work. Makes it a little easier to navigate the different devices and the code to go with it.

When the magnet is separated from the reed switch, it print's 'a' to the serial monitor. When the remote unit detects 'a' it changes the green LED to RED.

A question for you.
Is mySerial the Serial monitor ?

Yes, sorry, it's a garage door opener, I have the slave portion in the house with an LED that is green while the door is closed, and red when open. The button is supposed to trigger a relay at the master side for a quarter of a second which sends voltage to the main unit triggering the door to close.

No and sorry again, the 'a' goes through the HC-05 module, but I also have a Serial.print command to print it to the serial monitor.

  • You want help, we want the schematic.

  • The PCB trace routing needs to be cleaned up a lot.

I don't see it

Your 2 connections to the button look like they are on the same side, so permanently connected to each other. The trick with those buttons is to connect to opposite corners, that way it will always be right.

Here's the schematics for both units. My apologies, I should have posted it to begin with.

MASTER SCHEMATIC

SLAVE SCHEMATIC

Please accept my apologies for the messy and probably not correct schematics. I did them in EaseEDA online.

Aah, that explains it. It's on a PCB already so there's no way to change it without ordering a new one. But that's what I'm going to do. Thanks @PerryBebbington I seem to have forgotten that the two legs are connected.

Even with == you canot compare a string like that...

I guess I don't have it. I just looked at it again. But still, when I separate the magnet it sends 'a' to the serial monitor.

Cut one of the tracks to the button with a sharp knife and solder a wire to a pin on other side of the button, back to a suitable point on the PCB.

You also don't need the 10k resistor in series with the button.

1 Like

The switch appears to be between pin 4 and GND. Your code tests whether pin 4 is HIGH. Is that possible in your circuit ?

A secondary question, What keeps pin 4 in a known state when the switch is open ?

Try turning the button 90 degrees on the PCB.

  • Your RGB LEDs are shown as common anode but wired as common cathode, please confirm.
  • Give us a link to your NANO.

The RGB LED is common cathode.

This is the NANO board I'm using.

I can't turn the button, the holes won't line up but I think I'm going to do what @PerryBebbington said and cut the trace on the ground and solder a wire on to the opposite leg.

In fact, it's counterproductive as it forms a voltage divider with the internal pullup, making the LOW less deterministic.

I also agree with cutting a trace and soldering a workaround in place. A PCB can usually be saved with a kludge like that. I've done so (too) many times.

1 Like