Monday, February 7, 2011

code

i am tired and this code is just making me confused. i want to changed this code form
http://www.thebestcasescenario.com/forum/showthread.php?t=23490

#include 
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);    
double Thermistor(int RawADC) {
  long Resistance;  
  double Temp;
  Resistance=((10240000/RawADC) - 10000);
  Temp = log(Resistance);
  Temp = 1 / (0.001129148 + (0.000234125 * Temp) + (0.0000000876741 * Temp * Temp * Temp));
  Temp = Temp - 273.15;
  return Temp;  
}

void printDouble(double val, byte precision) {

  lcd.print (int(val));
  if( precision > 0) {
    lcd.print(".");
    unsigned long frac, mult = 1;
    byte padding = precision -1;
    while(precision--) mult *=10;
    if(val >= 0) frac = (val - int(val)) * mult;
    else frac = (int(val) - val) * mult;
    unsigned long frac1 = frac;
    while(frac1 /= 10) padding--;
    while(padding--) Serial.print("0");
    lcd.print(frac,DEC) ;
  }
}

void setup() {
  Serial.begin(115200);
  lcd.begin(16, 2);
}

#define ThermistorPIN 0  
#define Thermistor2PIN 1
double temp;
void loop() {
  lcd.setCursor(0, 0);
  temp=Thermistor(analogRead(ThermistorPIN));   
  lcd.print("Sensor 1 ");
  printDouble(temp,2);
  lcd.print((char)223);   
  lcd.print("C   ");
  lcd.setCursor(0, 1);
  temp=Thermistor(analogRead(Thermistor2PIN));
  lcd.print("Sensor 2 ");
  printDouble(temp,2);
  lcd.print((char)223);   
  lcd.print("C   ");  
  delay(100);
}


 i am trying to modify it to allot me to make a csv file on my computer so that i can log temp differences when i am testing my mod from the last post. but it was made for a 10k thermistor and a 10k resistor in a voltage divider circuit, and i am using this circuit http://reprap.org/wiki/Temperature_Sensor_1_1 that is for 100k thermistor and a 4.7k resistor in the circuit.

i am not a much of a math guy but i can normaly work out what dose what. but alas i am stumped with this one.

or if any one knows of code that dose this more cleanly that would help too.

i am also trying to get the code form http://reprap.org/wiki/Temperature_Sensor_1_1 to read 2 thermistors, but i have not been able to figure that one out has well. i have not seen any good example of how to use the same routine to do the math for 2 inputs.

thanks in advance for any help

