How do I use variables in functions/procedures

I have a strip of LED’s from a dismantled server. They are driven by 5 CD4094s on the strip.

If I use a whole load of

void pin5()
{
  digitalWrite(clockPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, 0);//feeds last chip
  shiftOut(dataPin, clockPin, MSBFIRST, 0);//feeds forth chip
  shiftOut(dataPin, clockPin, MSBFIRST, 4);//feeds third chip
  shiftOut(dataPin, clockPin, MSBFIRST, 0);//feeds second chip
  shiftOut(dataPin, clockPin, MSBFIRST, 0);//feeds first chip 
  delay(300);
  digitalWrite (latchPin,HIGH);
  digitalWrite (latchPin,LOW);
  delay(500);
}

(with different numbers at the end of each line)I can get them to light one led.

unfortunately they don’t light in sequence, the LED’s are not wired in sequence with the shift registers.

is it possible to put five different variables in the brackets of the void to then change the 0’s and 4 to something else?

I could then use a number of arrays to produce the variables and get the lights to light in sequence (just to see if it can be done).

At present I have a load of voids doing part of the job and before I added more voids I thought there must be a better way.

Perhaps something like

void pin5(a,b,c,d,e)
{
  digitalWrite(clockPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, a);//
  shiftOut(dataPin, clockPin, MSBFIRST, b);//
  shiftOut(dataPin, clockPin, MSBFIRST, c);//
  shiftOut(dataPin, clockPin, MSBFIRST, d);//
  shiftOut(dataPin, clockPin, MSBFIRST, e);//feeds first chip 
  delay(300);
  digitalWrite (latchPin,HIGH);
  digitalWrite (latchPin,LOW);
  delay(500);
}

where a to e could be assigned in arrays.

Why do you keep saying void? That is an attribute. What is the object that the void is an attribute of?

Here is code that sets 1 pixel to a specific colour, in this case it is the colour clear or 0 but it could be any colour for any pixel. Because of this, I have no idea what your code is doping or if it is needed.

// Function to set all LEDs off
void clearLEDs()
{
  // Cycle through all LEDs
  for (int i=0; i<LED_COUNT; i++)
  {
    // Set color to zero which is off
    leds.setPixelColor(i, 0);
  }
}

Yes, you’re close, just need to add the data type..

void pin5(byte a, byte b, byte c, byte d, byte e)

good luck.. ~q

pin5() is a function. Functions can return a value. For example int pin5() would return an int. The void just means that pin5() doesn’t return anything.

I don't t think @matelot 's strip is ws2812/neopixel. Just a bunch of individual single colour LEDs on a strip driven by shift registers.

void pin5(byte myLEDs[], byte myLEDsLen)
{
  digitalWrite(clockPin, LOW);
  for (int8_t j=myLEDsLen-1; j>=0; j--)
    shiftOut(dataPin, clockPin, MSBFIRST, myLEDs[j]);
  digitalWrite (latchPin,HIGH);
  digitalWrite (latchPin,LOW);
}

Then call it with

byte pattern1[5] = (
  0b10101010,
  0b11111111,
  0b00001111,
  0b11110000,
  0b10101010
);

...
pin5(pattern1, sizeof(pattern1));

Ah, didn't understand that.

thanks guys, I am looking at a cross between qubits-us and paulRB’s answer

byte a[5] = {1,2,3,4,5};
byte b[5] = {1,2,3,4,5};

before setup and

void pin5( byte a[], byte b[])
{
  digitalWrite(clockPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, a[1]);//feeds first chip
  shiftOut(dataPin, clockPin, MSBFIRST, b[1]);//feeds first chip
  shiftOut(dataPin, clockPin, MSBFIRST, c);//feeds first chip
  shiftOut(dataPin, clockPin, MSBFIRST, d);//feeds first chip
  shiftOut(dataPin, clockPin, MSBFIRST, e);//feeds first chip 
  delay(300);
  digitalWrite (latchPin,HIGH);
  digitalWrite (latchPin,LOW);
  delay(500);

called from within the loop.

I have entered the above and it compiles.

I think I now need to put it in a for statement so the array number changes with each pass?

If I have assigned the data type in building the array why do I need to do it again in the void brackets?

Please, please, please stop referring to voids. They are functions. Each function name is preceded by a an identifier indicating the type of data that the function returns. If it does not return any data than the identifier is void

Liked it but didn't understand it? :thinking:

I'm not sure about @qubits-us answer, but you have definitely not understood my answer!

That's a strange design! Do you understand the mapping between the LEDs and the shift register output pins? It will be difficult for us to help you write code to light the LEDs in sequence if we don't know the mapping.

I thought I had?

I am looking for a way to make the ‘function’ ( thanks UKheliBob ) work and using 'byte a' instead of just 'a' compiles.

I have just typed a long explanation about trying to avoid the need for 40 arrays and trying to cut it down as each one will consist of 4 0’s and 1 number but in doing so I have come to the conclusion that I will need 40 arrays of 5 numbers each even if every one has 4 0’s in it so your for loop will work.

To answer your further post the shift registers are not connected to the lights in sequence for some reason. Each light glowed behind a panel and lit an instruction or a state written on the front of the panel.
I just thought I would try to get it to work, which I have and learning the code I am trying to make it do various things.

sounds like you need a way to turn on/off a light corresponding to a specific bit in a bit sequence 1<<N shifts a 1 N bits to the left, equal to 2 to the Nth power.

if you have a 5 byte array, you could set the Nth bit by using the integer division and mod operator that returns the remainder of a divition

arr [N /8]  |= 1 << (N %8);

and conversely to clear a bit

arr [s /8] &= ~(1 << (s %8));

I think I have, using your help, managed to get the lights to flash in sequence. I have a small problem with making it even shorter, I can call the function with

pin(led5);
pin(led6);
pin(led7);
pin(led8);

and it lights pins 5,6,7 and 8
what is the correct format to change this to a 'for' statement i.e.

for (int x=5;x < 9; x++)
{
pin(led(x));
}

I can then gradually change the value of x in the 'for' statement to call all 40 functions.

what is pin(), i thought you were using shift registers as output devices?

ouch, that’s a forever loop..

sorry.. ~q

/*
looking at plug of narrow strip of leds
with plug gaps on the bottom count back right as 1, right of that as 2 to 10 
then front right as 11 right of that as 12 right as 13 to 20
wire a long strip up with pin 1 live pin 2 gnd pin 13 to D8 pin 14 to D12
pin 15 to D11 and pin 16 to vcc*/
int latchPin = 8;
int clockPin = 12;
int dataPin = 11;
byte led5[5] = {0,0,4,0,0};
byte led6[5] = {0,0,8,0,0};
byte led7[5] = {0,0,16,0,0};
byte led8[5] = {0,0,32,0,0};
byte led9[5] = {0,0,64,0,0};
byte led10[5] = {0,0,128,0,0};
byte led11[5] = {0,0,0,0,1};
byte led12[5] = {0,0,0,0,2};
byte led13[5] = {0,0,0,0,4};
byte led14[5] = {0,0,0,0,8};
byte led15[5] = {0,0,0,0,16};
byte led16[5] = {0,0,0,0,32};
byte led17[5] = {0,4,0,0,0};
byte led18[5] = {0,8,0,0,0};
byte led19[5] = {0,0,0,0,64};
byte led20[5] = {0,0,0,0,128};
byte led21[5] = {0,0,0,1,0};
byte led22[5] = {0,0,0,2,0};
byte led23[5] = {0,16,0,0,0};
byte led24[5] = {0,32,0,0,0};
byte led25[5] = {0,64,0,0,0};
byte led26[5] = {0,128,0,0,0};
byte led27[5] = {0,0,0,4,0};
byte led28[5] = {0,0,0,8,0};
byte led29[5] = {0,0,0,16,0};
byte led30[5] = {0,0,0,32,0};
byte led31[5] = {1,0,0,0,0};
byte led32[5] = {2,0,0,0,0};
byte led33[5] = {4,0,0,0,0};
byte led34[5] = {8,0,0,0,0};
byte led35[5] = {0,0,0,64,0};
byte led36[5] = {0,0,0,128,0};
byte led37[5] = {0,0,1,0,0};
byte led38[5] = {0,0,2,0,0};
byte led39[5] = {16,0,0,0,0};
byte led40[5] = {32,0,0,0,0};
//int a = 0;
byte b[5] = {1,2,3,4,5};
int c = 4;
int d = 0;
int e = 1;
int f = 0;
int g = 0;
int h = 0;
int i = 0;
//int j = 0;
byte myLEDsLen = 5;
void setup() {
  // put your setup code here, to run once:
    Serial.begin(9600);
pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
}

void loop() 
{
//pin5();
//for (int x=5;x < 9; x++)
//{
//pin(led(x));
//}
pin(led5);
pin(led6);
pin(led7);
pin(led8);
pin(led9);
pin(led10);
pin(led11);
pin(led12);
pin(led13);
pin(led14);
pin(led15);
pin(led16);
pin(led17);
pin(led18);
pin(led19);
pin(led20);
pin(led21);
pin(led22);
pin(led23);
pin(led24);
pin(led25);
pin(led26);
pin(led27);
pin(led28);
pin(led29);
pin(led30);
pin(led31);
pin(led32);
pin(led33);
pin(led34);
pin(led35);
pin(led36);
pin(led37);
pin(led38);
pin(led39);
pin(led40);
//pincheck();
//pincheck2();
//pincheck3();
}
void pin( byte myLEDs[])
{
  digitalWrite(clockPin, LOW);
  for (byte j=0; j < 5; j++)
  // for (byte j = 0; c < 5; c++)
  {
    shiftOut(dataPin, clockPin, MSBFIRST, myLEDs[j]);
    Serial.print (myLEDs[j]);
    Serial.print ("   ");
    Serial.println (j);
  }
  delay(30);
  digitalWrite (latchPin,HIGH);
  digitalWrite (latchPin,LOW);
  delay(100);
}

//64,128 on first line does nothing
//1,2 on second line does nothing

void pincheck()
{
  digitalWrite(clockPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, 0);//feeds first chip
  shiftOut(dataPin, clockPin, MSBFIRST, 0);//feeds first chip
  shiftOut(dataPin, clockPin, MSBFIRST, 4);//feeds first chip
  shiftOut(dataPin, clockPin, MSBFIRST, 0);//feeds first chip
  shiftOut(dataPin, clockPin, MSBFIRST, 0);//feeds first chip 
  delay(300);
  digitalWrite (latchPin,HIGH);
  digitalWrite (latchPin,LOW);
  delay(100);
}
void pincheck2()
{
  digitalWrite(clockPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, 0);//feeds first chip
  shiftOut(dataPin, clockPin, MSBFIRST, 2);//feeds first chip
  shiftOut(dataPin, clockPin, MSBFIRST, 0);//feeds first chip
  shiftOut(dataPin, clockPin, MSBFIRST, 0);//feeds first chip
  shiftOut(dataPin, clockPin, MSBFIRST, 0);//feeds first chip 
  delay(300);
  digitalWrite (latchPin,HIGH);
  digitalWrite (latchPin,LOW);
  delay(100);
}
void pincheck3()
{
  digitalWrite(clockPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, 0);//feeds first chip
  shiftOut(dataPin, clockPin, MSBFIRST, 0);//feeds first chip
  shiftOut(dataPin, clockPin, MSBFIRST, 0);//feeds first chip
  shiftOut(dataPin, clockPin, MSBFIRST, 64);//feeds first chip
  shiftOut(dataPin, clockPin, MSBFIRST, 0);//feeds first chip 
  delay(300);
  digitalWrite (latchPin,HIGH);
  digitalWrite (latchPin,LOW);
  delay(100);
}

this is the whole thing, still not finished, I would like to replace all the lines

pin(led5);
pin(led6);
pin(led7);
pin(led8);
pin(led9);
pin(led10);
pin(led11);
pin(led12);
pin(led13);

etc with a for loop something like

for (int x=5;x < 9; x++)
{
pin(led(x));
}

but I can't find the correct format.

Perhaps a 2-D Array?