Home \ Help \ Search \ Login RSS

Welcome, Guest. Please Login

This forum is now READ-ONLY! Please visit http://forum.wiring.co/ for the new forum.

Wiring ForumProgramming Questions & HelpSyntax › for (i=0; i<7; i=i+1)

Pages: 1 2 
for (i=0; i<7; i=i+1) (Read 29353 times)
09/07/05 at 18:48:06

boppyer   Offline
YaBB Newbies
London

Posts: 39
*
 
Hi,
I'm having problems with the below loop. I'm trying to loop through some LED columns, they normally work fine but I'm trying to put loops in, rather than adding every line manually. Does any one know how to get varialbe (test) to work within a loop?

I can't seem to get it to read the test var.


// program loop
void loop()
{

int i;
int test;

max7219_all(0x00);
delay(500);

for (i=0; i<7; i=i+1) {
print("%i\n", i);
int test = 'max7219_col' + 1;
max7219_put(max7219_row7, test);
}

delay(1000);
}


many thanks in advance Smiley

boppyer
 
IP Logged
 
Reply #1 - 09/07/05 at 22:32:57

michael05   Offline
YaBB Newbies

Posts: 41
*
 
don't initialize the variable a second time..

wrong:
Code:
int test;
...
int test = 'max7219_col' + 1; 



right:
Code:
int test;
...
test = 'max7219_col' + 1; 

 
IP Logged
 
Reply #2 - 09/08/05 at 11:27:49

boppyer   Offline
YaBB Newbies
London

Posts: 39
*
 
weird it still doesn't seem to work:

// program loop
void loop()
{

int i;
int test;

//max7219_all(0x00);
//delay(500);

for (i=0; i<7; i=i+1) {
print("%i\n", i);
test = 'max7219_col' + i;
max7219_put(max7219_row7, test);
}

delay(1000);
}


it comes up with a unexpected char:'m'

many thanks

boppyer
 
IP Logged
 
Reply #3 - 09/08/05 at 12:21:25

zambetti   Offline
YaBB Newbies

Posts: 21
*
 
the problem seems to be here:

Code:
int test = 'max7219_col' + 1; 



if you wish to iterate through the column values, then you could set up an array with the column values and iterate by accessing the elements of the array via an iterator such as:

Code:
// declare array with 8 spots for values
int max7219_cols[8];

void setup(){
  // set array values
  max7219_cols[0] = 0x01;
  max7219_cols[1] = 0x02;
  max7219_cols[2] = 0x04;
  // and so on
}

// later, where iterator 'i' is between 0 and 7 inclusive
max7219_put(max7219_row7, max7219_cols[i]);
 

 
IP Logged
 
Reply #4 - 09/08/05 at 14:20:03

boppyer   Offline
YaBB Newbies
London

Posts: 39
*
 
thanks for that, it worked, but one more question Smiley

If I have this array:

boolean displayMatrix[] = {{0, 0, 0, 1, 1, 0, 0, 0},
{0, 0, 1, 1, 1, 1, 0, 0},
{0, 1, 1, 1, 1, 1, 1, 0},
{1, 1, 1, 1, 1, 1, 1, 1},
{0, 0, 1, 1, 1, 1, 0, 0},
{0, 0, 1, 1, 1, 1, 0, 0},
{0, 0, 1, 1, 1, 1, 0, 0},
{0, 0, 1, 1, 1, 1, 0, 0}};


which is a arrow, how would i animate it upwards going through the array, sorry if it's simple, but i always get stuck with loops Smiley

kind regards

boppyer Smiley
 
IP Logged
 
Reply #5 - 09/09/05 at 04:09:01

zambetti   Offline
YaBB Newbies

Posts: 21
*
 
Not sure if this is what you are looking for (and i haven't had a chance to test it), but it might do something interesting at least. Smiley Modular arithmetic is our friend when dealing with arrays and looped animation.

Code:
// declare array w/ 8 spots for led rows of 8bits (leds)
int arrow_cols[8];

void setup()
{
  // set array values to draw arrow
  arrow_cols[0] = max7219_col3 | max7219_col4;
  arrow_cols[1] = max7219_col2 | max7219_col3 | max7219_col4 | max7219_col5;
  // and so on
}

// draws arrow graphic on screen w/ vertical offset (0-7 inclusive)
void drawArrow(int startoffset)
{
  // initialize iterator
  int i = 0;
  // for each row of max7219
  while(i < 8){
    // max7219_put(row #, 8bits indicating state of 8 leds in that row);
    // rows range from 1 to 8 inclusive
    // iterate through arrrow graphic array with starting offset
    max7219_put(i + 1, arrow_cols[(startoffset + i) % 8]);
    i++;
  }
}

// variable to hold frame # of animation
int frame = 0;

void loop()
{
  // draw arrow to display with offset
  drawArrow(frame);
  // show for 100ms
  delay(100);
  // increment frame in animation
  frame++;
  // make sure to loop on 8th frame
  frame %= 8;
}
 

 
IP Logged
 
Reply #6 - 09/09/05 at 11:31:21

boppyer   Offline
YaBB Newbies
London

Posts: 39
*
 
that works a treat Smiley nice one for that, it's brilliant. Smiley Smiley

One last question though, would it be possible to change the array so it's like this one:

boolean displayMatrix[] = {
{0, 0, 0, 1, 1, 0, 0, 0},
{0, 0, 1, 1, 1, 1, 0, 0},
{0, 1, 1, 1, 1, 1, 1, 0},
{1, 1, 1, 1, 1, 1, 1, 1},
{0, 0, 1, 1, 1, 1, 0, 0},
{0, 0, 1, 1, 1, 1, 0, 0},
{0, 0, 1, 1, 1, 1, 0, 0},
{0, 0, 1, 1, 1, 1, 0, 0}};

It just makes it easier to then edit and make up characters, I've got to make the entire alphabet so it would be quicker doing it like above because it's a more visual approach. I will share my alphabet once I've completed it Smiley

kind regards

boppyer:-)
 
IP Logged
 
Reply #7 - 09/09/05 at 13:42:08

boppyer   Offline
YaBB Newbies
London

Posts: 39
*
 
also does anyone know what would be the best way to scroll through mutilple arrays? (scrolling text?) Smiley I'm liking my LED, i would recomend getting one Smiley

cheers

boppyer
 
IP Logged
 
Reply #8 - 09/10/05 at 14:34:30

mellis   Offline
YaBB Newbies

Posts: 43
*
 
Here's some code that lets you specify an 8x8 matrix of individual pixels, then combines all the pixels in a row into the one number that will get passed to max7219_put (I'm calling the array of those 8 numbers a sprite).

Displaying multiple sprites on the LED display at once will be a bit trickier.  Do you want them to scroll horizontally, vertically or both?  (Both is much more complicated, as potentially parts of 4 letters could be shown at once, and you need a two-dimensional array of text). 

I'm thinking that all of this should get added to the display library at some point, so that your code will become much simpler.  If you have a sense of other things you'd like to do with the display, let us know, as it will help plan the library.

[code]
boolean arrow_pixels[8][8] = {
       {0, 0, 0, 0, 0, 0, 0, 0},
       {1, 0, 0, 0, 0, 0, 0, 0},
       {0, 1, 1, 1, 1, 1, 1, 1},
       {1, 1, 1, 1, 1, 1, 1, 1},
       {1, 1, 0, 0, 0, 0, 0, 0},
       {0, 0, 1, 0, 1, 1, 0, 0},
       {0, 0, 1, 1, 1, 1, 0, 0},
       {0, 0, 0, 0, 0, 0, 1, 1}
};
int arrow[8];
int frame = 0;

void createSprite(int pixels[8][8], int sprite[8])
{
       int row, col;

       for (row = 0; row < 8; row++) {
               sprite[row] = 0;

               for (col = 0; col < 8; col++) {
                       sprite[row] += pixels[row][col] << (col ? col - 1 : 7);
               }
       }
}

// draws arrow on 8x8 led matrix w/ vertical offset of 0-7 inclusive (wraps)
void drawSprite(int sprite[8], int voffset)
{
       int row;

       for (row = 0; row < 8; row++) {
               max7219_put(row + 1, sprite[(row + voffset) % 8]);
       }
}

void setup()
{
       createSprite(arrow_pixels, arrow);
}

