Pages: 1 2 [3]   Go Down
Author Topic: Temperature sensor + Leds  (Read 4937 times)
0 Members and 1 Guest are viewing this topic.
Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25798
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
if (digit1 [glow]=[/glow] 1){
.... etc

THIS ISN'T CORRECT

D a m n   right  smiley-wink

You need a "for" loop.
« Last Edit: May 22, 2010, 06:50:55 am by AWOL » 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.

Europe, Estonia
Offline Offline
Full Member
***
Karma: 0
Posts: 208
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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);
« Last Edit: May 22, 2010, 09:57:29 am by Fredx » Logged

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

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".
« Last Edit: May 22, 2010, 09:02:26 am by AWOL » 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.

Europe, Estonia
Offline Offline
Full Member
***
Karma: 0
Posts: 208
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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)
« Last Edit: May 22, 2010, 10:12:56 am by Fredx » Logged

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

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.
« Last Edit: May 22, 2010, 11:05:57 am by PaulS » Logged

Europe, Estonia
Offline Offline
Full Member
***
Karma: 0
Posts: 208
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

« Last Edit: May 22, 2010, 01:06:57 pm by Fredx » Logged

Europe, Estonia
Offline Offline
Full Member
***
Karma: 0
Posts: 208
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@ 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.
« Last Edit: May 22, 2010, 03:06:08 pm by Fredx » Logged

Europe, Estonia
Offline Offline
Full Member
***
Karma: 0
Posts: 208
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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++;
}


« Last Edit: May 24, 2010, 04:57:14 am by Fredx » Logged

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

Quote
while(var < digit1, var < digit2){ <- I know this is wrong but maybe some other statement will do?
Code:
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.
Logged

Europe, Estonia
Offline Offline
Full Member
***
Karma: 0
Posts: 208
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for fast reply smiley

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
« Last Edit: May 24, 2010, 05:24:07 am by Fredx » Logged

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

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

Code:
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++;
}
Logged

Europe, Estonia
Offline Offline
Full Member
***
Karma: 0
Posts: 208
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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).
« Last Edit: May 24, 2010, 07:41:58 am by Fredx » Logged

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

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

Europe, Estonia
Offline Offline
Full Member
***
Karma: 0
Posts: 208
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: 1 2 [3]   Go Up
Jump to: