Programming a thermometer starting from Starter's Kit Project 05

Hi everybody,

I hope that's the right section of the forum to post my question.

Starting by saying that I'm a total newbie for what concerns programming, coding and electronics, I try to dive as much as I can in this (for me) new and interesting world.
I've started couple of months ago with the Arduino Starting Kit, which I find really helpful.
After completing Project 05 (Mood Cue), I tried to figure out how I could write a program that control and move the servo motor according to the temperatures measured by the sensor which come with the Starter Kit.

Below you'll find my guess - obviously wrong :wink:

I admit I was trying to figure out what's wrong there, but I could spot so many flaws and uncertainty that I really don't know where to start.

One of my main doubts is how to translate temperature's reading to servo output. I guess the function map() is missing something

#include <Servo.h>
Servo myServo;
int const Temp = A0;
const float baselineTemp = 19.0;
int angle;

void setup () {
  myServo.attach(9);
  Serial.begin(9600);
}

void loop () {
  int sensorVal = analogRead(A0);
  Temp = analogRead(Temp);
  Serial.print("Temp: ");
  Serial.print(A0);
  float temperature = (voltage - .5) * 100; //conversion
  Serial.println(temperature);
   
   if(temperature < baselineTemp){
     angle = map(potVal, 0, 1023, 0, 179);
     myServo.write(angle);
     Serial.print(", angle: ");
     Serial.println(angle);
  }else if(temperature >= baselineTemp+2 && temperature < baselineTemp+4){
     angle = map(potVal, 0, 1023, 0, 179);
     myServo.write(angle);
     Serial.print(", angle: ");
     Serial.println(angle)
  }else if(temperature >= baselineTemp+4 && temperature < baselineTemp+6){
     angle = map(potVal, 0, 1023, 0, 179);
     myServo.write(angle);
     Serial.print(", angle: ");
     Serial.println(angle)
  }else if(temperature >= baselineTemp+6){
     angle = map(potVal, 0, 1023, 0, 179);
     myServo.write(angle);
     Serial.print(", angle: ");
     Serial.println(angle)
  }
  delay(15);
}

Attached you can also find the schemes of the circuit - which i still didn't build but just design (apologize to all the experts here to have used "design" for such a basic sketch...)

Any help would be very much appreciated!
Thanks!

  Temp = analogRead(Temp);

Is Temp supposed to be the pin number to read from or the value read from that pin? Both is most definitely the wrong answer.

The code you posted doesn't even compile, does it? Fix all the errors before you post code here, unless the problem is that you don't know how to fix them. In which case, you should be posting the error messages that you don't understand.

PaulS:

  Temp = analogRead(Temp);

Is Temp supposed to be the pin number to read from or the value read from that pin? Both is most definitely the wrong answer.

The code you posted doesn't even compile, does it? Fix all the errors before you post code here, unless the problem is that you don't know how to fix them. In which case, you should be posting the error messages that you don't understand.

Right,
the code even doesn't compile.

The aim of my post was to find someone keen to help me in doing this (I guess basic) program.

Apologize if this is the wrong section or my question (helping in write such a program) doesn't fit.

Apologize if this is the wrong section or my question (helping in write such a program) doesn't fit.

It's not in the wrong section. It's that you asked for the wrong kind of help. Even after prompting, you still haven't posted the error messages.

Fixed the code, prompted with a different error

Here's the v. 0.1

#include <Servo.h>
Servo myServo;
int const Temp = A0;
const float baselineTemp = 19.0;
int angle;

void setup () {
  myServo.attach(9);
  Serial.begin(9600);
}

void loop () {
  int sensorVal = analogRead(A0);
  Temp = analogRead(Temp);
  Serial.print("Temp: ");
  Serial.print(A0);
  float temperature = (voltage - .5) * 100; //conversion
  Serial.println(temperature);
   
   if(temperature < baselineTemp){
     angle = map(Temp, 0, 1023, 0, 179);
     myServo.write(angle);
     Serial.print(", angle: ");
     Serial.println(angle);
  }else if(temperature >= baselineTemp+2 && temperature < baselineTemp+4){
     angle = map(Temp, 0, 1023, 0, 179);
     myServo.write(angle);
     Serial.print(", angle: ");
     Serial.println(angle)
  }else if(temperature >= baselineTemp+4 && temperature < baselineTemp+6){
     angle = map(Temp, 0, 1023, 0, 179);
     myServo.write(angle);
     Serial.print(", angle: ");
     Serial.println(angle)
  }else if(temperature >= baselineTemp+6){
     angle = map(Temp, 0, 1023, 0, 179);
     myServo.write(angle);
     Serial.print(", angle: ");
     Serial.println(angle)
  }
  delay(15);
}

