|
Post by mwclarkson on Feb 7, 2014 15:15:07 GMT -8
OK, I'm still pretty new with Arduino/C although I've done a fair bit of simple VB/C++/Python/Java.
I'm still playing with circles from the tutorial in the book and I'm trying to get a crawling line of pixels marching across the screen.
I can get them to move off the right edge before jumping back to the left but they won't reappear on the left until the centre of the circle has reached x:0 (so the entire right side of the circle pops in at once).
If I make the circles march the other way they appear neatly on the right but pop out poorly on the left (again, once the centre his x:0).
Is this a hardware limitation or have I done something obviously dumb with my programming?
#include <EEPROM.h> #include <SPI.h> #include <GD2.h>
int num; // Number of dots int r; // Radius of dots int spacing; // Space allowed for each dot int dot[10]; // Position of each dot
void setup() { num = 12; r = 15; spacing = 480/(num-2); // This should give a zone of 48 px per cirlce // for 10 circles in a row for (int i = 0;i<num;i++) { dot[i] = spacing*(i-1); // Should start .|..........|. // Where | is the edge of the screen } GD.begin(); }
void loop() { GD.ClearColorRGB(0x000000); GD.Clear(); GD.PointSize(16*r); GD.Begin(POINTS); for (int i = 0; i <num; i++) { GD.Vertex2ii(dot[i],100); GD.Vertex2ii(480-dot[i],170); dot[i]++; if(dot[i] > (480+spacing)) { dot[i] = - spacing; } }
GD.swap(); delay(5); }
|
|
|
Post by jamesbowman on Feb 7, 2014 16:25:00 GMT -8
Aha, this is a bit subtle .. the problem is that Vertex2ii can only handle positive coordinates. The book talks about this on p.55, and covers Vertex2f() - which can handle negative coordinates. excamera.com/files/gd2book_v0.pdfSo changing the code (I also corrected the size of the "dot[]" array): #include <EEPROM.h> #include <SPI.h> #include <GD2.h>
int num; // Number of dots int r; // Radius of dots int spacing; // Space allowed for each dot int dot[12]; // Position of each dot
void setup() { num = 12; r = 15; spacing = 480/(num-2); // This should give a zone of 48 px per cirlce // for 10 circles in a row for (int i = 0;i<num;i++) { dot[i] = spacing*(i-1); // Should start .|..........|. // Where | is the edge of the screen } GD.begin(); }
void loop() {
GD.ClearColorRGB(0x000000); GD.Clear();
GD.PointSize(16*r); GD.Begin(POINTS); for (int i = 0; i <num; i++) { GD.Vertex2f(16 * dot[i], 16 * 100); GD.Vertex2f(16 * (480-dot[i]), 16 * 170); dot[i]++; }
GD.swap(); delay(5); } Gives this:
|
|
|
Post by mwclarkson on Feb 8, 2014 2:55:53 GMT -8
Thank you very much!
So I had done something dumb with my programming (array declaration), but that wasn't my only problem. :-)
I had a skim read through the book before I started, but reading without doing I have inevitably missed an awful lot.
Have to say, I'm really impressed so far. Once I get a bit more comfortable trouble shooting I'm looking forward to throwing these at my students (11-18) to see what they come up with.
|
|