Hi, bool is not working.

Hello, I'm new to Arduino I wrote a code that can work with l298 to control the irrigation system in my house, but the place that I define as bool doesn't work, can you please help?

const int buttonPin = 2;
const int tbuttonPin = 8;
const int lockPin = 7;
const int unlockPin = 6;
int buttonstate = 0;
//int tbuttonstate = 0;


void setup() {
  pinMode(buttonPin, INPUT);
  //pinMode(tbuttonPin, INPUT);
  pinMode(lockPin, OUTPUT);
  pinMode(unlockPin, OUTPUT);
}

void loop() {
  bool rwaterstate = false;
  //bool lwaterstate = false;

  buttonstate = digitalRead(buttonPin);
  //tbuttonstate = digitalRead(tbuttonPin);

  if (buttonstate == HIGH) {
    rwopen();
    bool (rwaterstate == true);
  }
  if ((buttonstate == HIGH) && (rwaterstate == true)) {
    rwclose();
    bool (rwaterstate == false);

  }
}


void rwopen() {
  digitalWrite(lockPin, HIGH);
  delay(300);
  digitalWrite(lockPin, LOW);
}
void rwclose() {
  digitalWrite(lockPin, LOW);
  digitalWrite(unlockPin, HIGH);
  delay(350);
  digitalWrite(unlockPin, LOW);
}

just

  if (buttonstate == HIGH) {
    rwopen();
    rwaterstate = true;
  }
  if ((buttonstate == HIGH) && (rwaterstate == true)) {
    rwclose();
    rwaterstate = false;

For study: "C/C++ data types"

void loop() {
  bool rwaterstate = false;

Each time through loop() rwaterstate is reset to false. That does not sound right

If you want rwaterstate to retain its value when loop() restarts use

void loop() {
  static bool rwaterstate = false;

Thank you very much for your feedback but I couldn't understand what to do right now

How do you know that it "doesn't work" ?

Try printing its value every time before you test it in the program. Is the value what you expect ?

Try my suggestion in reply #2. It cannot do any harm

Not sure what you think this does?

  if (buttonstate == HIGH) {
    rwopen();
    bool (rwaterstate == true);
  }

If you are trying to assign rwasterstate the value true, that's not going to happen. If you are trying to define rwaterstate, that variable goes out of scope (i.e., dies) when the last brace of the if statement block is reached.

Did you mean this:

  if (buttonstate == HIGH) {
    rwopen();
    waterstate = true;
  }

e_akbulut:
Thank you very much for your feedback but I couldn't understand what to do right now

Well, that is very ambiguous. Did you try incorporating the code UKHeliBob and I posted?

I followed your suggestions but still not working

e_akbulut:
I followed your suggestions but still not working

Then, please post the updated code in a new post.

You don't use == for assignment. Use = instead.

Maybe something like this will work:

const int buttonPin = 2;
const int tbuttonPin = 8;
const int lockPin = 7;
const int unlockPin = 6;
int buttonstate = 0;
//int tbuttonstate = 0;


bool RightWaterValveOpen = false;


void setup()
{
  pinMode(buttonPin, INPUT);
  //pinMode(tbuttonPin, INPUT);
  pinMode(lockPin, OUTPUT);
  pinMode(unlockPin, OUTPUT);
}


void loop()
{
  buttonstate = digitalRead(buttonPin);
  //tbuttonstate = digitalRead(tbuttonPin);


  if (buttonstate == HIGH)
  {
    if (RightWaterValveOpen)
    {
      // Is open so close
      rwclose();
    }
    else
    {
      // Is closed so open
      rwopen();
    }
  }
}


void rwopen()
{
  digitalWrite(unlockPin, LOW);
  digitalWrite(lockPin, HIGH);
  delay(300);
  digitalWrite(lockPin, LOW);
  RightWaterValveOpen = true;
}


void rwclose()
{
  digitalWrite(lockPin, LOW);
  digitalWrite(unlockPin, HIGH);
  delay(350);
  digitalWrite(unlockPin, LOW);
  RightWaterValveOpen = false;
}

Thank you very much for your kind support.