i've posted lesson 4, feedback and suggestions welcome!
http://www.ladyada.net/learn/arduino/lesson4.html
Lesson 4
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- cstcyr
- Posts: 7
- Joined: Thu Oct 11, 2007 11:43 pm
Great job on Lesson 4!
I got my Arduino a few days ago and have been having a great time getting back into electronics and programming.
One thing I was interested in seeing you cover in the Serial portion of the lesson was having the Arduino read from the serial port. I'm working on a project to control Luxeon LED's for lighting. I'm looking at having the Arduino listen on the serial port for commands to turn on and off, blink, RGB color mix, and possibly run patterns.
But the lessons have been excellent so far. Keep up the great work and promoting open source hardware.
Christian
I got my Arduino a few days ago and have been having a great time getting back into electronics and programming.
One thing I was interested in seeing you cover in the Serial portion of the lesson was having the Arduino read from the serial port. I'm working on a project to control Luxeon LED's for lighting. I'm looking at having the Arduino listen on the serial port for commands to turn on and off, blink, RGB color mix, and possibly run patterns.
But the lessons have been excellent so far. Keep up the great work and promoting open source hardware.
Christian
-
- Posts: 18
- Joined: Fri Jun 08, 2007 2:14 pm
Lesson 5 and Beyond
Overjoyed at your tutorials: Arduino, micro course, Atmex, etc.
"it turns out that reading from the serial port is MUCH harder than writing stuff out.
so i decided to save reading for another lesson. "
I so hope you can keep the lessons coming. This is unbelievably fortuitous.
We bought four items from you, but not quite the starter pack, and am wishing we could buy the LED package as well. Haven't found it among your offerings yet. Should I keep looking?
Thanks for what you do, LadyAda, you're a National Treasure.
WingSpread
"it turns out that reading from the serial port is MUCH harder than writing stuff out.
so i decided to save reading for another lesson. "
I so hope you can keep the lessons coming. This is unbelievably fortuitous.
We bought four items from you, but not quite the starter pack, and am wishing we could buy the LED package as well. Haven't found it among your offerings yet. Should I keep looking?
Thanks for what you do, LadyAda, you're a National Treasure.
WingSpread
- -nkt-
- Posts: 19
- Joined: Sun Feb 08, 2009 4:34 pm
Re: Lesson 4
There's a heck of a "Gotcha!" in this one.
I did the drive calculator bit. I decided to use However, there were two things I noticed. Firstly, nowhere does it explicitly say that you can declare the types as unsigned like that (I recalled it vaguely from my degree course some ten years ago - to say I'm rusty is an understatement!) and secondly, and I'm sure some of you are ahead of me on this... you run out of digits on the unsigned long as well!
As it happens, 100*1024*1024*1024 gives you a result of 0! This rather made me scratch my head, and fiddle about a bit. After some testing, I realised I was working with too big a number. 1024*1024*1024-1 (2^32-1) is as big as the long type can handle. Anything times this will give 0. And this is the conversion to bytes from Gbytes.
It might be worth noting that you can't do the number of bytes calculation like the Mb and kb calcs, because you run out of digits no matter what. Yes, it's the point of the tutorial, but a note would be nice.
I did the drive calculator bit. I decided to use
Code: Select all
unsigned long Gbytes = 100; // Gig drive size
unsigned long bytes = 0; // drive size in bytes
unsigned long Kbytes = 0; // drive size in Kb
As it happens, 100*1024*1024*1024 gives you a result of 0! This rather made me scratch my head, and fiddle about a bit. After some testing, I realised I was working with too big a number. 1024*1024*1024-1 (2^32-1) is as big as the long type can handle. Anything times this will give 0. And this is the conversion to bytes from Gbytes.
It might be worth noting that you can't do the number of bytes calculation like the Mb and kb calcs, because you run out of digits no matter what. Yes, it's the point of the tutorial, but a note would be nice.
-
- Posts: 1
- Joined: Mon Mar 02, 2009 2:53 pm
Programming weirdness Re: Lesson 4
First off, thanks ladyada for the the wonderful tutorials!
Hmm, I didn't notice it asking for drive size in bytes in "wrapping up, the final project," so I never ran into NKT-'s problem (perhaps the tutorial page has been updated recently).
I ran into different issues with the final project though. I looked through the other threads but didn't immediately see this so here goes.
Even when I copy-pasted the tutorial's whited out solution, I would get zero for all the real_drive values.
After a few hours playing around with it, and then after getting some help since I am rather new to programming, it seems that for some reason constants are treated differently from int type variables? I'm running the starter pack with "arduino-0013-linux2.tgz Arduino 0013 for Linux (rev 2)" which says in the titlebar "0013 Alpha," using ubuntu intrepid.
For instance (I simplified the drive calc program down)
outputs
but
correctly outputs
Also, when I made a small loop because I got tired of testing values manually, it worked fine:
outputs everything correctly:
In addition, if I change the variable types to floats, everything works:
outputs
So I guess it's something to do with the Arduino memory or something (when I modified the give blanket out program to C and ran it, it worked fine), except I haven't googled up anyone else having the same problem and I was copy-pasting the tutorial after all which must have worked for other people.
I do find the output interesting though, and initially I thought there was something special about powers of 2 (why the drive_kb and drive_mb calculations worked, but the real_drive_kb and real_drive_mb calculations didn't):
Has anyone else encountered this? Why does this happen? I'm curious, as all this memory and stuff is new to me, but I'm not sure what search terms to google =)
Hmm, I didn't notice it asking for drive size in bytes in "wrapping up, the final project," so I never ran into NKT-'s problem (perhaps the tutorial page has been updated recently).
I ran into different issues with the final project though. I looked through the other threads but didn't immediately see this so here goes.
Even when I copy-pasted the tutorial's whited out solution, I would get zero for all the real_drive values.
After a few hours playing around with it, and then after getting some help since I am rather new to programming, it seems that for some reason constants are treated differently from int type variables? I'm running the starter pack with "arduino-0013-linux2.tgz Arduino 0013 for Linux (rev 2)" which says in the titlebar "0013 Alpha," using ubuntu intrepid.
For instance (I simplified the drive calc program down)
Code: Select all
int a = 100;
long b;
long c;
void setup() // run once, when the sketch starts
{
Serial.begin(9600); // set up Serial library at 9600 bps
b = a;
Serial.print("b = a = ");
Serial.println(b);
c = b * 10;
Serial.print(100 * 10 = ");
Serial.println(c);
void loop() {}
Code: Select all
b = a = 100
100 * 10 = 0
Code: Select all
c = b * 10.;
Code: Select all
1000.00
Code: Select all
int a = 100;
long b;
long c;
int x;
void setup() // run once, when the sketch starts
{
Serial.begin(9600); // set up Serial library at 9600 bps
b = a;
Serial.print("b = a = ");
Serial.println(b);
x = 0;
while(x < 1030)
{
c = b * x;
Serial.print("100 * ");
Serial.print(x);
Serial.print("= ");
Serial.println(c);
x++;
}
}
void loop() {}
Code: Select all
b = a = 100
100 * 0= 0
100 * 1= 100
[ blah blah blah ]
100 * 1028= 102800
100 * 1029= 102900
Code: Select all
int a = 100;
float b;
float c;
Code: Select all
b = a = 100.00
[...]
100*9 = 900.00
100*10= 1000.00
[etc.]
I do find the output interesting though, and initially I thought there was something special about powers of 2 (why the drive_kb and drive_mb calculations worked, but the real_drive_kb and real_drive_mb calculations didn't):
Code: Select all
b = a = 100
100 * 1 = 100
100 * 2 = 200
[ everything up to 9 works fine, then it gets weird ]
100*9 = 900
100*10= 0
100*11= 1000
[ and so on except around powers of 2, for instance: ]
100*12= 0
100*13 = 1000
100*14 = 0
100*15 = 1500
100*16= 1600
100*17= 1700
100*18= 0
100*19= 1000
[...][and again powers of 2]
100*27= 1000
100*28 = 0
100*29 = 1000
100*30 = 0
100*31 = 3100
100*32 = 3200
100*33 = 3300
100*34 = 0
100*35 = 1000
100*36 = 0
[Thus for the original code, *1024 for drive_ sizes and *1000 real_drive sizes, the drive_ sizes would work]
100*1000= 0
100*1001= 1000
[...]
100*1023= 0
100*1024= 102400
100*1025= 0
-
- Posts: 3
- Joined: Sun Apr 26, 2009 8:54 pm
Re: Lesson 4. byte defaults to unsigned?
+1
"Introduction to types, part 2 ...By default, variables are signed"
http://www.ladyada.net/learn/arduino/lesson4.html
Apparently in the Arduino environment byte defaults to unsigned.
"A byte stores an 8-bit unsigned number, from 0 to 255."
http://arduino.cc/en/Reference/Byte
First I tried initializing a byte variable to -127. It evaluated to +129!
Also I learned that the byte variable didn't print correctly until I cast it to an integer type.
The serial monitor showed the output:
Then I initialized a byte variable to 1 and decremented twice.
It went to 0 and rolled over (overflowed? underflowed?) to 255:
Serial monitor output:
Thanks!
I experimented with different types and overflow as suggested.First off, thanks ladyada for the the wonderful tutorials!
"Introduction to types, part 2 ...By default, variables are signed"
http://www.ladyada.net/learn/arduino/lesson4.html
Apparently in the Arduino environment byte defaults to unsigned.
"A byte stores an 8-bit unsigned number, from 0 to 255."
http://arduino.cc/en/Reference/Byte
First I tried initializing a byte variable to -127. It evaluated to +129!
Also I learned that the byte variable didn't print correctly until I cast it to an integer type.
Code: Select all
byte negativebyte = -127;
void setup() // run once, when the sketch starts
{
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.print("Without casting, negativebyte = ");
Serial.println(negativebyte);
Serial.print("Casting byte to char, negativebyte = ");
Serial.println((char) negativebyte);
Serial.print("Casting byte to int, negativebyte = ");
Serial.println((int) negativebyte);
}
void loop() // we need this to be here even though its empty
{
}
Code: Select all
Without casting, negativebyte = ?
Casting byte to char, negativebyte = ?
Casting byte to int, negativebyte = 129
It went to 0 and rolled over (overflowed? underflowed?) to 255:
Code: Select all
byte testbyte = 1;
void setup() // run once, when the sketch starts
{
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.print("Without casting byte to int, testbyte = ");
Serial.println(testbyte);
Serial.print("Casting byte to int, testbyte = ");
Serial.println((int) testbyte);
testbyte = testbyte - 1;
Serial.print("After subtracting 1, testbyte = ");
Serial.println((int) testbyte);
testbyte--;
Serial.print("After subtracting 1, testbyte = ");
Serial.println((int) testbyte);
}
void loop() // we need this to be here even though its empty
{
}
Code: Select all
Without casting byte to int, testbyte =
Casting byte to int, testbyte = 1
After subtracting 1, testbyte = 0
After subtracting 1, testbyte = 255
Please be positive and constructive with your questions and comments.