Seattle, WA USA
Offline
Brattain Member
Karma: 313
Posts: 35502
Seattle, WA USA
|
 |
« Reply #15 on: May 21, 2010, 02:39:19 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19052
I don't think you connected the grounds, Dave.
|
 |
« Reply #16 on: May 21, 2010, 03:02:47 pm » |
I loathe unecessary globals: 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 }
|
|
|
|
« Last Edit: May 21, 2010, 03:15:57 pm by AWOL »
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Europe, Estonia
Offline
Full Member
Karma: 0
Posts: 208
|
 |
« Reply #17 on: May 21, 2010, 03:11:57 pm » |
Using PaulS suggestions ended up like this 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 23.4375000000 Celsius, 23 Max, 23 Min 23.4375000000 Celsius, 23 Max, 23 Min
...
|
|
|
|
« Last Edit: May 21, 2010, 03:13:12 pm by Fredx »
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19052
I don't think you connected the grounds, Dave.
|
 |
« Reply #18 on: May 21, 2010, 03:16:32 pm » |
int tempb = 0; Why? Serial.print(tempb,DEC); Isn't ten places of decimals excessive?
|
|
|
|
« Last Edit: May 21, 2010, 03:17:36 pm by AWOL »
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Europe, Estonia
Offline
Full Member
Karma: 0
Posts: 208
|
 |
« Reply #19 on: May 21, 2010, 03:16:53 pm » |
AWOL,using your code, serial monitor show this: 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
|
|
|
|
« Last Edit: May 21, 2010, 03:20:55 pm by Fredx »
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19052
I don't think you connected the grounds, Dave.
|
 |
« Reply #20 on: May 21, 2010, 03:19:05 pm » |
Have another look at reply 16.
|
|
|
|
« Last Edit: May 21, 2010, 03:19:20 pm by AWOL »
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Europe, Estonia
Offline
Full Member
Karma: 0
Posts: 208
|
 |
« Reply #21 on: May 21, 2010, 03:25:01 pm » |
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 
|
|
|
|
« Last Edit: May 21, 2010, 03:28:25 pm by Fredx »
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19052
I don't think you connected the grounds, Dave.
|
 |
« Reply #22 on: May 21, 2010, 03:31:16 pm » |
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.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Europe, Estonia
Offline
Full Member
Karma: 0
Posts: 208
|
 |
« Reply #23 on: May 21, 2010, 03:48:51 pm » |
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 - 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? 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...
|
|
|
|
« Last Edit: May 21, 2010, 04:08:08 pm by Fredx »
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19052
I don't think you connected the grounds, Dave.
|
 |
« Reply #24 on: May 21, 2010, 04:04:10 pm » |
Think about how you'd break down "25" into "2" and "5". The rest is simple. Ten to fifteen lines of code, maximum.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Europe, Estonia
Offline
Full Member
Karma: 0
Posts: 208
|
 |
« Reply #25 on: May 21, 2010, 05:01:52 pm » |
Found something that may be useful 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 #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
|
|
|
|
« Last Edit: May 21, 2010, 05:08:35 pm by Fredx »
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 313
Posts: 35502
Seattle, WA USA
|
 |
« Reply #26 on: May 21, 2010, 05:09:10 pm » |
though this is C++ Good thing that's what the Arduino is programmed in.
|
|
|
|
|
Logged
|
|
|
|
|
Europe, Estonia
Offline
Full Member
Karma: 0
Posts: 208
|
 |
« Reply #27 on: May 21, 2010, 05:13:17 pm » |
That was bad..  I suppose, this should do the trick 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 {
|
|
|
|
« Last Edit: May 22, 2010, 04:25:24 am by Fredx »
|
Logged
|
|
|
|
|
Europe, Estonia
Offline
Full Member
Karma: 0
Posts: 208
|
 |
« Reply #28 on: May 22, 2010, 03:29:22 am » |
Ok, somehow got it work. Now have to think how to add leds. 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 }
|
|
|
|
« Last Edit: May 22, 2010, 05:46:08 am by Fredx »
|
Logged
|
|
|
|
|
Europe, Estonia
Offline
Full Member
Karma: 0
Posts: 208
|
 |
« Reply #29 on: May 22, 2010, 06:46:03 am » |
How do I make leds blink the number (of times) of digits? 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
|
|
|
|
« Last Edit: May 22, 2010, 06:47:47 am by Fredx »
|
Logged
|
|
|
|
|
|