Go Down

### Topic: Binary Thermostat - First Project (Read 1 time)previous topic - next topic

#### superTallDan

##### May 22, 2011, 06:06 am
This is my first arduino project.  I am not much of a programmer or an electronics guy so I am looking for some feedback.  Be as kind or cruel as you like.

Code: [Select]
int ledPins[] = {2,3,4,5,6,7,8,9,10}; // Initialize array of led pins
int temperaturePin = 0;  //Initialize temperature pin

void setup()
{
for(int i = 0; i < 9; i++){  //set led pins as outputs
pinMode(ledPins[i],OUTPUT);
}
}
void loop()
{
float tempC = getVoltage(temperaturePin);  //getting the voltage reading from the temperature sensor
tempC = (tempC - .5) * 100;          //converting from 10 mv per degree wit 500 mV offset
float tempF = tempC*1.8+32;         //convert C to F
int abstempF = fabs(tempF);         // take abs value of temp in F
if(tempF <0)                        //light red pin if temp in F is negative
digitalWrite(ledPins[8],HIGH);
else
digitalWrite(ledPins[8],LOW);    //do not light red pin if temp in F is positive
for(int i = 7; i >= 0; i--){
if (pow(2,i) <= abstempF) {        //converts decimal to binary one digit at a time (assumes temp in F cannot be above 127)
digitalWrite(ledPins[i], HIGH);
abstempF=abstempF-pow(2,i);
}
else
digitalWrite(ledPins[i], LOW);
}
delay(1000);
}
float getVoltage(int pin){
return (analogRead(pin) * .004882814); //converting from a 0 to 1024 digital range
// to 0 to 5 volts (each 1 reading equals ~ 5 millivolts
}

#### johnwasser

#1
##### May 22, 2011, 04:04 pm
Looks pretty good.  I have a few suggestions:
Code: [Select]

//// Since these values don't change I'd recommend using the 'const' storage type.  This will allow
//// the compiler to warn you if you accidentally try to change them at run time.  It also allows for
//// better optimization because the compiler knows the values won't change.

const int ledPins[] = {2,3,4,5,6,7,8,9,10}; // Initialize array of led pins
const int temperaturePin = 0;  //Initialize temperature pin

//// Since you treat the red LED (#8) separately it might be clearer to have it in a separate declaration:
const int redLED = 10;
const int ledPins[] = {2,3,4,5,6,7,8,9}; // Initialize array of led pins

void setup()
{
//// Rather than use a magic constant here you can use a formula that will always match the size of the
//// array of pins:
for(int i = 0; i < (sizeof ledPins/sizeof ledPins[0]); i++)
////  Note that the 'sizeof' operator gives an answer in bytes.
//// We have to divide by the size of a single element to get the array size in elements
{  //set led pins as outputs
pinMode(ledPins[i],OUTPUT);
}
////  And if we treat the red LED as separate:
pinMode(redLED, OUTPUT);
}
void loop()
{
float tempC = getVoltage(temperaturePin);  //getting the voltage reading from the temperature sensor
tempC = (tempC - .5) * 100;          //converting from 10 mv per degree wit 500 mV offset
float tempF = tempC*1.8+32;         //convert C to F
int abstempF = fabs(tempF);         // take abs value of temp in F

if(tempF <0)                        //light red pin if temp in F is negative
digitalWrite(redLED,HIGH);
else
digitalWrite(redLED,LOW);    //do not light red pin if temp in F is positive

////  Since HIGH and LOW are synonyms for 'true' and 'false' this can simplify to:
digitalWrite(redLED, tempF < 0);  // Light the red LED if the temperature is below 0

for(int i = 7; i >= 0; i--){
////
//// WARNING: the pow() function works on float values and the conversions can cause round-off errors
//// For integer powers of 2 it is best to use the left shift operator <<
////
if ((1<<i) <= abstempF) {        //converts decimal to binary one digit at a time (assumes temp in F cannot be above 127)
digitalWrite(ledPins[i], HIGH);

////  When you have something that looks like "variable = variable op value" you can use the C shorthand assignment operators;
////
////   X = X + Y;  can be written X += Y;
////   X = X - Y;  can be written X -= Y;
////   X = X * Y;  can be written X *= Y;
////
abstempF -= (i<<i);
}
else
digitalWrite(ledPins[i], LOW);
}
delay(1000);
}
float getVoltage(int pin){
return (analogRead(pin) * .004882814); //converting from a 0 to 1024 digital range
// to 0 to 5 volts (each 1 reading equals ~ 5 millivolts
}

[/quote]
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

#2
John,

Thanks!

-Dan

#### bsautner

#3
##### May 26, 2011, 04:56 pm
Hi Dan looks good,

I was just passing by and noticed your post. If you want to have some fun with this, I have an open source project going where you could add a few lines of code to this and post your values onto the nimbits data logger system (www.nimbits.com) and your temps can be relayed onto twitter, facebook, email alerts etc.

Anyway I just noticed that your arduino code is similar enough to what I posted under the arduino example on nimbits.com and since it's connected to an ethernet shield it should be pretty easy. Just thought i'd mention it.

- Ben

Go Up

Please enter a valid email to subscribe