Go Down

Topic: MsTimer2 and arrays (Read 497 times) previous topic - next topic

greendude

Hi everyone

I've been trying to use MsTimer2 to update a led array outside of the main loop but I'm having a little trouble writing the update function without including parameters. Especially trying to call the array of numbers into the function. If you guys could take a look I would be grateful.

Cheers

Code: [Select]

#include <MsTimer2.h>

void setup() {
  int i=0;
  pinMode (6,OUTPUT);
  pinMode (7,OUTPUT);
  pinMode (8,OUTPUT);
 int array[] ={0,0,0,0,0,0,0,0};
 
 MsTimer2::set(1, updatearray);
 MsTimer2::start();
}

void loop() {
}

void updatearray() {
 static int i;
 if (i < 8)
 {shift(i);
 i = i+1;
 }
 else
 {i=0;
 shift(i);
 i=i+1;}
}

void shift(int i)
{
digitalWrite(6,1);
 shiftOut(7,8,MSBFIRST,array[i]);
 shiftOut(7,8,MSBFIRST,1<<i);
 digitalWrite(6,0);
}

mikalhart

Hey, greendude...

When you declare a variable like "array" inside a function, it is only visible from within that function.  Since your "array" lives inside setup(), the code inside shift() can't get to it.  An easy solution is just to move it outside of setup() like this:

Code: [Select]

int array[] ={0,0,0,0,0,0,0,0};

void setup() {
...


Mikal


Go Up