Changing boolean state with two buutons

Hello, everyone!
I need to develop a system that must have an overall state of On or Off controlled by 1 “On” button and 1 “Off” button and do some stuff with those stats, so i started trying to make this part of the code attaching the buttons to a boolean variable, but it just gets stuck in “true” state and doesn’t change.

const int on = 13;
const int off = 10;
const int onoff = 9;
bool stg = false;

void setup()
{
 pinMode(on, INPUT);
 pinMode(off, INPUT);
 pinMode(onoff, OUTPUT);
}

void loop(){
 if(digitalRead(on) == HIGH){
  if(stg == false){
    stg = !stg; 
  }
 }
 if(digitalRead(off) == HIGH){
  if(stg == true){
    stg == !stg;
  }
 }
 digitalWrite(onoff, stg);
}

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

Got pulldowns on those buttons?

Yes. Every input is grounded with resistor.

Verify with a simple digital read sketch that the buttons are wired correctly, and that when pressed the input reads HIGH and when not pressed it reads LOW.

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

What model Arduino are you using?

Thanks.. Tom... :slight_smile:

cattledog:
Verify with a simple digital read sketch that the buttons are wired correctly, and that when pressed the input reads HIGH and when not pressed it reads LOW.

Already did it and everything seems fine.

TomGeorge:
Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled “How to use this Forum”.
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

What model Arduino are you using?

Thanks… Tom… :slight_smile:

I’ll pay more attention next time.
About the circuit, i tryed to simulate it on proteus, but dind’t worked, so i’m doing it in tinkercad.
This is my circuit now. There are some other things involved that will be introduced to the code later. What i’m using now is the first led and the 2 buttons in the middle.

one button for "ON" one button for "OFF"

simply code

 if(digitalRead(on) == HIGH){
   stg = HIGH;
  }

 if(digitalRead(off) == HIGH){
   stg = LOW;
  }

and you should be done

But maybe I have misunderstood the functionality you want to achive. If so please describe your functionality with normal words.

best regards Stefan

StefanL38:
one button for "ON" one button for "OFF"

simply code

 if(digitalRead(on) == HIGH){

stg = HIGH;
  }

if(digitalRead(off) == HIGH){
  stg = LOW;
  }



and you should be done

But maybe I have misunderstood the functionality you want to achive. If so please describe your functionality with normal words.

best regards Stefan

It's something close to that, but i also need to shut the system down if a sensor detects it's value gets to 15% or lower and that can't be changed if i press "on" button.

ighor57:
It’s something close to that, but i also need to shut the system down if a sensor detects it’s valeu gets to 15% ou lower an that can’t be changed if i press “on” button.

Your reply got hopelessly mangled.
Please try again.

A couple of letters can make something completelly indecipherable. Now it shall make sense.

Anyway, i tryed another approach and, instead of using a boolean state, i put the two shutting down parameters in an "if" and it worked well.