The error...

servo.ino: In function 'void loop()':
servo:14: error: assignment of read-only variable 'Temp'
servo:17: error: 'voltage' was not declared in this scope
servo:30: error: expected `;' before '}' token
servo:35: error: expected `;' before '}' token
servo:40: error: expected `;' before '}' token
servo:14: error: assignment of read-only variable 'Temp'

You've declared Temp to be const:

int const Temp = A0;

which is a good thing. So, why are you trying to write to it:

  Temp = analogRead(Temp);

?

servo:17: error: 'voltage' was not declared in this scope

So, why are you referencing it here?

  float temperature = (voltage - .5) * 100; //conversion

I suspect that you left a line out that defines voltage as a function of the reading from the Temp pin.

     Serial.println(angle)

Needs a ; on the end.

Ok,

thanks.

Fixed with v0.2, no errors.

#include <Servo.h>
Servo myServo;
int const Temp = A0;
const float baselineTemp = 19.0;
int angle;

void setup () {
  myServo.attach(9);
  Serial.begin(9600);
}

void loop () {
  int sensorVal = analogRead(A0);
  Serial.print("Temp: ");
  Serial.print(A0);
  float voltage = (sensorVal/1024.0) * 5.0;
  float temperature = (voltage - .5) * 100; //conversion
  Serial.println(temperature);
   
   if(temperature < baselineTemp){
     angle = map(Temp, 0, 1023, 0, 179);
     myServo.write(angle);
     Serial.print(", angle: ");
     Serial.println(angle);
  }else if(temperature >= baselineTemp+2 && temperature < baselineTemp+4){
     angle = map(Temp, 0, 1023, 0, 179);
     myServo.write(angle);
     Serial.print(", angle: ");
     Serial.println(angle);
  }else if(temperature >= baselineTemp+4 && temperature < baselineTemp+6){
     angle = map(Temp, 0, 1023, 0, 179);
     myServo.write(angle);
     Serial.print(", angle: ");
     Serial.println(angle);
  }else if(temperature >= baselineTemp+6){
     angle = map(Temp, 0, 1023, 0, 179);
     myServo.write(angle);
     Serial.print(", angle: ");
     Serial.println(angle);
  }
  delay(15);
}

Tomorrow I'll try to wire everything up and see if it does what I want it do it (changing angle in servo as the temperature changes).

For what I can understand from the sketch, it should work, right?
The only doubt I have is related to the behaviour of the servo

     angle = map(Temp, 0, 1023, 0, 179);

The pin number is a constant, so the value for angle will always be the same. You want to be mapping the temperature, not the pin number, and its unlikely that it gets to 1023 degrees wherever you are, regardless of what scale you are using.

   if(temperature < baselineTemp){
     angle = map(Temp, 0, 1023, 0, 179);
     myServo.write(angle);
     Serial.print(", angle: ");
     Serial.println(angle);
  }else if(temperature >= baselineTemp+2 && temperature < baselineTemp+4){
     angle = map(Temp, 0, 1023, 0, 179);
     myServo.write(angle);
     Serial.print(", angle: ");
     Serial.println(angle);
  }else if(temperature >= baselineTemp+4 && temperature < baselineTemp+6){
     angle = map(Temp, 0, 1023, 0, 179);
     myServo.write(angle);
     Serial.print(", angle: ");
     Serial.println(angle);
  }else if(temperature >= baselineTemp+6){
     angle = map(Temp, 0, 1023, 0, 179);
     myServo.write(angle);
     Serial.print(", angle: ");
     Serial.println(angle);
  }

Regardless of the temperature, do the same thing. Why?

Sorry,

But I didn't get your explanation.

My aim was to read the value of the thermosensor with the

void loop () {
  int sensorVal = analogRead(A0);

And the - according to the reading - let the servo react consequently

My aim was to read the value of the thermosensor with the

Which works. Then, you convert sensorVal to voltage (correctly, it seems) and then convert voltage to temperature (by a means that doesn't look incorrect).

But, then nothing else in the code makes sense. You are mapping a value from one range to another range. It would seem to me that you want to map temperature , in some range, to servo angle, in some range. Instead, you are mapping the number of the pin that you read from from a meaningless range to a servo angle.

So, your servo is being told to go to some place based, not on temperature but on pin number. That's the part I don't understand.

Well, that and the fact that whichever of the if statements evaluates to true, you do the same thing. Typically, people have a bunch of if statements because they want to do different things depending on which (possibly compound) statement is true.