3 comments:

  1. #include
    // This line sets the pins for the LCD output
    LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

    // This calculates the temperature from the sensor input.
    // It does the calculations based on the thermistor values
    double Thermistor(int RawADC) {
    // define some variables...
    long Resistance;
    double Temp;

    // get the thermistor resistance (Ohms?)
    Resistance=((10240000/RawADC) - 10000);

    // This calculation gives you the temp based on the resistance (ohms)
    Temp = log(Resistance);
    Temp = 1 / (0.001129148 + (0.000234125 * Temp) + (0.0000000876741 * Temp * Temp * Temp));

    // Not sure - convert Celsius to kelvin degrees?
    Temp = Temp - 273.15;

    // return the calculated temperature
    return Temp;
    }

    // Don't worry about this method: it prints a value to the LCD with precision. For example, printDouble (3.141976878 , 2) will display "3.1" on the LCD.

    void printDouble(double val, byte precision) {
    lcd.print (int(val));
    if( precision > 0) {
    lcd.print(".");
    unsigned long frac, mult = 1;
    byte padding = precision -1;
    while(precision--) mult *=10;
    if(val >= 0) frac = (val - int(val)) * mult;
    else frac = (int(val) - val) * mult;
    unsigned long frac1 = frac;
    while(frac1 /= 10) padding--;
    while(padding--) Serial.print("0");
    lcd.print(frac,DEC) ;
    }
    }
    // Used to start the LCD readout.
    void setup() {
    Serial.begin(115200);
    lcd.begin(16, 2);
    }

    // Main loop
    #define ThermistorPIN 0
    #define Thermistor2PIN 1
    double temp;
    void loop() {
    lcd.setCursor(0, 0);
    // Calculate the temperature for thermistor 1
    temp=Thermistor(analogRead(ThermistorPIN));
    //write the temp to the LCD display
    lcd.print("Sensor 1 ");
    printDouble(temp,2);
    lcd.print((char)223);
    lcd.print("C ");
    lcd.setCursor(0, 1);

    // Calc thermistor temp for T2
    temp=Thermistor(analogRead(Thermistor2PIN));
    // Print to LCD
    lcd.print("Sensor 2 ");
    printDouble(temp,2);
    lcd.print((char)223);
    lcd.print("C ");
    delay(100);
    }

    If you just want to get hold of the temp readings to write a CSV, you can use serial output to write the temperatures back to your PC.
    If you change the arduino main loop to (roughly) like below, and run the 'serial monitor' in the arduino software, it should start showing temperatures. You can probably cut and paste into notepad to save a txt file. Alternatively, you could write a short java program to read the serial port and write to a file direct.

    // Main loop
    #define ThermistorPIN 0
    #define Thermistor2PIN 1
    double temp;
    void loop() {
    lcd.setCursor(0, 0);
    // Calculate the temperature for thermistor 1
    temp=Thermistor(analogRead(ThermistorPIN));
    //write the temp back to the PC
    Serial.println(temp);
    Serial.println(",");

    // Calc thermistor temp for T2
    temp=Thermistor(analogRead(Thermistor2PIN));
    // Print back to PC
    Serial.println(temp);
    Serial.println(",");
    // print a new line
    Serial.println("\n");

    // Pause for one second
    delay(1000);
    }

    ReplyDelete
  2. Doh!
    Try the test program from
    http://reprap.org/wiki/Temperature_Sensor_1_1

    Since it's set up for a 100k thermistor and a 4.7k resistor.

    Change the 'Serial.Print' lines to output just the number, then a ,

    :-)
    Hope this helps!!!

    ReplyDelete
  3. Doh! number two.

    OK, actually read your post rather than just the code.

    Right, I've adjusted the original RepRap Test for two thermistors:
    Change THERMISTOR_PIN1 and PIN2 to the pins you want to use.

    #define THERMISTOR_PIN1 1
    #define THERMISTOR_PIN2 2

    // Thermistor lookup table for RepRap Temperature Sensor Boards (http://make.rrrf.org/ts)
    // Made with createTemperatureLookup.py (http://svn.reprap.org/trunk/reprap/firmware/Arduino/utilities/createTemperatureLookup.py)
    // ./createTemperatureLookup.py --r0=100000 --t0=25 --r1=0 --r2=4700 --beta=4066 --max-adc=1023
    // r0: 100000
    // t0: 25
    // r1: 0
    // r2: 4700
    // beta: 4066
    // max adc: 1023
    #define NUMTEMPS 20
    short temptable[NUMTEMPS][2] = {
    {1, 841},
    {54, 255},
    {107, 209},
    {160, 184},
    {213, 166},
    {266, 153},
    {319, 142},
    {372, 132},
    {425, 124},
    {478, 116},
    {531, 108},
    {584, 101},
    {637, 93},
    {690, 86},
    {743, 78},
    {796, 70},
    {849, 61},
    {902, 50},
    {955, 34},
    {1008, 3}
    };

    void setup()
    {
    Serial.begin(9600);
    Serial.println("Starting temperature exerciser.");
    }

    void loop()
    {
    int celsius1 = read_temp(THERMISTOR_PIN1);
    int celsius2 = read_temp(THERMISTOR_PIN2);
    int fahrenheit1 = (((celsius1 * 9) / 5) + 32);
    int fahrenheit2 = (((celsius2 * 9) / 5) + 32);

    Serial.print("Temp 1 ,");
    Serial.print(celsius1);
    Serial.print(", ");

    Serial.print(" Temp 2 ,");
    Serial.print(celsius2);

    Serial.println("");


    delay(1000);
    }

    int read_temp(int pin)
    {
    int rawtemp = analogRead(pin);
    int current_celsius = 0;

    byte i;
    for (i=1; i rawtemp)
    {
    int realtemp = temptable[i-1][1] + (rawtemp - temptable[i-1][0]) * (temptable[i][1] - temptable[i-1][1]) / (temptable[i][0] - temptable[i-1][0]);

    if (realtemp > 255)
    realtemp = 255;

    current_celsius = realtemp;

    break;
    }
    }

    // Overflow: We just clamp to 0 degrees celsius
    if (i == NUMTEMPS)
    current_celsius = 0;

    return current_celsius;
    }


    Email me if you have problems :-)

    ReplyDelete