void loop()
{
       drawSprite(arrow, frame);
       delay(100);
       frame++;
}
[/code]
 
IP Logged
 
Reply #9 - 09/12/05 at 18:51:46

boppyer   Offline
YaBB Newbies
London

Posts: 39
*
 
Hi,
Thanks for your reply, I gave it a go but it came up with some weird results. I tried putting a single line in the array. and the LED displayed random pattern in a loop, quite pretty but not a line Sad

Is it because were parsing numbers from 0 to 7 rather than HEX numbers like the earlier example?

max7219_put(max7219_row7, max7219_col7 | max7219_col6 | max7219_col5 | max7219_col4 | max7219_col3 | max7219_col2 | max7219_col1 | max7219_col0);
max7219_put(max7219_row6, max7219_col7 | max7219_col0); etc

or is that also sending binary? sorry for the questions.

I would like to display multiple characters but would live with a single type or scroll. Many thanks for your help, sorry for my late reply but was a way from a computer Sad

best wishes

boppyer:-)
 
IP Logged
 
Reply #10 - 09/16/05 at 23:07:39

mellis   Offline
YaBB Newbies

Posts: 43
*
 
Sorry about that.  It's hard for me to debug because I don't have one of the displays on hand. 

Can you post your code?  And a description of the output you see?  For example, comment out the frame++ line - that should give you a constant output.  If it doesn't, please report that too.
 
IP Logged
 
Reply #11 - 09/22/05 at 12:23:36

boppyer   Offline
YaBB Newbies
London

Posts: 39
*
 
Hi,
sorry not to get back sooner but been a bit manic Sad I've taken a photo:

...

and the code is:

Code:
boolean arrow_pixels[8][8] = {
   {0, 0, 0, 0, 0, 0, 0, 0},
   {0, 0, 0, 0, 0, 0, 0, 0},
   {0, 0, 0, 0, 0, 0, 0, 0},
   {1, 1, 1, 1, 1, 1, 1, 1},
   {0, 0, 0, 0, 0, 0, 0, 0},
   {0, 0, 0, 0, 0, 0, 0, 0},
   {0, 0, 0, 0, 0, 0, 0, 0},
   {0, 0, 0, 0, 0, 0, 0, 0}
};
int arrow[8];
int frame = 0;

void createSprite(int pixels[8][8], int sprite[8])
{
   int row, col;

   for (row = 0; row < 8; row++) {
	sprite[row] = 0;

	for (col = 0; col < 8; col++) {
    sprite[row] += pixels[row][col] << (col ? col - 1 : 7);
	}
   }
}

// draws arrow on 8x8 led matrix w/ vertical offset of 0-7 inclusive (wraps)
void drawSprite(int sprite[8], int voffset)
{
   int row;

   for (row = 0; row < 8; row++) {
	max7219_put(row + 1, sprite[(row + voffset) % 8]);
   }
}

void setup()
{
	 setPinModes();
  max7219_init();
  createSprite(arrow_pixels, arrow);
}

void loop()
{
   drawSprite(arrow, frame);
   delay(100);
   //frame++;
}


 



I tried it will a simple line in the array to make it easier to debug. any help much appreciated, got my flash working nice though Smiley

kind regards

benjamin:-)
 
IP Logged
 
Reply #12 - 11/23/05 at 15:20:57

uki   Offline
YaBB Newbies

Posts: 19
*
 
Boppyer, please help,

I copied your code in wiring and each time he tells me :

In function `void setup()':
error: cannot convert `boolean (*)[8]' to `int (*)[8]' for argument `1' to `void createSprite(int (*)[8], int*)'

I don't understand why, did you have a same problem?
What can I do to correct a code?

thanks in advance,
uki
 
IP Logged
 
Reply #13 - 11/27/05 at 15:45:43

mellis   Offline
YaBB Newbies

Posts: 43
*
 
Try changing this line:

Code:
boolean arrow_pixels[8][8] = {   



to:

Code:
int arrow_pixels[8][8] = {   


 
IP Logged
 
Reply #14 - 11/27/05 at 18:14:35

uki   Offline
YaBB Newbies

Posts: 19
*
 
Thaks a lot, now it works!!


best,
uki
 
IP Logged
 
Pages: 1 2