Go Down

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

AWOL

#30
May 22, 2010, 01:50 pm Last Edit: May 22, 2010, 01:50 pm by AWOL Reason: 1
Quote
if (digit1 [glow]=[/glow] 1){
.... etc

THIS ISN'T CORRECT


D a m n   right  ;)

You need a "for" 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

#31
May 22, 2010, 02:25 pm Last Edit: May 22, 2010, 04:57 pm by Fredx Reason: 1
Oh My God, I (totally noob) Got It!

Full working code:
Quote

const int pin = 0; // analog pin
int digit1 = 0;
int digit2 = 0;
int tempc = 0;
int LEDpin1 =  12;
int LEDpin2 =  13;
int LEDpin3 =  11;
int var;

void setup()
{
 Serial.begin(9600); // start serial communication
   pinMode(LEDpin1, OUTPUT);
     pinMode(LEDpin2, OUTPUT);
       pinMode(LEDpin3, OUTPUT);
}

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

var = 0;
while(var < digit1){
   digitalWrite(LEDpin1, HIGH);
  delay(500);
 digitalWrite(LEDpin1, LOW);
 delay(500);
 var++;
}


   digitalWrite(LEDpin2, HIGH);
  delay(500);
 digitalWrite(LEDpin2, LOW);
 delay(500);
 
 var = 0;
while(var < digit2){
   digitalWrite(LEDpin3, HIGH);
  delay(500);
 digitalWrite(LEDpin3, LOW);
 delay(500);
 var++;
}


Serial.println(tempc,DEC);

delay(1400); // delay before loop
}


I think that if the temperature is 22.9 degrees, it still gives me 22 degrees.

Maybe someone knows how to round 22.9 to 23 degrees?

I mean to round this value:
tempc = (5.0 * tempc * 100.0) / (8.0 * 1024.0);

AWOL

#32
May 22, 2010, 03:58 pm Last Edit: May 22, 2010, 04:02 pm by AWOL Reason: 1
Quote
Maybe someone knows how to round 22.9 to 23 degrees?

Same as always - add 0.5, and assign the result to an "int".
"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

#33
May 22, 2010, 04:53 pm Last Edit: May 22, 2010, 05:12 pm by Fredx Reason: 1
Code: [Select]
tempc = (5.0 * tempc * 100.0) / (8.0 * 1024.0);

int tempb = tempc + 0.5;


Does it do this:

22.1 + 0.5 = 22.6 -> ROUNDED to 22
22.4 + 0.5 = 22.9 -> ROUNDED to 22
22.6 + 0.5 = 23.1 -> ROUNDED to 23

Isn't "tempc" already been rounded, because it's INT ? (plz look Reply #31)

PaulS

#34
May 22, 2010, 06:05 pm Last Edit: May 22, 2010, 06:05 pm by PaulS Reason: 1
Quote
Does it do this:

22.1 + 0.5 = 22.6 -> ROUNDED to 22
22.4 + 0.5 = 22.9 -> ROUNDED to 22
22.6 + 0.5 = 23.1 -> ROUNDED to 23


Yes. Except that the number is truncated, not rounded.

Quote
Isn't "tempc" already been rounded, because it's INT ?


No. When a floating point number is stored in an int, the decimal portion is truncated.

D3C3PT1C0N

#35
May 22, 2010, 07:59 pm Last Edit: May 22, 2010, 08:06 pm by Fredx Reason: 1
Thank you.
Is it ok when i use temperature sensor WITHOUT a resistor?
In datasheet it is written:
1) Operates from 4 to 30 Volts
2) Less than 60 µA current drain
3) Low impedance output, 0.1 ohm for 1mA load
4) Supply Voltage +35V to -0.2V
5) Output Voltage +6V to -1.0V
6) Output Current 10 mA


D3C3PT1C0N

#36
May 22, 2010, 08:16 pm Last Edit: May 22, 2010, 10:06 pm by Fredx Reason: 1
@ Ladyada.net -> It seems that I can use it without resistor.


I noticed that Duemilanove-s power consumption is quite noticeable -
I connected new 9V battery to my project and after about 3 hours continuous use the voltage level has fallen from 9.5V to 7.7V
So approximately 3-4 hours more and the minumum Operating Voltage is exceeded. Should conside to grab some rechargeable batteries.

D3C3PT1C0N

#37
May 24, 2010, 11:29 am Last Edit: May 24, 2010, 11:57 am by Fredx Reason: 1
Just for curiosity - is Arduino capable of blinking 2 leds at the same time?
For example, when the temperature is 23 degrees, then both leds start to blink at the same time, one just does 1 blink extra? I know that the commands executed by arduino can't be run in parallel.. but maybe with a little shift.

Maybe it is possible to add extra variable
Quote

var = 0;
while(var < digit1, var < digit2){
<- I know this is wrong but maybe some other statement will do?
digitalWrite(LEDpin1, HIGH);
and
digitalWrite(LEDpin2, HIGH);

delay(500);
digitalWrite(LEDpin1, LOW);
and
digitalWrite(LEDpin2, LOW);

delay(500);
var++;
}



PaulS

Quote
while(var < digit1, var < digit2){ <- I know this is wrong but maybe some other statement will do?

Code: [Select]
while(var < digit1 && var < digit2)
{
 digitalWrite(LEDpin1, HIGH);
 digitalWrite(LEDpin2, HIGH);
 delay(500);

 digitalWrite(LEDpin1, LOW);
 digitalWrite(LEDpin2, LOW);
 delay(500);
 var++;
}


Both lights will blink at the same time (or close enough that you can't tell the difference), the same number of times.

D3C3PT1C0N

#39
May 24, 2010, 12:09 pm Last Edit: May 24, 2010, 12:24 pm by Fredx Reason: 1
Thanks for fast reply :)

But what happens, if we have a situation like this? Do they both blink 2 times or 3 times?
Quote

digit1 = 2;
digit2 = 3;


while(var < digit1 && var < digit2)
{
 digitalWrite(LEDpin1, HIGH);
 digitalWrite(LEDpin2, HIGH);
 delay(500);

 digitalWrite(LEDpin1, LOW);
 digitalWrite(LEDpin2, LOW);
 delay(500);
 var++;
}



Is it possible to make these leds blink different number of times?
It sounds quite complicated..


it is 23 degrees:
Led1__Led2
BLINK BLINK <- at the same time
BLINK BLINK
______BLINK

PaulS

With that while loop, they will blink twice. Try this:

Code: [Select]
int val1 = 3;
int val2 = 8;
int val = 0;
while(val < val1 || val < val2)
{
  if(val < val1)
    digitalWrite(ledPin1, HIGH);
  if(val < val2)
    digitalWrite(ledPin2, HIGH);

  delay(500);

  digitalWrite(ledPin1, LOW);
  digitalWrite(ledPin2, LOW);

  val++;
}

D3C3PT1C0N

#41
May 24, 2010, 02:30 pm Last Edit: May 24, 2010, 02:41 pm by Fredx Reason: 1
almost, except (for 23 degrees) they dont blink, but led2 stays a little longer ON than led1.

Let it be.. im working with another project.

Thank you for your help!

--------------------

The second project i'm planning, is to add two input buttons to arduino, so that when i press button no. 1 - then arduino starts to measure the temperature (for 3 times) and when i press button no. 2 - arduino starts to measure something else (maybe humidity... but i haven't got the sensor yet).

PaulS

Put the missing delay after turning the LEDs off back in, and it should work.

D3C3PT1C0N

leds light up with a little shift, but it's ok.
Thanks again!

Go Up