need help with thermistor comparing program

Hi im new to this forum and to the arduino. After playing around with the examples and stuff that comes built in with the program, i decided to make my first "real" program.

Here's the idea, you press a button and the thermistor records a value in a variable, you press the button again and the thermistor records a second value in another variable, then you press it the third time and it turns on one of two LEDs depending on which value was higher, the first thermistor value or the second thermistor value. Here's my code which dosen't work:

int thermpin = 0;    // thermistor pin
int firsthotterpin = 7;  //first LED
int secondhotterpin = 6;   // second LED
int firstreading = 0;       // variable to store the value coming from the sensor
int secondreading = 0; // variable to store the value coming from the sensor
int buttonpin = 2;   //button pin

void setup() {
  pinMode(firsthotterpin, OUTPUT);  // declare the ledPin as an OUTPUT
  pinMode(secondhotterpin, OUTPUT);  // declare the ledPin as an OUTPUT
  pinMode(thermpin, INPUT);  // declare the ledPin as an OUTPUT
  pinMode(buttonpin, INPUT);  // declare the ledPin as an OUTPUT
}

void loop() {
  if (digitalRead(buttonpin) == HIGH) {            // check if the input is HIGH
    firstreading = analogRead(thermpin);
  }
  if (digitalRead(buttonpin) == HIGH) {            // check if the input is HIGH
    secondreading = analogRead(thermpin);
  }
  if (digitalRead(buttonpin) == HIGH) {            // check if the input is HIGH
    if (firstreading < secondreading) {
      digitalWrite(firsthotterpin, HIGH);
    }
    else{
      digitalWrite(secondhotterpin, HIGH);
    }
  }
}

Can someone help me out here and tell me what is wrong with it i know my code is messy and I have a feeling that this is a very odd way to achieve what i want???

all the code seems to do is turn on the first led after 1 button press and both of them after 2 button presses. any help would be greatly appreaciated

by the way, pay no attention to the comments, they just left over from my copy&pasting

Firstly, you need to debounce your button.

Secondly, as to your logic errors:

Your code definitely wont work, for the sole reason that your checking the same condition and then performing different actions. On second thought, reading that sentence I just wrote doesn't make sense.

Basically, you need to do something like this. I'm going to let you write the code but Ill post some pseudo code.

int button_push = 0;

void loop()
{
      if the button has been pushed then increment the variable button_push
      
      if button_push == 1
            then perform actions for first time the button has been pushed
      else if button_push == 2
            then perform actions for second time the button has been pushed
      else if button_push >= 3
            then perform final actions
}

There are many ways to code this. What i have proposed is just one of the hundreds.

That was a great method you just taught me with the incrementing variables, i never would have though of that :D. After screwing around a bit with your example, i finally managed to get it working so thanks for your help :slight_smile:

as for the debounce, I just put a delay before checking the button status everytime.

heres my new working code for anyone else who is wondering how I did it:

int thermpin = 0;    // thermistor pin
int firsthotterpin = 7;  //first LED
int secondhotterpin = 6;   // second LED
int firstreading = 0;       // variable to store the value coming from the sensor
int secondreading = 0; // variable to store the value coming from the sensor
int buttonpin = 2;   //button pin
int button_push = 0;



void setup() {
  pinMode(firsthotterpin, OUTPUT);  // declare the ledPin as an OUTPUT
  pinMode(secondhotterpin, OUTPUT);  // declare the ledPin as an OUTPUT
  pinMode(thermpin, INPUT);  // declare the ledPin as an OUTPUT
  pinMode(buttonpin, INPUT);  // declare the ledPin as an OUTPUT
  Serial.begin(9600);
}





void loop()
{
start:

  if (digitalRead(buttonpin) == LOW) {            // check if the input is HIGH
    digitalWrite(firsthotterpin, LOW);
    digitalWrite(secondhotterpin, LOW);
    delay(500);
    button_push=button_push+1;
    Serial.println(button_push);
  }

  if (button_push == 1){
    firstreading = analogRead(thermpin);
    Serial.println("firstreading");
    Serial.println(firstreading);
  }
  else if (button_push == 2) {
    secondreading = analogRead(thermpin);
    Serial.println("secondreading");
    Serial.println(secondreading);  

  }
  else if (button_push == 3) {


    if (firstreading < secondreading) {
      digitalWrite(firsthotterpin, HIGH);

      button_push=0;
      goto start;
    }
    else if (firstreading > secondreading){
      digitalWrite(secondhotterpin, HIGH);

      button_push=0;
      goto start;
    }
    else if (firstreading = secondreading){
      digitalWrite(secondhotterpin, HIGH);
      digitalWrite(firsthotterpin, HIGH);

      button_push=0;
      goto start;
    }
  }
  else if (button_push >= 4) {

    button_push=0;
    goto start;
  }
}