analog alarm and value replacement

Hi i need a bit of help and be prepared as this will be quite long winded.

For part of my project i require a temperature sensor and alarm. however it needs to be dynamic. i am using a LM35. what it needs to do is output a small buzz when it reads within a certain temperature range using a piezo (for example 40-50 degrees) the thing is is that i need this to be able to change. it will have a single tact switch for control. should the switch be pressed within the temperature range it will deactivate the piezo however should it be pressed outside of the temperature range that temperature should become the target temperature (for example the range is 40-50 degrees but if the button is pressed when it is is 30 degrees the range becomes 25-35 degrees) this range change needs to be persistent even if it is turned off. this is far more complex than my current coding skill and i don't know if this is do-able. if someone could send me towards a relevant tutorial or give me a bit of help. thanks.

this range change needs to be persistent even if it is turned off.

So, you need to store the center-point in EEPROM. That's pretty simple.

The rest of the project sounds pretty simple, too. What part(s) are you having trouble with?

i would like to know how to set a variable to be saved to the eeprom and change it to the input value from an analog source when a button is pressed.

[qote]i would like to know how to set a variable to be saved to the eeprom[/quote]

byte thisNeedsToBeSavedToEEPROM = 14;

and change it to the input value from an analog source when a button is pressed.

   int currValue = digitalRead(somePin);
   if(currValue != prevValue)
   {
      int analogValue = analogRead(someOtherPin);
      thisNeedsToBeSavedToEEPROM = analogValue/4; // scale to fit

      saveThis(thisNeedsToBeSavedToEEPROM);
   }
   prevValue = currValue;

All YOU need to do is research how to create the saveThis() function to actually save the value in EEPROM.

THANKS :D ! This is exactly what i wanted. enough to get me set in the right direction but not so much that i still know how the code works.

oh for clarification purposes when you say “some pin” could you please say which set of pins this refers to e.g digital input, analog input etc.

oh for clarification purposes when you say "some pin" could you please say which set of pins this refers to e.g digital input, analog input etc.

Which pins does analogRead() work with? Which pins does digitalRead() deal with? The context is pretty clear, I would think.

Don't forget that you'll need to read EEPROM, too. Probably in setup(), and store the value in some variable.

thankyou i figured it out :slight_smile:
could you tell me if this would be funtional and if not then how i could fix it.

int but = 2;
int led = 13;
int addr = 14;
int butState = 0;
void setup() {
Serial.begin(9600);
pinMode(but, INPUT);
pinMode(led, OUTPUT);
}
void loop() {
int butState = digitalRead(but);
int val = analogRead(A0);
int midpoint =  EEPROM.read(14);
Serial.println(sensorValue, DEC)
if (val > midpoint) {
digitalWrite(led, HIGH);
}
if (val < midpoint) {
digitalWrite(led, LOW );
}
if (butState == HIGH) {
EEPROM.write(addr, val)
}
}

could you tell me if this would be funtional

Not really. You want to write to EEPROM as few times as possible. That means writing when the switch BECOMES pressed, not when the switch IS pressed. Look at the State Change Detection example.

int but = 2;
int led = 13;
int addr = 14;
int butState = 0;
int lastbbutState = 0;
void setup() {
Serial.begin(9600);
pinMode(but, INPUT);
pinMode(led, OUTPUT);
}
void loop() {
int butState = digitalRead(but);
int val = analogRead(A0);
int midpoint =  EEPROM.read(14);
Serial.println(sensorValue, DEC)
if (val > midpoint) {
digitalWrite(led, HIGH);
}
if (val < midpoint) {
digitalWrite(led, LOW );
}
if (butstate !=lastbutState {
if (butstate == HIGH) {
EEPROM.write(addr, val);
}
}
}

now?

now?

Does that code even compile?

int lastbbutState = 0;
if (butstate !=lastbutState {

I guess I missed the part where you assign a different value to lastbbutState/lastbutState.

again as i do not yet have an arduino so i cant test it but other than the typo can you see any immediate problems?

i do not yet have an arduino so i cant test it

You can download the IDE, and run the compiler, at least.

The proof will really only happen when you have the Arduino and can actually run the code.

this version compiled successfully, do you think it would work?

#include <EEPROM.h>
int but = 2;
int led = 13;
int addr = 14;
int butState = 0;
int lastbutState = 0;
void setup() {
Serial.begin(9600);
pinMode(but, INPUT);
pinMode(led, OUTPUT);
}
void loop() {
int butState = digitalRead(but);
int val = analogRead(A0);
int midpoint =  EEPROM.read(14);
Serial.println(val, DEC);
if (val > midpoint) {
digitalWrite(led, HIGH);
}
if (val < midpoint) {
digitalWrite(led, LOW );
}
if (butState !=lastbutState) {
if (butState == HIGH) {
EEPROM.write(addr, val);
}
}
}
int butState = digitalRead(but);

Creates a new variable called butState that is NOT the same one you created at the top and will dissapear and be recreated each time the loop function repeats.

Use the global one you already have. Lose the "int" in front of this one.

Also, since lastbutState never changes in your program, it is hardly keeping up with the last button state. You need a line somewhere at the end that says:

lastbutState = butState

So lastbutState will have in it the old button state when the loop repeats.

You need a line somewhere at the end that says:

Well, one like that, anyway. One with a semi-colon on the end, perhaps? 8)

thanks for the help :smiley: