• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

C and Arduino help

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.
That one makes sense to me, it's just horrendously crude is all.
I want to find one that you calibrate by setting a few variables and it extrapolates the temp between 'em.
I don't know that it's even possible without writing the equation myself (not likely to happen, heh, more likely to give me a massive headache).

Here's some info they gave us...

You can calculate the voltage of a circuit with known resistance with the equation:
Vout = Vin *(R2/(R1+R2))
R1 refers to the thermistor resistor and R2 refers to the fixed resistor.
For example, if it is 25C, using a 10K Ohm resistor, the calculation becomes:

3.40V= 5*(10000/(10000+4700))

So, at room temperature, you’d expect the voltage to by 3.57V.

This can be rearranged to calculate the resistance of the thermistor using the following
equation:
R1 = ((R2 x Vin)/Vout) - R2

So for room temperature, the resultant calculation is:

(10000 x (5/3.40)) – 10000 = 4700 Ohm.

Use this to calculate the resistance of the thermistor from the voltage you read from
the circuit in (1).


I wonder if it could be done like I did the other one with a switch case statement? That made a lot of sense to me....but it would be pretty clunky.

Clunky isn't really my concern though...I'm kind of at the point where I'm just trying to scrape by in this unit....and these labs aren't weighted highly at all. The exam is worth heaps and we've been told there won't be many arduino questions in it....but we have 2 projects coming up and we need to use our knowledge from these labs to build them. The first is to make a climate control device...so you can see why they want you to complete the labs.
 
It could work.
How accurate does your doodad have to be? Thermisters are an issue sometimes because their response to temps isn't linear.
 
It could work.
How accurate does your doodad have to be? Thermisters are an issue sometimes because their response to temps isn't linear.

I'm not sure really, they tell us about the Steinhart–Hart equation too.
They want us to use that for question 3 (which I was going to avoid). I'll probably just try this switch case thing.
 
OK, here's where I'm at now....we need to complete a huge project (huge as far as I'm concerned....it's extremely daunting reading it). I'm largely up to date on my other subjects so I can focus on this now....it's due in about 3 weeks.

It involves this thermistor stuff so I'm looking into the steinhart-hart equation now (even though I barely understand it). Hopefully I can work it all out and make my project go before the deadline. In that case I'll easily be able to go back and finish the themistor lab (kind of going about thing in the reverse order!). The project is worth 20x as much as the lab.

I borrowed some steinhart code from the arduino site. So here's the beginnings of my project.

/*1.it needs to light LEDs depending on temp
* 2.it needs to sound an alarm when too hot
* 3.it needs to activate a fan through a transistor after a certain temp is reached
* 4.Use different fan speeds depending on temp
* 5.Implement an alarm reset system using a code from several button presses
* 6.It should all only work when an LDR senses the lights are on
* 7.Use a potentionmeter as a calibration mechanism
* 8.Print out an array of info when a button is pressed.
* 9.Allow all aspects of the system to be configured using variables
*/


#include <math.h>

#define ThermistorPIN 0 // Use Pin 0 for analog input

float vcc = 5;

float balance = 10000;

float thermr = 10000;

float Thermistor(int RawADC) {
long Resistance;
float Temp;

Resistance=((1024 * balance / RawADC) - balance);
Temp = log(Resistance); // Saving the Log(resistance) so you don't need to calculate it later
Temp = 1 / (0.001129148 + (0.000234125 * Temp) + (0.0000000876741 * Temp * Temp * Temp));
Temp = Temp - 273.15; // Convert Kelvin to Celsius

return Temp; // Return the Temperature
}



void setup() {
Serial.begin(115200);
}

void loop() {
float temp;
temp=Thermistor(analogRead(ThermistorPIN)); // read ADC and convert it to Celsius

delay(5000); // Delay for 5 seconds
}



and my fritzing:

qnk51h.jpg

So I've got something to start from at least.

One of my primary questions: It needs to only work in the light.....ie: it does nothing when the LDR senses no light. How would one go about programming that? Like every function is dependant on the LDR function or something?


EDIT: Oh I forgot to include an LDR in the fritzing.
Also the ? component at the top is going to be a fan.
 
The really easy way is to start your void loop() with an if statement.
In essence:
Code:
void loop(){
  if(analogRead(LDRPin) > minimumLDRValue){
    all the code goes here!
  }
}
Then if the LDR goes dark, nothing executes except for the check to see if there is light.
 
The really easy way is to start your void loop() with an if statement.
In essence:
Code:
void loop(){
  if(analogRead(LDRPin) > minimumLDRValue){
    all the code goes here!
  }
}
Then if the LDR goes dark, nothing executes except for the check to see if there is light.

Excellent, that's easy then. Only like 10 other massive parts to program and I'm done!

I updated the fritzing so now there's an LDR, a resistor before the speaker and a fan picture......and the tutor gives his approval.

So I'll start out my code with your if statement.....then I'll probably stare at the screen blankly for a while....then I'll probably have to go and seek help from the tutor next week lol.
 
lol
Here's the trick: Do it by sections.
Never use delay for time sensitive stuff, just millis() and such.

If you don't use delay, each section will work happily around it's fellows.
This does mean that writing functions is tough, honestly I'm not sure what to do about that if the instructor demands functions.
 
OK I've been working on the very first part.....the lighting of LEDs based on the temp.
I'm going to do the switch case thing like I did previously.

Question: If I have a function called "Thermistor" and it returns "temp".....then "temp" only belongs to the scope of that function right?

So in the switch case thing....where I say int range = map(temp, 0, 50, 0, 3);

I can't use temp in that scope. So I need to do a function call right?
I don't know how to code said function call.
 
Declare temp on a globel level (before setup), inside the thermister function use tempTemp or something.

Then in the loop to populate temp with the current temp:

temp = thermister();
That will call the thermister function, and the value it returns will get stuffed into the temp variable.
(Not sure if you actually need () on thermister).
 
I don't understand the syntax for function calls....what does it mean when you have a "void" function. What are the brackets after the function for? When you call a function, you can either assign the output of the function to a variable or you can just call the function by itself?

I don't know which one to use? I'm not sure about this global temp variable.
ahhhhh here's my code:

/*
* Carl White ICT106 Project 01
* Arduino climate control device
*
* 1.it needs to light LEDs depending on temp
* 2.it needs to sound an alarm when too hot
* 3.it needs to activate a fan through a transistor after a certain temp is reached
* 4.Use different fan speeds depending on temp
* 5.Implement an alarm reset system using a code from several button presses
* 6.It should all only work when an LDR senses the lights are on
* 7.Use a potentionmeter as a calibration mechanism
* 8.Print out an array of info when a button is pressed.
* 9.Allow all aspects of the system to be configured using variables
*/

#include <math.h>
const int rawADC = 0; // Use Pin 0 for analog input
int potenPin = 1;
int ldrPin = 2;
int buttonPin6 = 6;
int buttonPin7 = 7;
int speakerPin = 9;
int ledPin10 = 10;
int ledPin11 = 11;
int ledPin12 = 12;
int fanPin = 13;
float vcc = 5;
float balance = 10000;
float thermr = 10000;
long temp;



//The steinhart-hart function called "Thermistor" to determine temp
double Thermistor(int rawADC){
long temp;
long Resistance;
float Temporary;
Resistance=((1024 * balance / rawADC) - balance);
Temporary = log(Resistance); // Saving the Log(resistance) so you don't need to calculate it later
Temporary = 1 / (0.001346239 + (0.0002309426 * Temporary) + (0.000000098159 * Temporary * Temporary * Temporary));
Temporary = Temporary - 273.15; // Convert Kelvin to Celsius
temp = Temporary;
return temp; // Return the temperature
}
//End of the steinhart-hart function called "Thermistor"

//Lighting LEDs based on temp//
void LEDs(){
//Declare the LED pins as output
pinMode(ledPin10, OUTPUT);
pinMode(ledPin11, OUTPUT);
pinMode(ledPin12, OUTPUT);
int range;
int temp;
int rawADC;
temp = Thermistor(rawADC);
range = map(temp, 0, 50, 0, 3);

switch (range) {
case 0: // no LEDs
Serial.println("All LEDs off");
digitalWrite(ledPin10, LOW);
digitalWrite(ledPin11, LOW);
digitalWrite(ledPin12, LOW);
break;
case 1: // 1 LED
Serial.println("First LED on");
digitalWrite(ledPin10, HIGH);
digitalWrite(ledPin11, LOW);
digitalWrite(ledPin12, LOW);
break;
case 2: // 2 LEDs
Serial.println("First 2 LEDs on");
digitalWrite(ledPin10, HIGH);
digitalWrite(ledPin11, HIGH);
digitalWrite(ledPin12, LOW);
break;
case 3: // 3 LEDs
Serial.println("All 3 LEDs on");
digitalWrite(ledPin10, HIGH);
digitalWrite(ledPin11, HIGH);
digitalWrite(ledPin12, HIGH);
break;
}
}
//End of LED function

//Sounding an Alarm based on temp
void Alarm(){
}
//End of Alarm function

void setup() {
Serial.begin(9600);
}

void loop() {
if(analogRead(ldrPin) > 100){
//Do the entire rest of the program, if the lights are ON
float temp;
temp=Thermistor(rawADC); // read ADC, convert it to Celsius and put it into the "temp" variable

LEDs(temp);




}
//Don't do anything if the lights are off
else{
Serial.println("Operation halted due to lack of light");
}
}

It doesn't compile obviously.
I'm going to get help from the tutor next week.
 
On the question of functions:
If the function has to return a value, it needs to be a data type (int, float, byte, etc.). If it doesn't return a value (like say a function that sets fan speed), it can be a data type or a void.

If it returns a value you have to ask it for one, while if it's a void you can't ask it for one.
(Example: if it's void Function() that I'm calling, I would just say "Function(variables to pass to it, if any);" If it's int Function that I'm calling, it has to be "someVariable = Function(variables to pass to i);"

The only issue preventing compiling is that you're calling LEDs(temp); as a function, but LEDs isn't set up to receive a variable.
Right now there are rather of a lot of variables named "temp" to be declared. That isn't a problem persay, but it can be quite confusing.
It looks like it could be declared once globally as a float (sidenote: float and double are identical in Arduinoland), and then reused by all the sections.

I spent some time removing the multiple declarations and passing the variables around happily, I marked most of the changes. Here're the results:
Code:
/*
* Carl White ICT106 Project 01
 * Arduino climate control device
 *
 * 1.it needs to light LEDs depending on temp
 * 2.it needs to sound an alarm when too hot
 * 3.it needs to activate a fan through a transistor after a certain temp is reached
 * 4.Use different fan speeds depending on temp
 * 5.Implement an alarm reset system using a code from several button presses
 * 6.It should all only work when an LDR senses the lights are on
 * 7.Use a potentionmeter as a calibration mechanism
 * 8.Print out an array of info when a button is pressed.
 * 9.Allow all aspects of the system to be configured using variables
 */

#include <math.h>
const int rawADC = 0; // Use Pin 0 for analog input
int potenPin = 1;
int ldrPin = 2;
int buttonPin6 = 6;
int buttonPin7 = 7;
int speakerPin = 9;
int ledPin10 = 10;
int ledPin11 = 11;
int ledPin12 = 12;
int fanPin = 13;
float vcc = 5;
float balance = 10000;
float thermr = 10000;
long temp;



//The steinhart-hart function called "Thermistor" to determine temp
double Thermistor(int rawADC){
  long Resistance;
  float Temporary;
  Resistance=((1024 * balance / rawADC) - balance);
  Temporary = log(Resistance); // Saving the Log(resistance) so you don't need to calculate it later
  Temporary = 1 / (0.001346239 + (0.0002309426 * Temporary) + (0.000000098159 * Temporary * Temporary * Temporary));
  Temporary = Temporary - 273.15; // Convert Kelvin to Celsius
  Temporary;
  return Temporary; // Return the temperature
}
//End of the steinhart-hart function called "Thermistor"

//Lighting LEDs based on temp//
void LEDs(int currentTemp){            //NEW STUFF!  Note "int currentTemp" being declared to recieve the temp variable
  //Declare the LED pins as output
  pinMode(ledPin10, OUTPUT);
  pinMode(ledPin11, OUTPUT);
  pinMode(ledPin12, OUTPUT);
  int range;
  range = map(currentTemp, 0, 50, 0, 3);  //Changed to use the variable that "temp" arrives in the function in

  switch (range) {
  case 0: // no LEDs
    Serial.println("All LEDs off");
    digitalWrite(ledPin10, LOW);
    digitalWrite(ledPin11, LOW);
    digitalWrite(ledPin12, LOW);
    break;
  case 1: // 1 LED
    Serial.println("First LED on");
    digitalWrite(ledPin10, HIGH);
    digitalWrite(ledPin11, LOW);
    digitalWrite(ledPin12, LOW);
    break;
  case 2: // 2 LEDs
    Serial.println("First 2 LEDs on");
    digitalWrite(ledPin10, HIGH);
    digitalWrite(ledPin11, HIGH);
    digitalWrite(ledPin12, LOW);
    break;
  case 3: // 3 LEDs
    Serial.println("All 3 LEDs on");
    digitalWrite(ledPin10, HIGH);
    digitalWrite(ledPin11, HIGH);
    digitalWrite(ledPin12, HIGH);
    break;
  }
}
//End of LED function

//Sounding an Alarm based on temp
void Alarm(){
}
//End of Alarm function

void setup() {
  Serial.begin(9600);
}

void loop() {
  if(analogRead(ldrPin) > 100){
    //Do the entire rest of the program, if the lights are ON
    temp=Thermistor(rawADC); // read ADC, convert it to Celsius and put it into the "temp" variable
    LEDs(temp);  //Passing the "temp" variable, freshly filled by the line above, to LEDs

  }

  //Don't do anything if the lights are off
  else{
    Serial.println("Operation halted due to lack of light");
    delay(10);  //As-is the repeated serial printing may choke things without a bit of a delay)
  }
}

As a sidenote, if you put code tags around your code: [code] Your code goes here! [/code] it shows up more nicely :D


Second sidenote: If you can bring an Arduino home to play with, do so!
Failing that, it may be worthwhile to buy one, or a clone and a FTDI board.
 
You're a legend!
I will look at that tomorrow (It's midnight here in Australia).

Yeah, I've borrowed an Arduino and plugged in all my components ready to go. It took ages plugging it all in correctly so I'd like to try and figure out as much as possible this week while I've got it. I'll remember that code thing too, thanks.
 
Donno bout a legend, more like someone who struggled for hours on that routine (those routines, really), but thanks regardless :D


Hooking stuff up can be maddening, just wait till you do your first perfboarding! It's a headache, but sooooooo awesome when you have it working and can put it in a box or wrap tape around it and put it into service :D
 
OK I fixed up my code, based on your suggestions.
Then I commented out the part about the LDR (for now) to try and test the Thermistor.

My void loop looks like:
Code:
void loop() {
//  int lightReading
//  lightReading = lightLevel()
//    if(lightReading > 100){
//Do the entire rest of the program, if the lights are ON
  float temp;
  temp=Thermistor(rawADC);       // read ADC, convert it to Celsius and put it into the "temp" variable
  
  LEDs(temp);
  Serial.println(temp);
  delay(1000);
  
  
    }


The serial monitor:
All LEDs off
-2147483648.00

x infinity (even when I held a cigarette lighter near the thermistor for a minute)

Something wrong with the steinhart function I guess...
 
You aren't actually reading the temp anywhere.
I missed it till now, but the first line of your temp reading function should be
Code:
tempTemp = analogRead(rawADC)
Then swap tempTemp into the place of rawADC in the equation itself.

Right now the temp function is assuming that the thermister is returning a value of zero, the result being the Arduino divides by zero, with dubious results.


Essentially it's a similar issue to the previous: Reusing variable names. rawADC is a constant (unchangable) set to zero, that might be better named "tempPin" or "thermisterPin" or something like that. Then the equation function is re-declaring rawADC to use as the thermister's output, even if you said "rawADC = analogRead(0)" rawADC would still be zero, due to being a constant.


We're getting there, this is progress!


For bonus points, the Arduino has answered the question of what infinity is.
 
I think I changed everything as you wanted....but I get the same results...

Code:
/*
* Carl White ICT106 Project 01
* Arduino climate control device
*
* 1.it needs to light LEDs depending on temp
* 2.it needs to sound an alarm when too hot
* 3.it needs to activate a fan through a transistor after a certain temp is reached
* 4.Use different fan speeds depending on temp
* 5.Implement an alarm reset system using a code from several button presses
* 6.It should all only work when an LDR senses the lights are on
* 7.Use a potentiometer as a calibration mechanism
* 8.Print out an array of info when a button is pressed.
* 9.Allow all aspects of the system to be configured using variables
*/

#include <math.h>
const int thermPin = 0;         // Use Pin 0 for thermistor analog input
      int potenPin = 1;
      int ldrPin = 2;
      int buttonPin6 = 6;
      int buttonPin7 = 7;
      int speakerPin = 9;
      int ledPin10 = 10;
      int ledPin11 = 11;
      int ledPin12 = 12;
      int fanPin = 13;
float vcc = 5;
float balance = 10000;
float thermr = 10000;
long temp;





//The steinhart-hart function called "Thermistor" to determine temp
double Thermistor(int thermPin){
  long Resistance;
  float Temporary;
  long temptemp;
  temptemp = analogRead(thermPin);
  Resistance = ((1024 * balance / temptemp) - balance); 
  Temporary = log(Resistance); // Saving the Log(resistance) so you don't need to calculate it later
  Temporary = 1 / (0.001346239 + (0.0002309426 * Temporary) + (0.000000098159 * Temporary * Temporary * Temporary));
  Temporary = Temporary - 273.15;  // Convert Kelvin to Celsius
  temp = Temporary;
  return temp;                 // Return the temperature
}
//End of the steinhart-hart function called "Thermistor"




//Fan Function
void fanSpin(temp){
  pinMode(fanPin, OUTPUT);
  int range;
  range = map(temp, 0, 50, 0, 3);
  
  switch (range) {
        case 0;  //slowest speed
    Serial.println("fan off");
    digitalWrite(fanPin, LOW);
    delay(1000);
    
        case 1;  //slowest speed
    Serial.println("slowest fan speed");
    digitalWrite(fanPin, HIGH);
    delay(750);
    digitalWrite(fanPin, LOW);
    delay(750);
    
        case 2;  //medium speed
    Serial.println("medium fan speed");
    digitalWrite(fanPin, HIGH);
    delay(500);
    digitalWrite(fanPin, LOW);
    delay(500);
    
        case 3;  //fastest speed
    Serial.println("fastest fan speed");
    digitalWrite(fanPin, HIGH);
    delay(250);
    digitalWrite(fanPin, LOW);
    delay(250);
  }
}




//Lighting LEDs based on temp//
void LEDs(temp){
  //Declare the LED pins as output
  pinMode(ledPin10, OUTPUT);
  pinMode(ledPin11, OUTPUT);
  pinMode(ledPin12, OUTPUT);
  
  int range;
  range = map(temp, 0, 50, 0, 3);

  switch (range) {
  case 0:    // no LEDs
    Serial.println("All LEDs off");
    digitalWrite(ledPin10, LOW);
    digitalWrite(ledPin11, LOW);
    digitalWrite(ledPin12, LOW);
    break;
  case 1:    // 1 LED
    Serial.println("First LED on");
    digitalWrite(ledPin10, HIGH);
    digitalWrite(ledPin11, LOW);
    digitalWrite(ledPin12, LOW);
    break;
  case 2:    // 2 LEDs
    Serial.println("First 2 LEDs on");
    digitalWrite(ledPin10, HIGH);
    digitalWrite(ledPin11, HIGH);
    digitalWrite(ledPin12, LOW);
    break;
  case 3:    // 3 LEDs
    Serial.println("All 3 LEDs on");
    digitalWrite(ledPin10, HIGH);
    digitalWrite(ledPin11, HIGH);
    digitalWrite(ledPin12, HIGH);
    break;
 }
 }
//End of LED function




//Sounding an Alarm based on temp
void Alarm(){
  
}
//End of Alarm function





//Reading the light level from the LDR
//void lightLevel()


  
//End of light level function





void setup() {
     Serial.begin(9600);
}

void loop() {
//  int lightReading
//  lightReading = lightLevel()
//    if(lightReading > 100){
//Do the entire rest of the program, if the lights are ON
  float temp;
  temp=Thermistor(thermPin);       // read ADC, convert it to Celsius and put it into the "temp" variable
  
  LEDs(temp);
  Serial.println(temp);
  delay(1000);
  
  
    }
//Don't do anything if the lights are off
//  else{
//  Serial.println("Operation halted due to lack of light");

I've been working on the fan thing too.
And lol....we it's dividing by 0? I'm surprised my computer hasn't blown up or something....
 
Thing to do now is to have it report all the variables by serial after each step of the equation.
It's a pain to parse (I like to put a , between each number), but then you can track down where things go pear shaped.
 
Thing to do now is to have it report all the variables by serial after each step of the equation.
It's a pain to parse (I like to put a , between each number), but then you can track down where things go pear shaped.

OK I Just added the very first one....

Code:
//The steinhart-hart function called "Thermistor" to determine temp
double Thermistor(int thermPin){
  long Resistance;
  float Temporary;
  long temptemp;
  temptemp = analogRead(thermPin);
  Serial.println(temptemp);
  Resistance = ((1024 * balance / temptemp) - balance); 
  Temporary = log(Resistance); // Saving the Log(resistance) so you don't need to calculate it later
  Temporary = 1 / (0.001346239 + (0.0002309426 * Temporary) + (0.000000098159 * Temporary * Temporary * Temporary));
  Temporary = Temporary - 273.15;  // Convert Kelvin to Celsius
  temp = Temporary;
  return temp;                 // Return the temperature
}
//End of the steinhart-hart function called "Thermistor"

the temptemp is showing 0.....it's not sensing any thermal energy at all?

I just realised I never set thermPin to Input......which I did now.....it still hasn't changed the results though.....dammit
 
That means there's a short somewhere in the thermister circuit and no voltage is getting to the Arduino.

What I would do is thusly:
Write a quick and simple analogRead-serial sketch to see what's happening on that pin
Code:
void setup(){
  Serial.begin
}

void loop(){
  Serial.println(analogRead(0));
  delay(10);
}
Obviously you need to set the pin being read to whatever the thermister is connected to.
Then you can play with the circuit till you get reasonable values out of it.
 
Back