DS1820B value stored as a variable?

This is a special forum devoted to educators using Adafruit and Arduino products for teaching.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
crafty184
 
Posts: 21
Joined: Tue Oct 08, 2013 12:14 pm

DS1820B value stored as a variable?

Post by crafty184 »

I have a FLORA connected successfully to a DS1820B waterproof temperature sensor. I can run the Dallas Temperature test code (see http://milesburton.com/Dallas_Temperatu ... ol_Library) and it detects temperature just fine.

My goal is to store the temperature detected by the sensor into a variable and then develop a switch case around it.

I am having trouble coding the "storing" part. The library has a command getTempF and I want to use that. How do I call that command and then cause it to store the value detected (in F) into a variable I can later use in my switch case?

Thanks!

Chris Craft
Educator
Columbia, SC

User avatar
adafruit_support_bill
 
Posts: 88037
Joined: Sat Feb 07, 2009 10:11 am

Re: DS1820B value stored as a variable?

Post by adafruit_support_bill »

Code: Select all

  // returns temperature in degrees F
  float getTempF(uint8_t*);
getTempF() returns a floating point value. To store that in a variable simply assign the result to a float:

Code: Select all

float temperatureF = getTempF(address);  // address
But a floating point number can't be used in a switch/case statement. The cases need to map to integers. If you can explain what you are trying to achieve, maybe we can suggest an alternate approach.

User avatar
crafty184
 
Posts: 21
Joined: Tue Oct 08, 2013 12:14 pm

Re: DS1820B value stored as a variable?

Post by crafty184 »

Thanks for your quick reply.

We are creating a dog collar that "reads" the temperature of the dog (through the temp sensor near the skin) and lights up a neopixel that corresponds with the temp. We've sewn on blue (cool), white (less cool), rose pink (warm) and red (dangerous).

Our intent is to have it used on police K9's to keep them safe in summer. It's hot here in SC and each year a K9 or two dies due to overheating.

Just for fun, I've added a picture in case you're curious. It's a proof of concept (or MVP) right now, but it's a neat project the kids are working on.

I couldn't attach the picture (said it was invalid) so here's a link to it https://www.dropbox.com/s/32q7pqls7cvwg ... 7.jpg?dl=0.

Chris

User avatar
adafruit_support_bill
 
Posts: 88037
Joined: Sat Feb 07, 2009 10:11 am

Re: DS1820B value stored as a variable?

Post by adafruit_support_bill »

Nice project!

To test for temperature ranges, use an if/else structure something like this:

Code: Select all

if (temperature > 100)
{
    // set led to red
}
else if (temperature > 90)
{
    // set led pink
}
else if (temperature > 80)
{
    // set led white
}
else
{
    // set led blue
}

User avatar
Franklin97355
 
Posts: 23902
Joined: Mon Apr 21, 2008 2:33 pm

Re: DS1820B value stored as a variable?

Post by Franklin97355 »

Chris, just some thoughts. If you are using "neopixels" they will show all colors in one pixel, you don't need individual pixels. instead of using the waterproof ds18b20 and it's bulk you could use the chip itself and then waterproof the entire package somehow.

User avatar
crafty184
 
Posts: 21
Joined: Tue Oct 08, 2013 12:14 pm

Re: DS1820B value stored as a variable?

Post by crafty184 »

Thanks, those are great thoughts. I'm a novice at this, so this helps a bunch.

Turns out I accidentally lied, I'm using LED Sequins for this, not NeoPixels. Oops!

User avatar
Franklin97355
 
Posts: 23902
Joined: Mon Apr 21, 2008 2:33 pm

Re: DS1820B value stored as a variable?

Post by Franklin97355 »

I saw that in the picture. There are these to consider https://www.adafruit.com/product/1260

User avatar
crafty184
 
Posts: 21
Joined: Tue Oct 08, 2013 12:14 pm

Re: DS1820B value stored as a variable?

Post by crafty184 »

Would anyone be willing to review my code? It doesn't work yet, but I think I'm close. Thanks for the help!!

Code: Select all

#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 2

int temperature;

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

int redled = 12;
int blueled = 6;
int whiteled = 9;
int pinkled = 10;

void setup(void)
{
  Serial.begin(9600);
  sensors.begin(); 
  float getTempF(uint8_t*);
  pinMode(redled, OUTPUT);     
  pinMode(blueled, OUTPUT);
  pinMode(whiteled, OUTPUT);
  pinMode(pinkled, OUTPUT);
}

void loop(void)
{ 
  if (temperature > 100)
{
    digitalWrite(redled, HIGH); 
    delay(2000);
    digitalWrite(redled, LOW);    

}
else if (temperature > 90)
{
    digitalWrite(pinkled, HIGH); 
    delay(2000);
    digitalWrite(pinkled, LOW);
}
else if (temperature > 80)
{
    digitalWrite(whiteled, HIGH); 
    delay(2000);
    digitalWrite(whiteled, LOW);
}
else
{
    digitalWrite(blueled, HIGH); 
    delay(2000);
    digitalWrite(blueled, LOW);
}
}

User avatar
Franklin97355
 
Posts: 23902
Joined: Mon Apr 21, 2008 2:33 pm

Re: DS1820B value stored as a variable?

Post by Franklin97355 »

Code: Select all

  sensors.requestTemperatures(); // Send the command to get temperatures
  
  TempC = sensors.getTempCByIndex(0));  
