Monday, February 7, 2011

getting closer

with allot of thanks to renoir he sent me some code to use. in a comment on the last post,

i have had to refine it some more still and i have one more bug to work out.


#define THERMISTOR_PIN1 0
#define THERMISTOR_PIN2 1


// 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);


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

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

  Serial.println("");


  delay(500);
}

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;  // if i leave break here it will not compile, but when it dose i dont get any output that i can            //see
  }
  }

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

  return current_celsius;
}




    /break;  // if i leave break here it will not compile, but when it dose i dont get any output that i can            //see
the issue i am having is that on my serial connection all i get so far is just ther very first comment and nothing after that. so i think there is another bug or two in there some where the other bug seems to be related to the break command, the compiler says it is missing a statement before it but i am not sure how to fix that. has far as i can tell there is an IF statement in front of it.

 i think this will be a handy tool when it is done, so i hope that other people will find it useful

i would love feed back on the code, so please leave a comment

3 comments:

  1. sorry i changed the code, i put the wrong one in there, this is the one that will compile now. but that dose not give me the updates.

    ReplyDelete
  2. Note to self : be careful when cutting/pasting code into the comment box - it didn't come out quite as expected !!!

    ReplyDelete
  3. hi, check out https://github.com/triffid/FiveD_on_Arduino/blob/master/temp.c#L191

    your algorithm is broken- you need to find the two table entries either side of your ADC reading then do a linear interpolation on the temperature values. You're using all values of i rather than choosing the relevant one.

    My code uses 14.2 fixed point temperatures and table stored in flash, apart from that I think it's what you want.

    Official FiveD uses the same algorithm as far as I know

    ReplyDelete