Boolean stuck on true, toilet occupancy(help)

Hi, i was working on a toilet occupancy indicator that will detect the presence of people.
However, for some reason, the transmitter side, the Boolean is always stuck on the True side. Can anyone help to figure this out?

Transmitter

#include <RH_ASK.h>
#include <SPI.h> // Not actually used but needed to compile
RH_ASK driver;
boolean check=true;
int buttonState = 1;
const int buttonPin =2;
int PIR = 7;

void setup() {
  // put your setup code here, to run once:
pinMode(13, OUTPUT);
pinMode(buttonPin, INPUT);
digitalWrite(buttonPin, HIGH);
pinMode (PIR,INPUT);
Serial.begin(9600);    // Debugging only
    if (!driver.init())
         Serial.println("init failed");
         pinMode(PIR, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
digitalWrite(13, check);
buttonState = digitalRead(buttonPin);
int sense = digitalRead (PIR);
delay(500);

if (buttonState == HIGH){
    check = false;}
else if (buttonState == LOW)
    {if(check == true){;}
     else if(sense == HIGH){check = true;}}

if( check == true ){
    const char *msg = "a";
    driver.send((uint8_t *)msg, strlen(msg));
    driver.waitPacketSent();
    Serial.print("a");
    delay(100);
    }

    else
    {const char *msg = "b";
    driver.send((uint8_t *)msg, strlen(msg));
    driver.waitPacketSent();
    Serial.print("b");
    delay(100);}     
 }

Reciever

#include <RH_ASK.h>
#include <SPI.h> // Not actualy used but needed to compile
int led1 =2;
RH_ASK driver(2000,15,12,13,false);
int led2 =4;
void setup()
{
  
  Serial.begin(9600);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  
  if (!driver.init())
    Serial.println("init failed");
}
int i;
void loop()
{
  uint8_t buf[5];

  uint8_t buflen = sizeof(buf);
  
  if ( driver.recv(buf, &buflen) )
  {

    driver.printBuffer("Got:", buf, buflen);
    Serial.println((char*)buf);
    Serial.println(sizeof(buf));
    
  
   
    {if(buf[0] == 'a')
   {digitalWrite(led1, HIGH);}
   else if(buf[0] == 'b')
   {digitalWrite(led1, LOW);}
   }

   
   // lcd.setCursor(0, 0);
    //lcd.print((char*)buf);
i++;
    /*if(pos < 2)
      lcd.setCursor(0, pos);
      else
      {
      pos=0;
      Serial.println("");
      //lcd.clear();
      }
      for (i = 1; i < buflen; i++)
      {
      Serial.print((char)buf[i]);
      pos++;
    */
  }

Thanks!

How is the button switch wired?

just normally attached without any resistor. But i don't see that as a problem as the base code i made beforehand

boolean check=true;
int buttonState = 1;
const int buttonPin =2;
int PIR = 7;
void setup() {
  // put your setup code here, to run once:
pinMode(13, OUTPUT);
pinMode(buttonPin, INPUT);
digitalWrite(buttonPin, HIGH);
pinMode (PIR,INPUT);

}

void loop() {
  // put your main code here, to run repeatedly:
digitalWrite(13, check);
buttonState = digitalRead(buttonPin);
int sense = digitalRead (PIR);
delay(500);
if (buttonState == HIGH){
    check = false;}
else if (buttonState == LOW)
    {if(check == true){;}
     else if(sense == HIGH){check = true;}}
 }

the problem is that the boolean become stuck to true after i add the Radiohead code. But, i dont know if the button is the problem

almost1234:
However, for some reason, the transmitter side, the Boolean is always stuck on the True side.

Not sure.... seems like some weird stuff in the layout of code here...

{if(check == true){;}
else if(sense == HIGH){check = true;}}

The way you set out your brackets and things like that doesn't look tidy.

Anyway ..... what you could do is it strategically put some print statements (bread-crumb trails) in parts of your 'if' 'case' code....so you can visually see in the serial monitor screen which levels are reached.

almost1234:
The problem is that the boolean become stuck to true after i add the Radiohead code. But, i dont know if the button is the problem

Then simply (temporarily) go back to your 'previous' version of code, without the 'radiohead' code .... to see if your button works there.

just normally attached without any resistor.

What does "normally attached" mean?

In other words, describe exactly how you have wired the button, including both button connections.

Make sure you understand your PIR, active high or low, any sensitivity setting or duration hold of its own, logic level, etc.

almost1234:
just normally attached without any resistor. But i don't see that as a problem as the base code i made beforehand

It is a problem as you don't use INPUT_PULLUP, needed to make a button work if connected without resistor.

And of course the button must be connected between the pin and GND, or it still won't do anything useful.

wvmarle:
It is a problem as you don't use INPUT_PULLUP, needed to make a button work if connected without resistor.

And of course the button must be connected between the pin and GND, or it still won't do anything useful.

Ok, will try to see if this works. Thanks!

Southpark:
Then simply (temporarily) go back to your 'previous' version of code, without the 'radiohead' code .... to see if your button works there.

its works fine without the button being added to a resistor

UPDATE:
after trying some of your suggestion, i discovered that the button is not in fault here, but instead is the code itself.

After several try testing, i realized that the serial freezes when i pressed the button and the PIR sensor detected some movement. I saw that the boolean turns true for a second, but then the whole thing crashes instantly afterwards.

At this point, i could not find the reason behind this freeze as i tried to use every resistor in every possible place and it do not fix this problem.

Please help.

What boolean?
Where's your updated code and circuit diagram?

Sorry for not including any updates regarding the code, here is it.

#include <RH_ASK.h>
#include <SPI.h>
RH_ASK driver;
boolean check=true;
int buttonState = 0;
const int buttonPin =2;
int PIR =7;
void setup() {
  // put your setup code here, to run once:
pinMode(13, OUTPUT);
pinMode(buttonPin, INPUT);
digitalWrite(buttonPin, LOW);
pinMode (PIR,INPUT);
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
digitalWrite(13, check);
buttonState = digitalRead(buttonPin);
int sense = digitalRead(PIR);
if (buttonState== LOW){check = false;}
else if (buttonState == HIGH){if (check == true){;}
                              else if (sense == HIGH){check = true;}}
Serial.println(sense);                              

if( check == true){
    const char *msg = "a";
    driver.send((uint8_t *)msg, strlen(msg));
    driver.waitPacketSent();
    Serial.println("yay");}
 }

I altered the setting so that the button with resistor can work, but the rest is still the same as per se.
I have attached the picture of the configuration and the freeze error that occured.
Thanks

In the part of your code that says ....

else if (buttonState == HIGH){if (check == true){;}
else if (sense == HIGH){check = true;}}

That kind of means .... if 'check' is 'true', then do nothing..... right?

Or did you mean to write 'check = true' .... instead of 'if(check == true)' ?

Should probably code things like this.......

if (buttonState== LOW) {
   check = false;
}
else if (buttonState == HIGH) {
          if (check == true){
          }                                                       //do nothing
          else if (sense == HIGH) {
                 check = true;
                }
       }

A simple auto-reformat (ctrl-T) by the IDE makes this code a lot more readable:

#include <RH_ASK.h>
#include <SPI.h>
RH_ASK driver;
boolean check = true;
int buttonState = 0;
const int buttonPin = 2;
int PIR = 7;
void setup() {
  // put your setup code here, to run once:
  pinMode(13, OUTPUT);
  pinMode(buttonPin, INPUT);
  digitalWrite(buttonPin, LOW);
  pinMode (PIR, INPUT);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(13, check);
  buttonState = digitalRead(buttonPin);
  int sense = digitalRead(PIR);
  if (buttonState == LOW) {
    check = false;
  }
  else if (buttonState == HIGH) {
    if (check == true) {
      ;
    }
    else if (sense == HIGH) {
      check = true;
    }
  }
  Serial.println(sense);

  if ( check == true) {
    const char *msg = "a";
    driver.send((uint8_t *)msg, strlen(msg));
    driver.waitPacketSent();
    Serial.println("yay");
  }
}

That's some oddly nested if/else statement set indeed. Are you sure this is what you want to do?

Reading the screenshot, it seems like your code freezes the moment sense becomes 1 aka HIGH. When this happens, and buttonPin is HIGH (button not pressed?), check is set to true and the last block of your loop() is entered:

  if ( check == true) {
    const char *msg = "a";
    driver.send((uint8_t *)msg, strlen(msg));
    driver.waitPacketSent();
    Serial.println("yay");
  }

So my money is on the problem being related to whatever this "driver" is doing.

So i tried to reworked with my code and try to make it clean as possible to try to reduce the error

#include <RH_ASK.h>
#include <SPI.h>
RH_ASK driver;
boolean recheck = true;
boolean buttonState = 1;
const int buttonPin = 2;
const int PIR = 7;

void setup() {
  // put your setup code here, to run once:
  pinMode(13, OUTPUT);
  pinMode(buttonPin, INPUT);
  digitalWrite(buttonPin, HIGH); //put input to high and wait for button to pull it low
  pinMode(PIR, INPUT);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(13, recheck);
  buttonState = digitalRead(buttonPin);
  boolean sense = digitalRead(PIR); //shouldn't this be boolean?
  if (buttonState == HIGH) {
    recheck = false;
  }
  if (buttonState == LOW) {
    //this line seems redundant
    // if (recheck == true) {}

    if (sense == HIGH) {
      recheck = true;
    }
  }
  Serial.println(recheck);

  if ( recheck == true) {
    const char *msg = "a";
    driver.send((uint8_t *)msg, strlen(msg));
    driver.waitPacketSent();

    Serial.println("Sent message a");
  }
  delay(500); //slot in a short delay else it may give errors sometimes.
}

However, it still gives the same results as the root problem: the arduino still freezes when the button is pressed and the PIR sensor detect movement.
So I guess I have to agree to the fact regarding

wvmarle:
So my money is on the problem being related to whatever this "driver" is doing.

Even if it is true, what and why does it conflict and cause a halt to the arduino itself? it doesnt make sense
The library is RadioHead, just in case somebody asking.

Even if it is true, what and why does it conflict and cause a halt to the arduino itself?

How is the radio connected to the Arduino? Are there any pin conflicts? Post a link to the library.

More likely: it is just sitting there waiting for something that never happens.

Start by adding print statements in every line (just print 1, 2, 3, etc). Then you know at which line it hangs. Comment out that one line. See what happens. If it works, you know exactly at which point your code hangs. Then go and find out why. Start with the most suspected block:

  if ( recheck == true) {
    Serial.println('1');
    const char *msg = "a";
    Serial.println('2');
    driver.send((uint8_t *)msg, strlen(msg));
    Serial.println('3');
    driver.waitPacketSent();
    Serial.println("Sent message a");
  }