You need something like this otherwise you are never reading the DS18B20

User avatar
crafty184
 
Posts: 21
Joined: Tue Oct 08, 2013 12:14 pm

Re: DS1820B value stored as a variable?

Post by crafty184 »

Thanks, I thought I was doing that with this line...

Code: Select all

float getTempF(uint8_t*);
I will replace that with the code you reference. Thanks!

User avatar
Franklin97355
 
Posts: 23902
Joined: Mon Apr 21, 2008 2:33 pm

Re: DS1820B value stored as a variable?

Post by Franklin97355 »

Your line is called a definition it tells the compiler there is a function that takes a int and returns a float. That's all it does.

User avatar
crafty184
 
Posts: 21
Joined: Tue Oct 08, 2013 12:14 pm

Re: DS1820B value stored as a variable?

Post by crafty184 »

Ok friends, I really need some help. I think I'm close, but my lack of coding knowledge is causing me and my students to agonize over this.

When I run the code here, it *seems* to detect the temperature once and one LED *does* light up. However, there isn't a loop effect. I'm almost certain my trouble is in the if, else code. Take a look and help me and my kids out?

As an aside, I showed them the blog post about this project, they were SO EXCITED! I doubt Becky Stern ever sees these posts, but if she does, she needs to know she's made a huge impact on some of my students. They got the idea to use the Flora after seeing several of her wearables videos. </aside>

Code: Select all

#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 2

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
float TempF;
int redled = 12;
int blueled = 6;
int whiteled = 9;
int pinkled = 10;

void setup(void)
{
  Serial.begin(9600);
  sensors.begin(); 
  // float getTempF(uint8_t*);
  pinMode(redled, OUTPUT);     
  pinMode(blueled, OUTPUT);
  pinMode(whiteled, OUTPUT);
  pinMode(pinkled, OUTPUT);
}

void loop(void)
{ 
  
  TempF = sensors.getTempFByIndex(0); 
  
  if (TempF > 100)
{
    digitalWrite(redled, HIGH); 

}
else if (TempF > 90)
{
    digitalWrite(pinkled, HIGH); 
    
}
else if (TempF > 70)
{
    digitalWrite(whiteled, HIGH); 
    
}
else
{
    digitalWrite(blueled, HIGH); 
    
}
}

User avatar
adafruit_support_bill
 
Posts: 88037
Joined: Sat Feb 07, 2009 10:11 am

Re: DS1820B value stored as a variable?

Post by adafruit_support_bill »

Your code looks right. I'd add a Serial.println(TempF); after you read the temperature so you can see what the reading is in the Serial Monitor.

And I'll let Becky know that she has fans. :)

User avatar
crafty184
 
Posts: 21
Joined: Tue Oct 08, 2013 12:14 pm

Re: DS1820B value stored as a variable?

Post by crafty184 »

Thanks, Bill.

It's still doing what it's been doing, in that it will detect the temperature when it starts, but the LED never changes, even if the temp sensor detects a different temperature.

I welcome any thoughts, this is driving me nuts.

User avatar
Franklin97355
 
Posts: 23902
Joined: Mon Apr 21, 2008 2:33 pm

Re: DS1820B value stored as a variable?

Post by Franklin97355 »

What values do you get for the temperature after you added the code to print them out?

Locked
Please be positive and constructive with your questions and comments.

Return to “For Educators”