Pages: [1]   Go Down
Author Topic: How can I convert a float to an int? Also, temperature controlled LED problems?  (Read 470 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 60
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

First, I need to know how to completely get rid of the decimals on a float or rounding the float to the nearest whole number. Second I am trying to make it so when the temperature is 55 or higher than the LED is green, and when it is less than 55 the LED is blue, but it always seems to stay on green. Here's my code:

Code:
#include <TVout.h>
#include <fontALL.h>
#include <OneWire.h>

TVout tv;
int DS18S20_Pin = 2;
int r = 11;
int g = 12;
int b = 13;
void wb()
{
    digitalWrite(b,HIGH);
    digitalWrite(r,LOW);
    digitalWrite(g,LOW);
}
void wg()
{
  digitalWrite(b,LOW);
  digitalWrite(r,LOW);
  digitalWrite(g,HIGH);
}
void wr()
{
  digitalWrite(b,LOW);
  digitalWrite(r,HIGH);
  digitalWrite(g,LOW);
}
OneWire ds(DS18S20_Pin);
void setup() {
  int t = getTemp() * 9/5 + 32;
  tv.begin(NTSC,120,96);
  tv.select_font(font4x6);
  tv.print(0,0,"Temperature Logger");
  tv.print(0,7,"------------------");
  tv.print(0,14,"Outside: ");
  tv.print(33,14,t);
  
  //LET THERE BE EPIC CODE!
  if(t=1)
  {
    wb();
  }
  if(t=2)
  {
    wb();
  }
  if(t=3)
  {
    wb();
  }
  if(t=3)
  {
    wb();
  }
  if(t=4)
  {
    wb();
  }
  if(t=5){wb();}
  if(t=6){wb();}
  if(t=7){wb();}
  if(t=8){wb();}
  if(t=9){wb();}
  if(t=10){wb();}
  if(t=11){wb();}
  if(t=12){wb();}
  if(t=13){wb();}
  if(t=14){wb();}
  if(t=15){wb();}
  if(t=16){wb();}
  if(t=17){wb();}
  if(t=18){wb();}
  if(t=19){wb();}
  if(t=20){wb();}
  if(t=21){wb();}
  if(t=22){wb();}
  if(t=23){wb();}
  if(t=24){wb();}
  if(t=25){wb();}
  if(t=26){wb();}
  if(t=27){wb();}
  if(t=28){wb();}
  if(t=29){wb();}
  if(t=30){wb();}
  if(t=31){wb();}
  if(t=32){wb();}
  if(t=33){wb();}
  if(t=34){wb();}
  if(t=35){wb();}
  if(t=36){wb();}
  if(t=37){wb();}
  if(t=38){wb();}
  if(t=39){wb();}
  if(t=40){wb();}
  if(t=41){wb();}
  if(t=42){wb();}
  if(t=43){wb();}
  if(t=44){wb();}
  if(t=45){wb();}
  if(t=46){wb();}
  if(t=47){wb();}
  if(t=48){wb();}
  if(t=49){wb();}
  if(t=50){wb();}
  if(t=51){wb();}
  if(t=52){wb();}
  if(t=53){wb();}
  if(t=54){wb();}
  if(t=55){wg();}
  if(t=56){wg();}
  if(t=57){wg();}
  if(t=58){wg();}
  if(t=59){wg();}
  if(t=60){wg();}
  if(t=61){wg();}
  if(t=62){wg();}
  if(t=63){wg();}
  if(t=64){wg();}
  if(t=65){wg();}
  if(t=66){wg();}
}

void loop() {
  //nothing here
}

int getTemp(){
 //returns the temperature from one DS18S20 in DEG Celsius

 byte data[12];
 byte addr[8];

 if ( !ds.search(addr)) {
   //no more sensors on chain, reset search
   ds.reset_search();
   return -1000;
 }

 if ( OneWire::crc8( addr, 7) != addr[7]) {
   return -1000;
 }

 if ( addr[0] != 0x10 && addr[0] != 0x28) {
   return -1000;
 }

 ds.reset();
 ds.select(addr);
 ds.write(0x44,1); // start conversion, with parasite power on at the end

 byte present = ds.reset();
 ds.select(addr);  
 ds.write(0xBE); // Read Scratchpad

 
 for (int i = 0; i < 9; i++) { // we need 9 bytes
  data[i] = ds.read();
 }
 
 ds.reset_search();
 
 byte MSB = data[1];
 byte LSB = data[0];

 int tempRead = ((MSB << 8) | LSB); //using two's compliment
 int TemperatureSum = tempRead / 16;
 
 return TemperatureSum;
 
}

Why is the LED always green?
I am using a DS18B20 temperature sensor.
« Last Edit: July 06, 2012, 01:47:50 pm by CDCosma » Logged

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
 //LET THERE BE EPIC CODE!
  if(t=1)
  {
    wb();
  }
  if(t=2)
  {
    wb();
  }
  if(t=3)
  {
    wb();
  }
  if(t=3)
  {
    wb();
  }
  if(t=4)
  {
    wb();
  }
  if(t=5){wb();}
  if(t=6){wb();}
  if(t=7){wb();}
  if(t=8){wb();}
  if(t=9){wb();}
  if(t=10){wb();}
  if(t=11){wb();}
  if(t=12){wb();}
  if(t=13){wb();}
  if(t=14){wb();}
  if(t=15){wb();}
  if(t=16){wb();}
  if(t=17){wb();}
  if(t=18){wb();}
  if(t=19){wb();}
  if(t=20){wb();}
  if(t=21){wb();}
  if(t=22){wb();}
  if(t=23){wb();}
  if(t=24){wb();}
  if(t=25){wb();}
  if(t=26){wb();}
  if(t=27){wb();}
  if(t=28){wb();}
  if(t=29){wb();}
  if(t=30){wb();}
  if(t=31){wb();}
  if(t=32){wb();}
  if(t=33){wb();}
  if(t=34){wb();}
  if(t=35){wb();}
  if(t=36){wb();}
  if(t=37){wb();}
  if(t=38){wb();}
  if(t=39){wb();}
  if(t=40){wb();}
  if(t=41){wb();}
  if(t=42){wb();}
  if(t=43){wb();}
  if(t=44){wb();}
  if(t=45){wb();}
  if(t=46){wb();}
  if(t=47){wb();}
  if(t=48){wb();}
  if(t=49){wb();}
  if(t=50){wb();}
  if(t=51){wb();}
  if(t=52){wb();}
  if(t=53){wb();}
  if(t=54){wb();}
  if(t=55){wg();}
  if(t=56){wg();}
  if(t=57){wg();}
  if(t=58){wg();}
  if(t=59){wg();}
  if(t=60){wg();}
  if(t=61){wg();}
  if(t=62){wg();}
  if(t=63){wg();}
  if(t=64){wg();}
  if(t=65){wg();}
  if(t=66){wg();}
}

Dear god, what have you done?
Use the operators >, >=, <, <= for this. That cuts that down to 3-4 lines of code. The reason why it's not working now is because t=65 is an assignment, not a comparison. t==65 is a comparison.

To drop the decimals from a number it's as simple as this:
Code:
float pi = 3.14
int three = pi;
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 241
Posts: 24470
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You do know the difference between = and == ?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 553
Posts: 46267
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Where is the code to set the LED pins as OUTPUT? Rather important to do that. Turning the pullup resistor on or off doesn't accomplish much for pins that are intended to be output pins.
Logged

Pages: [1]   Go Up
Jump to: