Go Down

Topic: Temperature sensor + Leds (Read 6022 times) previous topic - next topic

PaulS

When you put float in front of tempc, you created a local variable with the same name as the global variable, with an initial value of 0.0.

From that point on, all references to tempc are to the local variable, until it goes out of scope.

So, the 0.0 values are not surprising.

You need to change the type of the global variable to float, not create a local variable.

AWOL

#16
May 21, 2010, 10:02 pm Last Edit: May 21, 2010, 10:15 pm by AWOL Reason: 1
I loathe unecessary globals:
Code: [Select]
const int pin = 0; // analog pin
float maxi = -100.0;
float mini = 100.0; // to start max/min temperature

void setup()
{
 Serial.begin(9600); // start serial communication
}

void loop()
{
 float tempc = 0.0;

 for(int i = 0; i < 8; i++) {
   tempc += analogRead(pin);
 }

 tempc = (5.0 * tempc * 100.0) / (8.0 * 1024.0);

 if(tempc > maxi) {maxi = tempc;} // set max temperature
 if(tempc < mini) {mini = tempc;} // set min temperature

 Serial.print(tempc, 2);  //or however many decimal places you need
 Serial.print(" Celsius, ");

 Serial.print(maxi, 2);
 Serial.print(" Max, ");
 Serial.print(mini, 2);
 Serial.println(" Min");

 delay(1000); // delay before loop
}
"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.

D3C3PT1C0N

#17
May 21, 2010, 10:11 pm Last Edit: May 21, 2010, 10:13 pm by Fredx Reason: 1
Using PaulS suggestions ended up like this

Quote

int pin = 0; // analog pin
int tempc = 0; // temperature variables
int tempb = 0;
int maxi = -100,mini = 100; // to start max/min temperature
int i;

void setup()
{
Serial.begin(9600); // start serial communication
}

void loop()
{

for(i = 0;i<=7;i++)
{
tempc += analogRead(pin);
}

float tempb = (5.0 * tempc * 100.0) / (8.0 * 1024.0);

if(tempb > maxi) {maxi = tempb;} // set max temperature
if(tempb < mini) {mini = tempb;} // set min temperature

Serial.print(tempb,DEC);
Serial.print(" Celsius, ");

Serial.print(maxi,DEC);
Serial.print(" Max, ");
Serial.print(mini,DEC);
Serial.println(" Min");

tempc = 0;
tempb = 0;

delay(1000); // delay before loop
}


And serial monitor
Quote

23.4375000000 Celsius, 23 Max, 23 Min
23.4375000000 Celsius, 23 Max, 23 Min

...

AWOL

#18
May 21, 2010, 10:16 pm Last Edit: May 21, 2010, 10:17 pm by AWOL Reason: 1
Code: [Select]
int tempb = 0;
Why?

Code: [Select]
Serial.print(tempb,DEC);
Isn't ten places of decimals excessive?
"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.

D3C3PT1C0N

#19
May 21, 2010, 10:16 pm Last Edit: May 21, 2010, 10:20 pm by Fredx Reason: 1
AWOL,
using your code, serial monitor show this:
Quote

23.44 Celsius, 10111 Max, 10111 Min
23.44 Celsius, 10111 Max, 10111 Min
...


It is good that eventually there are 2 decimal places, but have to manage out why are Max / Min displayed in binary ( is it bin ? )

Serial.print(tempc, 2); -> shows 23.44
but
Serial.print(maxi, 2); -> shows 11010

AWOL

#20
May 21, 2010, 10:19 pm Last Edit: May 21, 2010, 10:19 pm by AWOL Reason: 1
Have another look at reply 16.
"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.

D3C3PT1C0N

#21
May 21, 2010, 10:25 pm Last Edit: May 21, 2010, 10:28 pm by Fredx Reason: 1
Reply #16 working fine!
Big thanks to PaulS, Groove and AWOL. As I just got into Arduino-ing, I don't understand the programming very well. Actually I have never done programs before :P

AWOL

The reason you were getting ten places of decimals is because "DEC" has the value 10.

For integer datatypes, the "print" method uses the second parameter as a base, so "DEC" = 10, "BIN" = 2, "HEX" = 16. So, printing an integer with the second parameter of 2 prints the result in binary.

For floats, the "print" method specifies the number of decimal places (from 0018) to be printed.
"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.

D3C3PT1C0N

#23
May 21, 2010, 10:48 pm Last Edit: May 21, 2010, 11:08 pm by Fredx Reason: 1
Understood. Thanks again!

---
I just came up with an idea to display the temperature with only 2 LEDs. But I have no idea how to make it work.  The idea -
Quote

Lets say the temperature is 25 degrees.
There are 2 LEDs connected to Arduino.
The script should make the first led blink TWO times
and after that the second led FIVE times.


Is it very hard to make it? Any ideas from where to start?

Quote

IF the FIRST number is 1,
THEN (led HIGH).... delay(1000) .... (led LOW)

IF the FIRST number is 2,
THEN (led HIGH).... delay(1000) .... (led LOW)...delay(1000)..(led HIGH).... delay(1000) .... (led LOW)

IF the FIRST number is 3,
THEN (led HIGH).... delay(1000) .... (led LOW)...delay(1000)..(led HIGH).... delay(1000) .... (led LOW)...delay(1000)...(led HIGH).... delay(1000) .... (led LOW)

ELSE IF the FIRST number is 0,
THEN DO nothing



IF the SECOND number is 1,
THEN (led HIGH).... delay(1000) .... (led LOW)

etc...


AWOL

Think about how you'd break down "25" into "2" and "5".
The rest is simple.
Ten to fifteen lines of code, maximum.
"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.

D3C3PT1C0N

#25
May 22, 2010, 12:01 am Last Edit: May 22, 2010, 12:08 am by Fredx Reason: 1
Found something that may be useful
Code: [Select]

int extractDigit(int number, int place)
{
for(int i = 0; i < place; ++i)
number /= 10; //get rid of the preceding digits
return number % 10; //now ignore all of the following ones
}

though this is C++

the key to extracting digits is using integer and modular division. You could directly access no. 9 in 1981 with (1981 / 100) % 10. The reason this works is that 1981/100 = 19, and 19 % 10 = 9.

Also found premade 7-segment script
Quote

#define NUM_DIGITS 4

// table of 7-segment patterns
const byte seg_def[]  = // segment patterns for digits 0-9
 {//GFEDCBA
   B0111111,  // '0'
   B0000110,  // '1'
   B1011011,  // '2'
   B1001111,  // '3'
   B1100110,  // '4'
   B1101101,  // '5'
   B1111101,  // '6'
   B0000111,  // '7'
   B1111111,  // '8'
   B1100111   // '9'
 };

byte digit_array[NUM_DIGITS];

void NumberToArray(word number)
{
 char i;

 // initialize all digits to the empty pattern (no segments lit)
 for (i=0;i<NUM_DIGITS;i++) digit_array=0;

 // extract each digit (right to left)
 for (i=NUM_DIGITS-1;i>=0;i--) {
   // extract least significant digit as an index into the
   // segment pattern table
   digit_array=seg_def[number%10];
   // get next digit
   number=number/10;
   // exit if there are no more digits to avoid leading zeroes
   if (number==0) break;
 }/*for*/
}/*NumberToArray*/



Ok, this goes out of hand. It is 01:07 at night, must go to sleep

PaulS

Quote
though this is C++


Good thing that's what the Arduino is programmed in.

D3C3PT1C0N

#27
May 22, 2010, 12:13 am Last Edit: May 22, 2010, 11:25 am by Fredx Reason: 1
That was bad..


I suppose, this should do the trick
Code: [Select]

void digits ()                            // Function to display the digits
{                    
   tempc_ones = tempc % 10;           // Get the 1's digit of degrees
   if (tempc>=10){                      // If degrees greater than 10
   tempc_tens = tempc / 10;}         // Get 10's digit of degrees
   else {tempc_tens = 0;}               // Otherwise 10s is 0
{


D3C3PT1C0N

#28
May 22, 2010, 10:29 am Last Edit: May 22, 2010, 12:46 pm by Fredx Reason: 1
Ok, somehow got it work. Now have to think how to add leds.

Quote

const int pin = 0; // analog pin
int digit1 = 0;
int digit2 = 0;
int tempc = 0;

void setup()
{
 Serial.begin(9600); // start serial communication
}

void loop()
{
 for(int i = 0; i < 8; i++) {
   tempc += analogRead(pin);
 }

tempc = (5.0 * tempc * 100.0) / (8.0 * 1024.0);

digit1 = tempc / 10; // get the ten's digit
digit2 = tempc % 10; // get the one's digit

Serial.println(digit1);
Serial.println(digit2);

 delay(1000); // delay before loop
}

D3C3PT1C0N

#29
May 22, 2010, 01:46 pm Last Edit: May 22, 2010, 01:47 pm by Fredx Reason: 1
How do I make leds blink the number (of times) of digits?

Quote

digit1 = tempc / 10; // get the ten's digit
digit2 = tempc % 10; // get the one's digit

if(digit1 = 0){
delay(500);
}

if (digit1 = 1){
 digitalWrite(LEDpin1, HIGH);
  delay(500);
digitalWrite(LEDpin1, LOW);
delay(500);
}

.... etc


THIS ISN'T CORRECT

Go Up