Hello everyone just started using Arduino. I have programmed in C before but its been a while. I seem to be stumped by why this code produces 0 when I have the variable last in line 5
Serial.print("Your HD is ");
Serial.print(drive_gb);
Serial.println(" GB large.");
drive_kb = drive_gb;
-> drive_kb = 1024 * 1024 * drive_kb;
Serial.print("Kilobytes ");
Serial.print(drive_kb);
but works perfectly when I have the variable first in the equation in line 5
Serial.print("Your HD is ");
Serial.print(drive_gb);
Serial.println(" GB large.");
drive_kb = drive_gb;
-> drive_kb = drive_kb * 1024 * 1024;
Serial.print("Kilobytes ");
Serial.print(drive_kb);
I feel like its something that I forgot in C maybe lol but its driving me crazy and I cant move on till I figure out why! Maybe this is why I went Computer engineering instead of continuing computer science haha.
Thank you to whomever solves my insanity inssues.
Arduino Lesson 4 Code
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- adafruit_support_bill
- Posts: 88086
- Joined: Sat Feb 07, 2009 10:11 am
Re: Arduino Lesson 4 Code
What is the data type of drive_gb and drive_kb?
What is the value of drive_gb?
What is the value of drive_gb?
-
- Posts: 4
- Joined: Thu Jan 05, 2012 10:34 am
Re: Arduino Lesson 4 Code
sorry forgot to include that part:
int drive_gb = 5;
long drive_kb;
in the tutorial this seems fine.
Also to add the only thing different in the two pieces of code is the location of drive_kb in the equation which makes no sense to me as why it would completely change the answer.
int drive_gb = 5;
long drive_kb;
in the tutorial this seems fine.
Also to add the only thing different in the two pieces of code is the location of drive_kb in the equation which makes no sense to me as why it would completely change the answer.
- adafruit_support_bill
- Posts: 88086
- Joined: Sat Feb 07, 2009 10:11 am
Re: Arduino Lesson 4 Code
Just guessing here, but the compiler may be determining the type of the expression result by the type of the first operand. With 16 bit integer arguments, you will get an overflow. If you cast those 1024's to longs, you might get a better result.
-
- Posts: 4
- Joined: Thu Jan 05, 2012 10:34 am
Re: Arduino Lesson 4 Code
Well see thats the weird thing if you look into the lesson 4 on here it shows a problem where that happens.
/*
* Drive size calculator!
*/
int drive_gb = 100;
long drive_mb; // we changed the type from "int" to "long"
void setup() // run once, when the sketch starts
{
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.print("Your HD is ");
Serial.print(drive_gb);
Serial.println(" GB large.");
drive_mb = 1024 * drive_gb;
Serial.print("It can store ");
Serial.print(drive_mb);
Serial.println(" Megabytes!");
}
void loop() // we need this to be here even though its empty
{
}
here it shows the problem that drive_gb starts as an int and then in the equation drive_gb makes a temp variable to store the answer of 1024 * drive_gb in an int also which causes the error in the answer. It then goes on to show the solution to the problem here:
/*
* Drive size calculator!
*/
int drive_gb = 100;
long drive_mb;
void setup() // run once, when the sketch starts
{
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.print("Your HD is ");
Serial.print(drive_gb);
Serial.println(" GB large.");
drive_mb = drive_gb;
drive_mb = drive_mb * 1024;
Serial.print("It can store ");
Serial.print(drive_mb);
Serial.println(" Megabytes!");
}
void loop() // we need this to be here even though its empty
{
where you can see they moved drive_gb into drive_mb to convert it to a long then use the equation. The only thing I can think of is that the temp variable that the answer is saved to before stored in the final variable on the left side of the equation is whatever type the operand is on the right side of the equation.
long x;
long_a = 10000;
So X = 1024 * long_a
it would be a problem because the temp variable made for the right side equation would become an int because 1024 is an int.
but if i rearrange and do
X = long_a * 1024
then it would work because the temp variable is then casted to long.
let me know if this holds any water its driving me crazy!
/*
* Drive size calculator!
*/
int drive_gb = 100;
long drive_mb; // we changed the type from "int" to "long"
void setup() // run once, when the sketch starts
{
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.print("Your HD is ");
Serial.print(drive_gb);
Serial.println(" GB large.");
drive_mb = 1024 * drive_gb;
Serial.print("It can store ");
Serial.print(drive_mb);
Serial.println(" Megabytes!");
}
void loop() // we need this to be here even though its empty
{
}
here it shows the problem that drive_gb starts as an int and then in the equation drive_gb makes a temp variable to store the answer of 1024 * drive_gb in an int also which causes the error in the answer. It then goes on to show the solution to the problem here:
/*
* Drive size calculator!
*/
int drive_gb = 100;
long drive_mb;
void setup() // run once, when the sketch starts
{
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.print("Your HD is ");
Serial.print(drive_gb);
Serial.println(" GB large.");
drive_mb = drive_gb;
drive_mb = drive_mb * 1024;
Serial.print("It can store ");
Serial.print(drive_mb);
Serial.println(" Megabytes!");
}
void loop() // we need this to be here even though its empty
{
where you can see they moved drive_gb into drive_mb to convert it to a long then use the equation. The only thing I can think of is that the temp variable that the answer is saved to before stored in the final variable on the left side of the equation is whatever type the operand is on the right side of the equation.
long x;
long_a = 10000;
So X = 1024 * long_a
it would be a problem because the temp variable made for the right side equation would become an int because 1024 is an int.
but if i rearrange and do
X = long_a * 1024
then it would work because the temp variable is then casted to long.
let me know if this holds any water its driving me crazy!
- adafruit_support_bill
- Posts: 88086
- Joined: Sat Feb 07, 2009 10:11 am
Re: Arduino Lesson 4 Code
That is essentially what I said. The compiler is determining the type of the expression result by the type of the first operand.The only thing I can think of is that the temp variable that the answer is saved to before stored in the final variable on the left side of the equation is whatever type the operand is on the right side of the equation.
-
- Posts: 4
- Joined: Thu Jan 05, 2012 10:34 am
Re: Arduino Lesson 4 Code
Ah Ok I misunderstood that as being the very first operand on the left side of the = . Thank you very much!
Please be positive and constructive with your questions and comments.