Dear Arduino community. I am a newcomer to programming. As part of a class that I am in we are required to work with the Arduino. We been asked to hook up an 10k(ohm) NTC Thermistor to the Arduino and then do the following:
To do:
Program the Arduino to sample on analogue 0 at 0.5 Hz.
Make a conversion of the sampled data to represent a temperature in [⁰C]. A linear interpolation
strategy has to be used.
Receiving a ‘G’ on the Arduino serial port should start sending the temperature value on the serial
port.
Receiving an ‘S’ on the Arduino serial port should stop sending the temperature value on the serial
port.
Toggle LED on pin 13 each time a sample is sent to the com port. Toggle means that if the LED is on
then it must be turned off and vice versa. The LED must only change state once each time a sample
is sent
So far I have gotten point 1, 3 and 5 to work. I am not sure what they mean about point 2 but that is not what I have come here to ask for help with. So just ignore that part of the code that I am going to upload. I can't get the Arduino to stop again when I send it an S. Not sure what i have done wrong and hope someone can help me with it. Here is what I have so far:
bool Toogle;
int K = 18;
int power = 1023;
//
// The setup() function runs once each time the micro-controller starts
void setup()
{
Serial.begin(9600); //initialize serial communication at 9600 bits per second:
pinMode(13, OUTPUT);
}
// Add the main program code into the continuous loop() function
void loop(){
int sensorValue = analogRead(A0);
while (Serial.available() == 0);
char button = Serial.read();
if (button == 'G')
{
Serial.println("System starting");
Toogle = true;
}
if (button == 'S')
{
Serial.println("System is off");
Toogle = false;
}
while (Toogle == true)
{
//int R2 = 4000 * (power/ sensorValue - 1);
//int T = R2 / K; //Serial.print("The room temperature degree is:"); //Serial.println(T);
digitalWrite(13, !digitalRead(13));
delay(2000);
}
while (Toogle == false)
{
digitalWrite(13, LOW);
}
Serial.flush();
}
For anyone that is wondering. I can't ask my teachers since we been ask to make this little assigment during a one week holiday where we can't get in contact with them. Also sorry for that bad english. English is not my native language. If you reading this I hope you have a good day.
while (Toogle == true)
{
//int R2 = 4000 * (power/ sensorValue - 1);
//int T = R2 / K;
//Serial.print("The room temperature degree is:");
//Serial.println(T);
digitalWrite(13, !digitalRead(13));
delay(2000);
}
Once that you enter that while loop, there is no way to exit the while loop because you never change the state of Toogle. Once Toogle is true it stays true. You could probably change the while to an if.
You should consider some kind of calibration exercise to work out the relationship between raw analog input reading and temperature.
And be careful with integer arithmetic - the way it can't handle fractions in the middle of calculations can catch out the unwary.
Eg looking at your commented out code, if sensorValue is anywhere between 1023 and 513 then (power/sensorValue) will get rounded to 1, and R2 will be zero.
To hammy: I did research on the NTC but I still run into problem. First off since it is holiday I don't have access to the NTC and can't use them during the holidays. And form the information I have gather you would either use Steinhart–Hart equation or an first-order approximation. Since no Steinhart–Hart parameters is giving I think I need to use an first-order approximation but how can I if I don't have the NTC to collect data? And I had 2x 2 hour lesson with the Arduino. The first 2 hours being the history about the Arduino and the italian company that made it. The rest have been a few pieces of code for the Arduino so not a lot of sample code I can learn from. Tried to find websites that have some code from the Arduino but I haven't found anything that I can use for this assignment.
To groundFungus: I see. Well can I make an exist to the while loop? Like if I added:
char button = Serial.read();
if (button == 'S')
{
Serial.println("System is off");
Toogle = false;
}
into the while loop. Would it be able to exist it then?
To GypsumFantastic:
I thank you for the kind words. I presume that instead of having R2 and T being int they should be either float or double. As I have written to hammy i don't have the NTC so i am not sure how I can find the temperature.
Question 1 tells you to use linear interpolation - I think you need to forget about Steinhart-hart and first order approximations etc.
You need more information about your thermistor. Either you have to experiment with it, or the teacher should give you the values or something. I don't understand how you can do the project without this information.
Do you have access to an Arduino at all? It's hard to write a sketch without testing it.
power needs to be a float I think - I'm assuming you're going to uncomment those lines in the middle of your code.
You need to work on getting it to sample at 0.5Hz - here's a tutorial about