use last # from previous condition as a threshold

I have a 3 position off-on-on switch wired to my arduino, temp sensor and small motor. When the switch is in position 2 it reads the temp. However, when the switch is in position 3 I want it to use the last reading there was from position 2 at the moment I switch to position 3 and use that # as a threshold while in switch position 3. If the temp rises above that threshold while in position 3, it will digitalWrite(3,HIGH), which starts the motor via transistor.

I have everything wired correctly I believe, currently when the switch is in position 2, digital pin 4 is HIGH and 5 is LOW. When in switch position 3, digital pin 4 is LOW and 5 is HIGH.

When in switch position 3, how can I use the most recent reading from switch position 2 as my threshold?

Thanks in advance for the help!!

When in switch position 3, how can I use the most recent reading from switch position 2 as my threshold?

How are you saving the reading? Post the code you have now.

You have an answer in your other thread :wink:

This is the code I have now, I have removed the "if" statement until I figure out exactly how to call for the last reading in previous switch position 2 and use it as threshold in position 3.

int tempPin = 0;
int tempReading;
int switchpos2lastreading;
int switchpos3reading;


void setup() {
  Serial.begin(9600);
  pinMode(3,OUTPUT);// temp sensor
  pinMode(4,INPUT_PULLUP);//currently shows pin 4 HIGH in pos 2 and LOW in pos 3
  pinMode(5,INPUT_PULLUP);//currently shows pin 5 LOW in pos 2 and HIGH in pos 3
  // put your setup code here, to run once:
}

void loop() {
  tempReading = analogRead(tempPin);
  Serial.print("Temperature = ");
  Serial.print(tempReading);
  switchpos2lastreading = analogRead(tempPin) ;
 
 
// put your main code here, to run repeatedly:
 delay(2000);
}/code]
  tempReading = analogRead(tempPin);
  Serial.print("Temperature = ");
  Serial.print(tempReading);
  switchpos2lastreading = analogRead(tempPin) ;

How often will switchpos2lastreading contain a value different from tempReading?

Shouldn't switchpos2lastreading be assigned a value ONLY when the switch is in position 2?

Shouldn't switchpos2lastreading be assigned the value in tempReading when that is the case?

Sure that would prob work. The main thing I am having toruble understanding is how to save the last # from switch position 2 and use it in position 3. The rest of it I think will be easier to figure out after that.

Any ideas??

With switch in position 2, read data in switchpos2lastreading. When switching to position 3, simply use that value as the reference.

Alternative is to always use tempReading. First time you detect change from position 2 to position 3, copy the last tempReading to switchpos2lastreading before doing a new reading.

As mentioned in your other thread, built in something to make sure that you have a had a reading while the switch was in position two.

This from other thread. What should I change in the coding?

ok I tried it out and it doesn't seem to do what I want to do. This is what it does...

1.When plugged into usb and switch is off the motor doesn't run which is good. However, the motor runs in both switch position 2 and 3 and if you then go back to position 1 it continues to run. It only show "unknown switch position" on serial monitor when in the off position.

  1. with usb unplugged when switch in position 2 the motor runs no matter what. When I switch it to position 3 the motor does not run. Position 1 turns it off like it is supposed to.

I have attached the code below do you know what I need to change?

int tempPin = 0;
int tempReading;
byte switchposition;
byte oldswitchposition;
int threshold;


void setup() {
  Serial.begin(9600);
  pinMode(3,OUTPUT);
  pinMode(4,INPUT_PULLUP);
  pinMode(5,INPUT_PULLUP);
if (digitalRead(4) == HIGH && digitalRead(5)==LOW) {
    switchposition = 2; }
 
 else if (digitalRead(4) == LOW && digitalRead(5) == HIGH) {
    switchposition = 3; }
 
else {
   Serial.println("unknown switch position"); }
  
 oldswitchposition = switchposition;
}

