If statement executing even when false

Hi, not too long ago when I started Arduino fun, and I have a problem here. Maybe somebody can help me. Idea is, when button pressed, it shows what is temperature range .Everything is running well, but lights are staying on after button is released. Feel free to point on my other mistakes also.
Sorry for my English!

Thank you!

#include <Adafruit_NeoPixel.h>
int ThermistorPin1 = 0;
int LED_PIN = 6;
int LED_COUNT = 10;
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
int delayval = 250;
int Vo;
float R1 = 9150;
float logR2, R2, T;
float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;

const int buttonPin = 2;
int buttonState = 0;


void setup() {

  strip.begin();
  strip.show();
  Serial.begin(9600);
  // pinMode(buttonPin, INPUT);
}

void loop() {

  Vo = analogRead(ThermistorPin1);
  R2 = R1 * (1023.0 / (float)Vo - 1.0);
  logR2 = log(R2);
  T = (1.0 / (c1 + c2 * logR2 + c3 * logR2 * logR2 * logR2));
  T = T - 273.15;


  Serial.print("Temperature: ");
  Serial.println(T);


  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH) {

    //Temp *_*_*_*_*_*_*
    if (T < 23.0) {
      strip.setPixelColor(0, 0, 0, 100);
    }

    if ((T >= 23.0) && (T <= 27.0)) {
      strip.setPixelColor(0, 0, 100, 0);
    }

    if (T > 27.0) {
      strip.setPixelColor(0, 200, 0, 0);
    }
    strip.show();
    }

  delay(delayval);
}

You're not turning them off if the button is not pressed.

  if (buttonState == HIGH) {
    // setPixelColor depending on value of T
  } else {
    strip.setPixelColor(0, 0, 0, 0); // all off, presumably?
  }

  strip.show(); // executes regardless of buttonState

EDIT: thanks @xfpd for pointing out strip.show()

I would guess you got the flow wrong - can you post the code with the else that didn't work?

Add "show()" after every pixel buffer change you want to see

4 Likes

@vilniic92 gow is your button wired?

You commented out this line, which makes no difference as pins begin in INPUT mode, but it makes me think you may have been trying some theory.

  // pinMode(buttonPin, INPUT);

Do you have a pull-down resistor on the input pin? without one, the pin floats and may just decide it wants to stay looking like it is HIGH.

More common than a pull-down resistor is to use INPUT_PULLUP mode, and test if the switch is LOW to see if it is pressed.

Wire the switch between the pin and ground, no resistor needed, usually.

HTH

a7

Thank you!
Missing strip.show(); after strip.setPixelColor(0, 0, 0, 0); was my problem. I tried before with strip.setPixelColor(0, 0, 0, 0);, but without strip.show(); , now I know why it did not worked!
If you have a little bit time, can you, please, check, what would you do better if you made my code?

1 Like

I like to move "top-down" programming into functions. I like to use setup() to prepare LEDs and buttons (any sensor). Then I like to use loop() to call any function (sensors, button, time, calculations, decisions). When I have a new idea, I can make another separate function and add a call to the new function from loop() without re-write the core code. The compiler does the work to put events in logical, efficient order. Here is a simulation with a small example...

2 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.