Having trouble with this

So I compile and upload just fine and code runs and everything work except the button I added, need some help with why the button wont work to take readings when I press it. Like I said the code continues to run and components work but I dont want them to until I press the button. Can someone take a look at my code please? Sorry for the posting, this is my first time posting for help.

MushBox_LCD.ino (3.18 KB)

So you want it to wait till there's a button-push where it will proceed with the DHT?

Hum

if (buttonState = HIGH) {

Hum Hum

if (19 >= t <= 27)

runaway_pancake:
So you want it to wait till there's a button-push where it will proceed with the DHT?

Yes, I do. I am constantly playing and fine tuning the code. I'm new to this, coming from python! but yes im trying to achieve readings on press of a button. Don't worry about the numbers Im using to tune and get readings, Im playing with another device to find the good range on these sensors until my DHT22's arrive.

Why are you writing an output to DHTPIN? It is an input and not an output and I think you are supposed to let the library control that pin.

Look closer at what larryd pointed out.

if (buttonState = HIGH) {


if (19 >= t <= 27)

I’m certain this is not what you want.

ToddL1962:
Look closer at what larryd pointed out.

if (buttonState = HIGH) {

if (19 >= t <= 27)




I'm certain this is not what you want.

I had the temp right that was a temporary change before I uploaded. That’s been changed back, yes 19 needs to be less than my temps. the button state is confusing me, I learn the button firstly from those cheap Chinese kits with typos, just discovered the online tuts. THEY are different by far. So I am not getting this new button setup I’m using. wired up like so https://www.arduino.cc/en/Tutorial/Button.

No
if (buttonState = HIGH) {

Yes
if (buttonState == HIGH) {

No
if (19 >= t <= 27)

Yes
if ( t >= 19 && t <= 27)

larryd:
No
if (buttonState = HIGH) {

Yes
if (buttonState == HIGH) {

No
if (19 >= t <= 27)

Yes
if ( t >= 19 && t <= 27)

Ok, you’re now where I was, had those values! lol. Changed because it still takes readings and everything works like it should besides the button initializing the whole process, I want nothing to happen til button pressed. Im starting with the DHT11 then ill find my way to make the rest not respond like ii want. I just need to get the process to start. Thank you so much btw!

TheMikMik:
I had the temp right that was a temporary change before I uploaded. That's been changed back, yes 19 needs to be less than my temps. the button state is confusing me, I learn the button firstly from those cheap Chinese kits with typos, just discovered the online tuts. THEY are different by far. So I am not getting this new button setup I'm using. wired up like so https://www.arduino.cc/en/Tutorial/Button.

If you have your button wired that way then that is fine. However, the way your code is written a button press has absolutely no effect.

Also, you should not be writing to DHTPIN. Check for a LOW to HIGH transition on the input. If so then read the sensor, if not then don't do anything.

Sort of like this:

void loop() {
buttonState = digitalRead(buttonPin); 
if (buttonState == HIGH && prevButtonState == LOW)
{
   // read sensor and print data here
}
prevButtonState = buttonState;
}

You should initialize prevButtonState to LOW in setup()

“I want nothing to happen til button pressed.”

Which way is your switch wired?

You need to look at switch changes in state, not at switch levels.

 //*****************************************************************************************************
//run this code every 50ms
void checkSwitches()
{
  byte currentState;

  //*********************************
  currentState = digitalRead(SW1);

  //has this switch changed state?
  if (lastSW1State != currentState)
  {
    //update to the new switch state
    lastSW1State = currentState;

    //has the switch gone HIGH, pushed?
    if (currentState == HIGH)
    {
      //do something
    }

    //must have gone from HIGH to LOW then
    else
    {
      //do something
    }

  } //END of     if (lastSW1State != currentState)  

} //END of    checkSwitches()

BTW
Use CTRL T to format your code.
Attach your ‘complete’ sketch between code tags, use the </> icon in the posting menu.
[code]Paste your sketch here[/code]