void loop() {
  tempReading = analogRead(tempPin);
  Serial.print("Temp = ");
  Serial.print(tempReading);
  
if (digitalRead(4) == HIGH && digitalRead(5) == LOW) {
  switchposition = 2;
}
else if (digitalRead(4) == LOW && digitalRead(5) == HIGH) {
  switchposition = 3;
}
else {
  Serial.println("unknown switch position");
}

if (switchposition == 3 && oldswitchposition == 2) {
  threshold = analogRead(tempPin);
}
if (switchposition == 3) {
  if (analogRead(tempPin) > threshold) {
    digitalWrite(3, HIGH);
   }
}  
  
 delay(2000);
 oldswitchposition = switchposition;
}

My approach would be to only store the temp reading when the switch is in position 2. When the switch is in position 3 the temp reading can be compared to the stored temp, if there is one, since it will be the last one recorded when the switch was moved.

I don't see you writing LOW to the motor pin - do you need to do that to stop the motor? Here's my version:

int tempPin = 0;
int tempReading;
byte switchposition;
byte oldswitchposition;
int threshold;


void setup() {
  Serial.begin(9600);
  pinMode(3,OUTPUT);
  pinMode(4,INPUT_PULLUP);
  pinMode(5,INPUT_PULLUP);

  // illegal temp reading, in case we begin in positon 3
  tempReading = (-1);
}

void loop() {

  // determine switch position
  if (digitalRead(4) == HIGH && digitalRead(5) == LOW) {
    Serial.println("Position 2");
    switchposition = 2;
  }
  else if (digitalRead(4) == LOW && digitalRead(5) == HIGH) {
    Serial.println("Position 3");
    switchposition = 3;
  }
  else {
    Serial.println("Position 1");
    switchposition = 1;
    // Serial.println("unknown switch position");
  }

  switch( switchposition )
  {
  case 1:

    // motor should not be runnig
    digitalWrite(3, LOW);

    // forget any temp readings 
    tempReading = (-1);

    break;

  case 2:

    // read temp
    tempReading = analogRead(tempPin);
    Serial.print("Temp = ");
    Serial.print(tempReading);

    // may need to stop motor
    digitalWrite(3,LOW);

    break;

  case 3:

    // motor runs if over last reading
    digitalWrite(3, ((tempReading > 0) && (analogRead(tempPin) > tempReading)) ? HIGH : LOW);

    break;

  default:
    break;

  }


  delay(2000);

}

will give this a try and report back later.

Thanks

ok this is working almost...when it is in position 2 it actually prints position 3 on the monitor and when it is in position 3 it prints position 2 and the reading on the monitor. So i would like it to have the switch position right and print the readings on both. So I am actually setting the temperature threshold in position 3 and I would like to do that in position 2. Other than that it seems to work when plugged in with usb.

It doesn't however seem to work when I run it off the batteries because the power is not on at all times I am guessing. When I slide from position 2 to position 3 the lights on the arduino go off momentarily so I am guessing it wipes that threshold number clean every time. And sure enough the motor doesn't respond when I am only using batteries. The batteries are strong enough to run the motor bc while the usb was in and the motor was running I pulled the plug and the motor kept running.

So, how do I keep that figure stored when power is lost for a split second?

do I need to use a second switch just for the power?? And have another 2 position switch for setting/storing the threshold and the other position for testing the threshold?

That way the power stays on when I switch between the 2 positions???

Or is there a way for it to remember that number even if power is interrupted for a split second?

Your other thread was mostly about the wiring. Keep wiring related questions in the other thread. Keep your other question in this thread.

It gets confusing (and frustrating actually).

my apologies...I was getting answers on both threads so just wanted those answering to have all the info as I wasn't sure if everyone saw both threads.

I wasn't sure if everyone saw both threads.

And that is EXACTLY why you should NOT have two threads.

I can't be bothered merging two threads in different parts of the forum, so I'll just lock this one.