Wiring Forum
http://wiring.org.co/cgi-bin/yabb/YaBB.pl
Programming Questions & Help >> Syntax >> for (i=0; i<7; i=i+1)
http://wiring.org.co/cgi-bin/yabb/YaBB.pl?num=1126118886

Message started by boppyer on 09/07/05 at 18:48:06

Title: for (i=0; i<7; i=i+1)
Post by boppyer on 09/07/05 at 18:48:06
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 :-)

boppyer

Title: Re: for (i=0; i<7; i=i+1)
Post by michael05 on 09/07/05 at 22:32:57
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;

Title: Re: for (i=0; i<7; i=i+1)
Post by boppyer on 09/08/05 at 11:27:49
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

Title: Re: for (i=0; i<7; i=i+1)
Post by zambetti on 09/08/05 at 12:21:25
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]);

Title: Re: for (i=0; i<7; i=i+1)
Post by boppyer on 09/08/05 at 14:20:03
thanks for that, it worked, but one more question :-)

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 :-)

kind regards

boppyer :-)

Title: Re: for (i=0; i<7; i=i+1)
Post by zambetti on 09/09/05 at 04:09:01
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. :) 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;
}

Title: Re: for (i=0; i<7; i=i+1)
Post by boppyer on 09/09/05 at 11:31:21
that works a treat :-) nice one for that, it's brilliant. :-) :-)

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 :-)

kind regards

boppyer:-)

Title: Re: for (i=0; i<7; i=i+1)
Post by boppyer on 09/09/05 at 13:42:08
also does anyone know what would be the best way to scroll through mutilple arrays? (scrolling text?) :-) I'm liking my LED, i would recomend getting one :-)

cheers

boppyer

Title: Re: for (i=0; i<7; i=i+1)
Post by mellis on 09/10/05 at 14:34:30
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++;
}

Title: Re: for (i=0; i<7; i=i+1)
Post by boppyer on 09/12/05 at 18:51:46
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 :-(

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 :-(

best wishes

boppyer:-)

Title: Re: for (i=0; i<7; i=i+1)
Post by mellis on 09/16/05 at 23:07:39
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.

Title: Re: for (i=0; i<7; i=i+1)
Post by boppyer on 09/22/05 at 12:23:36
Hi,
sorry not to get back sooner but been a bit manic :-( 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 :-)

kind regards

benjamin:-)

Title: Re: for (i=0; i<7; i=i+1)
Post by uki on 11/23/05 at 15:20:57
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

Title: Re: for (i=0; i<7; i=i+1)
Post by mellis on 11/27/05 at 15:45:43
Try changing this line:


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


to:


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


Title: Re: for (i=0; i<7; i=i+1)
Post by uki on 11/27/05 at 18:14:35
Thaks a lot, now it works!!


best,
uki

Title: Re: for (i=0; i<7; i=i+1)
Post by boppyer on 11/29/05 at 00:37:26
Cheers David, it's works brilliantly :-)
much appreciated :-)

Title: Re: for (i=0; i<7; i=i+1)
Post by Tiago_D on 07/28/10 at 22:20:39
hi i made this function to scroll the text in my code for my Matrix, everything works as it should be but there is a space between letters that I wanted to remove i tried everything but I do not know what to do
I'm a little inexperienced in programming and therefore normal that the code is not best done

someone can help me please?

sorry my bad english.



Code:
void scroll (Sprite ch)
{
  for(int i=-8;i<=8;i++)
  {
    myMatrix.write(1, i, ch);
    myMatrix.write(1 - 8, i, ch);
    delay(200); 
    myMatrix.clear();   
  }
}

Title: Re: for (i=0; i<7; i=i+1)
Post by barragan on 07/28/10 at 22:52:34
Could you please post your complete code? I cant´t see what you´re doing. This example might be a reference: http://wiring.org.co/learning/libraries/spriteanimation.html

Title: Re: for (i=0; i<7; i=i+1)
Post by Tiago_D on 07/29/10 at 00:26:05
1ªpart


Code:
#include <Sprite.h>
#include <Matrix.h>

/* create a new Matrix instance
   pin 0: data  (din)
   pin 1: load  (load)
   pin 2: clock (clk)
*/
Matrix myMatrix = Matrix(10, 12, 11,1);

//letras (acabar de escrever todas)

Sprite letra_A = Sprite(8,8,
B11110000,
B00101000,
B00100100,
B00100010,
B00100100,
B00101000,
B11110000,
B00000000
);

Sprite  letra_B = Sprite(8,8,
B10000010,
B11111110,
B10010010,
B10010010,
B10010010,
B10010010,
B01101100,
B00000000
);

Sprite letra_C = Sprite(8,8,
B00111000,
B01000100,
B10000010,
B10000010,
B10000010,
B10000010,
B10000010,
B00000000
);

Sprite letra_D = Sprite(8,8,
B10000010,
B11111110,
B10000010,
B10000010,
B10000010,
B01000100,
B00111000,
B00000000
);
                      
Sprite letra_E = Sprite(8,8,
B11111110,
B10010010,
B10010010,
B10010010,
B10010010,
B10000010,
B10000010,
B00000000
);

Sprite letra_F = Sprite(8,8,
B11111110,
B00010010,
B00010010,
B00010010,
B00010010,
B00000010,
B00000010,
B00000000
);

Sprite letra_G = Sprite(8,8,
B00111000,
B01000100,
B10000010,
B10010010,
B10010010,
B10010010,
B11110010,
B00000000
);

Sprite letra_H = Sprite(8,8,
B11111110,
B00010000,
B00010000,
B00010000,
B00010000,
B00010000,
B11111110,
B00000000
);

Sprite letra_I = Sprite(8,8,
B10000010,
B10000010,
B10000010,
B11111110,
B10000010,
B10000010,
B10000010,
B00000000
);

Sprite  letra_J = Sprite(8,8,
B01000000,
B10000000,
B10000000,
B10000010,
B10000010,
B10000010,
B01111110,
B00000000
);

Sprite  letra_K = Sprite(
B11111110,
B00010000,
B00010000,
B00101000,
B01000100,
B10000010,
B10000010,
B00000000
);

Sprite letra_L = Sprite(8,8,
B11111110,
B10000000,
B10000000,
B10000000,
B10000000,
B10000000,
B10000000,
B00000000
);

Sprite  letra_M = Sprite(8,8,
B11111110,
B00000010,
B00000010,
B00001100,
B00000010,
B00000010,
B11111110,
B00000000
);

Sprite  letra_N = Sprite(8,8,
B11111110,
B00000100,
B00001000,
B00010000,
B00100000,
B01000000,
B11111110,
B00000000
);

Sprite  letra_O = Sprite(8,8,
B00111000,
B01000100,
B10000010,
B10000010,
B10000010,
B01000100,
B00111000,
B00000000
);

Sprite  letra_P = Sprite(8,8,
B00000010,
B11111110,
B00010010,
B00010010,
B00010010,
B00010010,
B00001100,
B00000000
);

Sprite  letra_Q = Sprite(8,8,
B00111000,
B01000100,
B10000010,
B10000010,
B10100010,
B01000100,
B10111000,
B00000000
);

Sprite  letra_R = Sprite(8,8,
B00000010,
B11111110,
B00000010,
B00010010,
B00110010,
B01010010,
B10001100,
B00000000
);

Sprite  letra_S = Sprite(8,8,
B10001100,
B10010010,
B10010010,
B10010010,
B10010010,
B10010010,
B01100010,
B00000000
);

Sprite  letra_T = Sprite(8,8,
B00000010,
B00000010,
B00000010,
B11111110,
B00000010,
B00000010,
B00000010,
B00000000
);

Sprite  letra_U = Sprite(8,8,
B01111110,
B10000000,
B10000000,
B10000000,
B10000000,
B10000000,
B01111110,
B00000000
);

Sprite  letra_V = Sprite(8,8,
B00011110,
B00100000,
B01000000,
B10000000,
B01000000,
B00100000,
B00011110,
B00000000
);
                  
Sprite  letra_W = Sprite(8,8,
B01111110,
B10000000,
B10000000,
B01111100,
B10000000,
B10000000,
B01111110,
B00000000
);

Sprite  letra_X = Sprite(8,8,
B10000010,
B01000100,
B00101000,
B00010000,
B00101000,
B01000100,
B10000010,
B00000000
);

Sprite letra_Y = Sprite(8,8,
B00000010,
B00000100,
B00001000,
B11110000,
B00001000,
B00000100,
B00000010,
B00000000
);

Sprite letra_Z = Sprite(8,8,
B10000010,
B11010010,
B10110010,
B10010010,
B10011010,
B10010110,
B10000010,
B00000000
);

//numeros

Sprite numero_1 = Sprite(8,8,
B00000000,
B00000000,
B10000000,
B11111111,
B10000010,
B00000000,
B00000000,
B00000000
);

Sprite numero_2 = Sprite(8,8,
B00000000,
B10000110,
B10001001,
B10010001,
B10100001,
B11000001,
B10000110,
B00000000

);

Sprite numero_3 = Sprite(8,8,
B00000000,
B01111100,
B10010010,
B10010010,
B10010010,
B10000010,
B10000010,
B00000000
);

Sprite numero_4 = Sprite(8,8,
B00000000,
B11111111,
B00010000,
B00010000,
B00010000,
B00010000,
B00011111,
B00000000
);

Sprite numero_5 = Sprite(8,8,
B00000000,
B01001111,
B10001001,
B10001001,
B10001001,
B10001001,
B01110001,
B00000000
);

Sprite numero_6 = Sprite(8,8,
B00000000,
B01111110,
B10001001,
B10001001,
B10001001,
B10001001,
B01110010,
B00000000
);

Sprite numero_7 = Sprite(8,8,
B00000000,
B00000111,
B00001001,
B00010001,
B00100001,
B11000001,
B00000001,
B00000000
);

Sprite numero_8 = Sprite(8,8,
B00000000,
B01101100,
B10010010,
B10010010,
B10010010,
B10010010,
B01101100,
B00000000
);

Sprite numero_9 = Sprite(8,8,
B00000000,
B01111110,
B10001001,
B10001001,
B10001001,
B10001001,
B01000110,
B00000000
);

Sprite numero_0 = Sprite(8,8,
B00000000,
B01111110,
B11000001,
B10010001,
B10001001,
B10000011,
B01111110,
B00000000
);

//simbolos

Sprite simbolo_ponto = Sprite(8,8,
B00000000,
B00000000,
B00000000,
B11000000,
B11000000,
B00000000,
B00000000,
B00000000
);

Sprite simbolo_traco = Sprite(8,8,
B00000000,
B00000000,
B00000000,
B01111100,
B01111100,
B00000000,
B00000000,
B00000000
);


void setup()
{
   myMatrix.setBrightness(10);  //intencidade da matriz
}

void scroll (Sprite ch)   //scroll do texto  (com problemas)
{
  for(int i=-8;i<=8;i++)
  {
    myMatrix.write(1, i, ch);
    myMatrix.write(1 - 8, i, ch);
    delay(200); 
    myMatrix.clear();   
  }
}

Title: Re: for (i=0; i<7; i=i+1)
Post by Tiago_D on 07/29/10 at 00:27:10
2ªpart


Code:
void scrollString(char * s) //passagem por parametros das letras para a função scroll
{
  int i=0;
  while(s[i])
  {
    //letras
   if(s[i]== 'a')
  scroll(letra_A);
   if(s[i]== 'b')
  scroll(letra_B);
   if(s[i]== 'c')
  scroll(letra_C);
   if(s[i]== 'd')
  scroll(letra_D);
  if(s[i]== 'e')
  scroll(letra_E);
  if(s[i]== 'f')
  scroll(letra_F);
  if(s[i]== 'g')
  scroll(letra_G);
  if(s[i]== 'h')
  scroll(letra_H);
  if(s[i]== 'i')
  scroll(letra_I);
  if(s[i]== 'j')
  scroll(letra_J);
  if(s[i]== 'k')
  scroll(letra_K);
  if(s[i]== 'l')
  scroll(letra_L);
  if(s[i]== 'm')
  scroll(letra_M);
  if(s[i]== 'n')
  scroll(letra_N);
  if(s[i]== 'o')
  scroll(letra_O);
  if(s[i]== 'p')
  scroll(letra_P);
  if(s[i]== 'q')
  scroll(letra_Q);
  if(s[i]== 'r')
  scroll(letra_R);
  if(s[i]== 's')
  scroll(letra_S);
  if(s[i]== 't')
  scroll(letra_T);
  if(s[i]== 'u')
  scroll(letra_U);
  if(s[i]== 'v')
  scroll(letra_V);
  if(s[i]== 'x')
  scroll(letra_X);
  if(s[i]== 'y')
  scroll(letra_Y);
  if(s[i]== 'z')
  scroll(letra_Z);
  //numeros
  if(s[i]== '1')
  scroll(numero_1);
  if(s[i]== '2')
  scroll(numero_2);
  if(s[i]== '3')
  scroll(numero_3);
  if(s[i]== '4')
  scroll(numero_4);
  if(s[i]== '5')
  scroll(numero_5);
  if(s[i]== '6')
  scroll(numero_6);
  if(s[i]== '7')
  scroll(numero_7);
  if(s[i]== '8')
  scroll(numero_8);
  if(s[i]== '9')
  scroll(numero_9);
  if(s[i]== '0')
  scroll(numero_0);
  //simbolos
  if(s[i]== '.')
  scroll(simbolo_ponto);
  if(s[i]== '-')
  scroll(simbolo_traco);
  i++;
  }
}
void loop () {
 
    scrollString("abcdfghijklmnopkrstuvwxyz123456789"); //onde se insere a palavra letras ou numeros
  }

Title: Re: for (i=0; i<7; i=i+1)
Post by barragan on 07/29/10 at 00:55:15
cual es el problema que estás tendiendo exactamente?

Title: Re: for (i=0; i<7; i=i+1)
Post by Tiago_D on 07/29/10 at 01:33:44
http://www.youtube.com/watch?v=lM_22OGfx-Q

see the video is more easy and that the problem can not remove the space between letters

Title: Re: for (i=0; i<7; i=i+1)
Post by barragan on 07/29/10 at 02:41:36
drawing always in position 1 and 7 (1-8)?
myMatrix.write(1, i, ch);
myMatrix.write(1 - 8, i, ch);

Title: Re: for (i=0; i<7; i=i+1)
Post by Tiago_D on 07/29/10 at 03:00:15
I based myself in this code


Code:
#include <Matrix.h>
#include <Binary.h>
#include <Sprite.h>

Matrix myMatrix = Matrix(0, 2, 1);

/* create a new Sprite instance
   8 pixels wide, 4 pixels tall
*/
Sprite wave = Sprite(
  8, 4,
  B00011000,
  B00100100,
  B01000010,
  B10000001
);

void setup()
{
}

int x = 0;

void loop()
{
  myMatrix.write(x, 2, wave);      // place sprite on screen
  myMatrix.write(x - 8, 2, wave);  // place sprite again, elsewhere on screen
  delay(75);                       // wait a little bit
  myMatrix.clear();                // clear the screen for next animation frame
  if(x == 8)                       // if reached end of animation sequence
  {
    x = 0;                         // start from beginning
  }
  x++;                             // advance x coordinate to the right
}


Title: Re: for (i=0; i<7; i=i+1)
Post by barragan on 07/29/10 at 03:01:10
I see, but as you can see x is changing for moving the sprite, in your case it is fixed.

Title: Re: for (i=0; i<7; i=i+1)
Post by Tiago_D on 07/29/10 at 03:23:26
yes I am using i to sprite I'm changing the position of y for sprite text

Title: Re: for (i=0; i<7; i=i+1)
Post by barragan on 07/29/10 at 06:31:37
then it should be something like myMatrix.write(1, i, ch); myMatrix.write(1, i-8, ch); ?

Title: Re: for (i=0; i<7; i=i+1)
Post by Tiago_D on 07/29/10 at 14:19:37
still not working well thanks anyway but I will try to resolve it if I can't I come back here to post

thanks for the help

Wiring Forum » Powered by YaBB 2.5 AE!
YaBB Forum Software © 2000-2010. All Rights Reserved.