Pages: [1]   Go Down
Author Topic: Integers use much less space then Floats?!  (Read 1143 times)
0 Members and 1 Guest are viewing this topic.
Westbrook, CT
Offline Offline
Full Member
***
Karma: 2
Posts: 139
"Why should I bother with made-up games when there are so many real ones going on." (c) Kurt Vonnegut
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Guys!

Recently i've been doing a project using my Arduino Uno R3, which is very simple. It is basically just a photoresistor that is used as a motion detector and a 8-ohm speaker from Radioshack. I want to make a burglar alarm that turns on when it spots significant changes in light.  smiley-cool

So my code is very short, just above 30 lines of code. You don't really have to study it. But maybe by looking at it would help you answer my question.
Code:
int sensor = 14; // sensor pin
int sound = 9;   // speaker pin

void setup()
{
  Serial.begin(9600); // Serial for Debuging
}

void loop()
{
  Serial.println(analogRead(sensor)); // Print out to Serial
  int comp = (analogRead(sensor));    // Comp integer which is used later on in the "for" statement to find by how much the light intensity has chaged.
  Serial.print("Sensor Read = ");     // Debug it
  Serial.println(comp);
 
  for (int i=0; i<50; i++) {          // Comparing 50 times
    int eye = (analogRead(sensor));
    int dif = comp - eye;             // if is diffirence between the comp light light measure and the current measure
     
    if (dif < -10 || dif > 10) {   // If there is an aceptable diffirence in this case 10 then do a sound
       tone(sound, 6000);
       delay(3000);
       noTone(sound);
       break;
       Serial.println("------------------------------------");
       Serial.print("Diffirence = ");
       Serial.println(dif);
       Serial.println("------------------------------------");
     }
     delay(100);                      // do this comparing 10 times per second
  }
}

But here is a Mystery (or a fact?)  smiley-roll-sweat When I change the
Code:
int eye = (analogRead(sensor));
int dif = comp - eye;           

To

Code:
float eye = (analogRead(sensor));
int dif = comp - eye;             

The Sketch size increase from 4,502 bytes to 5,178 bytes, that somewhere 600 bytes. Half a Kilobyte!!  smiley-eek

Now i know that longs range from something like -2,123,424,234, to 2,123,453,345 but why do they use so much space?
Would've the programmers from the 60's killed their self if they found out that just one number took the whole computer disk space at that time?!  smiley-eek  smiley-surprise
Logged

Arduino Uno R3
Mac OSX Lion


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

The processor has no floating point hardware, so fp operations have to be emulated in software.
This takes up program memory.
A float is four bytes, an int is two on the Arduino.
Variables reside in RAM, not program memory.
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.

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

But here is a Mystery (or a fact?)  smiley-roll-sweat When I change the

Code:
int eye = (analogRead(sensor));
int dif = comp - eye;

To

Code:
float eye = (analogRead(sensor));
int dif = comp - eye;

Why would you do that? The analogRead() function returns an int. Storing the two byte int in a 4 byte float wastes space and adds no value.
Logged

Westbrook, CT
Offline Offline
Full Member
***
Karma: 2
Posts: 139
"Why should I bother with made-up games when there are so many real ones going on." (c) Kurt Vonnegut
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Why would you do that? The analogRead() function returns an int. Storing the two byte int in a 4 byte float wastes space and adds no value.

True, But i just did it as to show an example. Maybe i'll use a light sensor or other hardware later on requiring longs.
Logged

Arduino Uno R3
Mac OSX Lion


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

Quote
Maybe i'll use a light sensor or other hardware later on requiring longs.
But analogRead will only ever return a two byte result.
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.

Westbrook, CT
Offline Offline
Full Member
***
Karma: 2
Posts: 139
"Why should I bother with made-up games when there are so many real ones going on." (c) Kurt Vonnegut
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


The processor has no floating point hardware, so fp operations have to be emulated in software.
This takes up program memory.
A float is four bytes, an int is two on the Arduino.
Variables reside in RAM, not program memory.

Thanks, but how does it only resides in RAM if 600bytes of program memory are being used up?
Logged

Arduino Uno R3
Mac OSX Lion


Westbrook, CT
Offline Offline
Full Member
***
Karma: 2
Posts: 139
"Why should I bother with made-up games when there are so many real ones going on." (c) Kurt Vonnegut
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

But analogRead will only ever return a two byte result.

Oh... Thats true. My bad
Logged

Arduino Uno R3
Mac OSX Lion


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

Quote
Thanks, but how does it only resides in RAM if 600bytes of program memory are being used up?
The AVR processor core has NO native support for floating point.
If you want to add two floating point numbers, you have to write a sequence of assembler instruction (which reside in program memory) to fetch, operate on and store the two lots of four bytes of RAM which represent the two floating point numbers.
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.

0
Offline Offline
Shannon Member
****
Karma: 162
Posts: 10496
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The Arduino software is clever enough to only compile into the binary sketch the code that is actually used - so if you don't use floats you don't get any of the float code incorporated.  Ditto all the other stuff and libraries.
Logged

[ I won't respond to messages, use the forum please ]

Valencia, Spain
Offline Offline
Faraday Member
**
Karma: 119
Posts: 4591
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Now i know that longs range from something like -2,123,424,234, to 2,123,453,345 but why do they use so much space?

What increased most was your program size because the compiler pulled in extra code for the floating point operations (which are done in software).
Logged

No, I don't answer questions sent in private messages...

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


The processor has no floating point hardware, so fp operations have to be emulated in software.
This takes up program memory.
A float is four bytes, an int is two on the Arduino.
Variables reside in RAM, not program memory.

Thanks, but how does it only resides in RAM if 600bytes of program memory are being used up?

Program memory is physically different from RAM.

http://www.arduino.cc/playground/Learning/Memory
Logged

Pages: [1]   Go Up
Jump to: