function not working

I tried to create a function for the section of code that repeats itself in the loop function. After struggles with "declaring the function", and getting error free build, I ran it but the program doesn't run properly. Whereas the LEDs are expected to light up rapidly (80 in about 2 seconds), when the function is in place, i get one LED about every second!!

/*Name: PointsRGBCCWRotTEST
  Date: 5Apr2018
  Desc: Last device first: leds trace CCW while device rotates CCW (same direction) Doesn't
        display middle tree leds at bottom of each point: only edges display.  I2C addr used 24-20
  MCU:  Arduino UNO
  H/W:  MCP23017 port expanders on FIVE points
  Vers: 1.0
  Prog Mem:
  Global Var
  Execution Time:*/

#include <Wire.h>
#include <Centipede.h>
Centipede CS;
const int pins[3] = {//9,10,11 are DIO pins for RGB transistors.  Active high turns transistor on.
  9, 10, 11 //use 9, 10, 11 because these are PWM pins on UNO
};
int x = 10;//delay between LED blink, pretty cool at 10 (fast)
int j;
byte i;
const byte rgb[7] = {0x02, 0x04, 0x08, 0x0A, 0x06, 0x0C, 0x0E}; //00000010= 0x02

void setup() {
  Wire.begin(); // start I2C
  CS.initialize();

  //Init all 7 I2C devices (points, 2 pentagons)
  for (byte i = 0; i < 8; i++)
  {
    CS.portMode(i, 0x00); //
  }
  // initialize the DIO pins as outputs
  for (byte i = 0; i < 3; i++)
  { pinMode(pins[i], OUTPUT);
  }
}

void loop()
{
  for (byte i = 0; i < 7; i++)
  { PORTB = rgb[i]; //cycle 7 colors on pins 9,10,11

    for (int j = 64; j <= 79; j++) //device 24
    {
      CS.digitalWrite(j, HIGH);
      delay(x);
      CS.digitalWrite(j, LOW);
      delay(x);
    }

    for (int j = 48; j <= 63; j++) //device 23
    {
      CS.digitalWrite(j, HIGH);
      delay(x);
      CS.digitalWrite(j, LOW);
      delay(x);
    }


    for (int j = 32; j <= 43; j++) //device 22
    {
      CS.digitalWrite(j, HIGH);
      delay(x);
      CS.digitalWrite(j, LOW);
      delay(x);
    }


    for (int j = 16; j <= 27; j++) //device 21
    {
      CS.digitalWrite(j, HIGH);
      delay(x);
      CS.digitalWrite(j, LOW);
      delay(x);
    }


    for (int j = 0; j <= 11 ; j++) //Device 20
    {
      CS.digitalWrite(j, HIGH);
      delay(x);
      CS.digitalWrite(j, LOW);
      delay(x);
    }

  }

}

I put this function above Setup()

Void I2CWrite()
{CS.digitalWrite(j, HIGH);
delay(x);
CS.digitalWrite(j, LOW);
delay(x);}

then called it in the program in the 5 places shown, as {I2CWrite()}, expecting the 4 lines to replace the current 4 lines.

Any ideas why this doesn't work?

The variable j that you use in the for loops is not the same variable as the variable j that you defined in the beginning of your program.

Any ideas why this doesn't work?

No, because you forgot to post the revised code.

Okay, here is the code that doesn't work, as opposed to the previous code that did. My function is above setup() and is supposed to run 5 times in the loop function. I tried passing the parameter J into the function but that didn't work in the various scenarios I tried.

/*Name: PointsRGBCCWRotTEST
  Date: 5Apr2018
  Desc: Last device first: leds trace CCW while device rotates CCW (same direction) Doesn't
        display middle tree leds at bottom of each point: only edges display.  I2C addr used 24-20
  MCU:  Arduino UNO
  H/W:  MCP23017 port expanders on FIVE points
  Vers: 1.0
  Prog Mem:
  Global Var
  Execution Time:*/

#include <Wire.h>
#include <Centipede.h>
Centipede CS;
const int pins[3] = {//9,10,11 are DIO pins for RGB transistors.  Active high turns transistor on.
  9, 10, 11 //use 9, 10, 11 because these are PWM pins on UNO
};
int x = 10;//delay between LED blink, pretty cool at 10 (fast)
int j;
byte i;
const byte rgb[7] = {0x02, 0x04, 0x08, 0x0A, 0x06, 0x0C, 0x0E}; //00000010= 0x02

int i2cdev()
{ CS.digitalWrite(j, HIGH);
  delay(x);
  CS.digitalWrite(j, LOW);
  delay(x);
}
void setup() {
  Wire.begin(); // start I2C
  CS.initialize();

  //Init all 7 I2C devices (points, 2 pentagons)
  for (byte i = 0; i < 8; i++)
  {
    CS.portMode(i, 0x00); //
  }
  // initialize the DIO pins as outputs
  for (byte i = 0; i < 3; i++)
  { pinMode(pins[i], OUTPUT);
  }
}

void loop()
{
  for (byte i = 0; i < 7; i++)
  { PORTB = rgb[i]; //cycle 7 colors on pins 9,10,11

    for (int j = 64; j <= 79; j++) //device 24
    {
      i2cdev();
    }

    for (int j = 48; j <= 63; j++) //device 23
    {
      i2cdev();
    }

    for (int j = 32; j <= 43; j++) //device 22
    {
      i2cdev();
    }

    for (int j = 16; j <= 27; j++) //device 21
    {
      i2cdev();
    }

    for (int j = 0; j <= 11 ; j++) //Device 20
    {
      i2cdev();
    }

  }

}

Which j are you supposed to be using?

TolpuddleSartre:
Which j are you supposed to be using?

The one that is not being used :slight_smile:

This line defines a "j" that is local to the loop() function, and is invisible to functions outside the loop.

for (int j = 64; j <= 79; j++)

You have defined another "j" that is global.

But I defined J just before the Setup(). Doesn't that make it global?

All I did was replace 4 lines of code with a function, like saying "GoSub" in BASIC (where all variables are global).

So where do I put this declaration? I don't get any errors about undeclared variables...that's the crux of the problem.

The only j that the function can see is the global one.
Can you see what value it contains?

I don't get any errors about undeclared variables.

That's a good thing, isn't it?

You're not writing BASIC anymore, Toto.

Thanks Dorothy.

I'll display my values on an LCD to see what's going on. Just a pain to have to do this for something so basic...uh...simple.

One of the major problems with early versions of BASIC is that all variables were global.

C/C++ solves that problem by allowing you to declare local variables.

The 'j's that you are declaring in your for loops are hiding the global decalration of j . Either pass j as a parameter to the function, or remove the declarations in the for loops.

for (j = 64; j <= 79; j++)