Show Posts
Pages: [1]
1  Forum 2005-2010 (read only) / Troubleshooting / Problem with the code for DS18S20 sensor. on: December 13, 2010, 05:41:46 pm
Hello

I´ve got a problem with a DS18S20 sensor. I´ve used a code from an already existing project but it were built for an DS18B20 sensor.
When I run the code it works but I cant get the right value from the sensor. I run the sensor in a room (21 C degrees) but the sensor says 26 C degrees. Please someone help me. I think I must have missed something in the code that only works for a DS18B20. Hope you understand my bad english smiley

#define TEMP_PIN 5 //

void OneWireReset(int Pin);
void OneWireOutByte(int Pin, byte d);
byte OneWireInByte(int Pin);

void setup() {
    digitalWrite(TEMP_PIN, LOW);
    pinMode(TEMP_PIN, INPUT);      // sets the digital pin as input (logic 1)
Serial.begin(9600);
    delay(100);
    Serial.print("Temperatur:\n");
}

void loop(){
  int HighByte, LowByte, TReading, SignBit, Tc_100, Whole, Fract;

  OneWireReset(TEMP_PIN);
  OneWireOutByte(TEMP_PIN, 0xcc);
  OneWireOutByte(TEMP_PIN, 0x44); // perform temperature conversion, strong pullup for one sec

  OneWireReset(TEMP_PIN);
  OneWireOutByte(TEMP_PIN, 0xcc);
  OneWireOutByte(TEMP_PIN, 0xbe);

  LowByte = OneWireInByte(TEMP_PIN);
  HighByte = OneWireInByte(TEMP_PIN);
  TReading = (HighByte << smiley-cool + LowByte;
  SignBit = TReading & 0x8000;  // test most sig bit
  if (SignBit) // negative
  {
    TReading = (TReading ^ 0xffff) + 1; // 2's comp
  }
  Tc_100 = ((TReading*100/2));    // multiply by 0.5

  Whole = Tc_100 / 100;  // separate off the whole and fractional portions
  Fract = Tc_100 % 100;


  if (SignBit) // If its negative
  {
     Serial.print("-");
  }
  Serial.print(Whole);
  Serial.print(".");
  if (Fract < 10)
  {
     Serial.print("0");
  }

  Serial.print(Fract);

      Serial.print("\n");
  delay(5000);      
}

void OneWireReset(int Pin) // reset.  Should improve to act as a presence pulse
{
     digitalWrite(Pin, LOW);
     pinMode(Pin, OUTPUT); // bring low for 500 us
     delayMicroseconds(500);
     pinMode(Pin, INPUT);
     delayMicroseconds(500);
}

void OneWireOutByte(int Pin, byte d) // output byte d (least sig bit first).
{
   byte n;

   for(n=8; n!=0; n--)
   {
      if ((d & 0x01) == 1)  // test least sig bit
      {
         digitalWrite(Pin, LOW);
         pinMode(Pin, OUTPUT);
         delayMicroseconds(5);
         pinMode(Pin, INPUT);
         delayMicroseconds(60);
      }
      else
      {
         digitalWrite(Pin, LOW);
         pinMode(Pin, OUTPUT);
         delayMicroseconds(60);
         pinMode(Pin, INPUT);
      }

      d=d>>1; // now the next bit is in the least sig bit position.
   }

}

byte OneWireInByte(int Pin) // read byte, least sig byte first
{
    byte d, n, b;

    for (n=0; n<8; n++)
    {
        digitalWrite(Pin, LOW);
        pinMode(Pin, OUTPUT);
        delayMicroseconds(5);
        pinMode(Pin, INPUT);
        delayMicroseconds(5);
        b = digitalRead(Pin);
        delayMicroseconds(50);
        d = (d >> 1) | (b<<7); // shift d to right and insert b in most sig bit position
    }
    return(d);
}

//Will_jr
2  Forum 2005-2010 (read only) / Troubleshooting / Re: Why does the led L lamp blink??? on: January 15, 2010, 12:58:52 pm
I connect a battery eliminator to the board with 9V output. Now it seems to work like I want it to. But still I got problem when it´s connect to the USB. I will try to reinstall everything and add the new FTDI. Do you think this is the problem? Or can it be something else?
Thanks for your feedback//
3  Forum 2005-2010 (read only) / Troubleshooting / Why does the led L lamp blink??? on: January 14, 2010, 06:04:48 pm
I bought my first Arduino Duemilanove 328 for a few days ago. I followed the install instruction. When it was time to plug the board in the led L blink (irregular) already though I did´nt upload the "first" blink example. The resetbutton seems not to work either. I tried to upload other example (blinkwithoutdelay etc etc). It seems to work for a couple of seconds but then it stop working. When I reset, it starts over again (same loop). Still blinking irregular (led L).
Do I have a "broken" board or can I fixed this with some update files?
Please help me!
Pages: [1]