can I call a function to replace a for statement?

I have enclosed the program as far as I have got

/*
	3 shift registers in, 2 out.

        Drive 2 banks of 8 LED's with 2 banks of 8 switches (with 1 extra input bank added for future expansion).
        
        The circuit:
	Connected to 3 cd 4021's with the inputs via switches to 5v and with 10k resistors to 0v.
        Connected to 2 74LS595's with their outputs connected via resistors and LED's to 0v.

	Created 09.11.13
	By matelot
	Modified ------------
	By ----------- 
set up CD4021 with
pin1 - i/p 1      pin16 - vcc
pin2 - n/c        pin15 - i/p 2      
pin3 - data in    pin14 - i/p 3
pin4 - i/p 5      pin13 - i/p 4
pin5 - i/p 6      pin12 - n/c
pin6 - i/p 7      pin11 - n/c
pin7 - i/p 8      pin10 - clock
pin8 - gnd         pin9  - ser/par switch (latch)
bias inputs with resistors to gnd and put a switch on each input to vcc
*/
//connected to 2 cd4021's with the pin 11 on the first connected to pin 3 on the second.
const int latchPinin  =  5; //4021 pin 9
const int dataPinin   =  6;  //4021 pin 3
const int clockPinin  =  7; //4021 pin 10
/*
set up 74ls595 with
pin1 - o/p 2      pin16 - vcc
pin2 - o/p 3      pin15 - o/p 1      
pin3 - o/p 4      pin14 - data i/p
pin4 - o/p 5      pin13 - gnd
pin5 - o/p 6      pin12 - latch
pin6 - o/p 7      pin11 - clock
pin7 - o/p 8      pin10 - vcc
pin8 - gnd        pin9  - to pin 14 of next register
connected to 2 47LS595's with the pin 9 on the first connected to pin 12 on the second.
myinput bits are 1.WO1 2.WO2 3.WO3 4.WO4 
myoutput bits are 1.WO1red
*/
const int dataPinout  =  8;   //595 pin 14
const int latchPinout =  9;  //595 pin 11
const int clockPinout = 10; //595 pin 12
int myinput;
int myoutput = 136;
int myoutput2;
int a;
int i;
int shift1[] = {0,1,2,3};
int shift2[] = {4,5,6,7};
int shift3[] = {1,0,0,0};
int shift4[] = {0,1,0,0};
int shift5[] = {0,1,1,0};
int shift6[] = {0,0,0,1};

void setup()
{
  Serial.begin(9600);
  pinMode(dataPinout,  OUTPUT);
  pinMode(latchPinout, OUTPUT);
  pinMode(clockPinout, OUTPUT);
  pinMode(latchPinin,  OUTPUT);
  pinMode(clockPinin,  OUTPUT); 
  pinMode(dataPinin,    INPUT);
}

void loop()
{
   delayMicroseconds  (20);
   digitalWrite       (clockPinin,HIGH);/*puts the clockpin high so the first
                                bit is read before the register is clocked on*/
   digitalWrite       (latchPinin,1);  //set latch to 1 to collect parallel data
   delayMicroseconds  (20);                //pause to let digitalWrite finish
   digitalWrite       (latchPinin,0); //set latch to 0 to transmit data serially
   digitalWrite       (latchPinout, LOW); 
     digitalWrite       (clockPinin,HIGH); 
   shiftOut           (dataPinout, clockPinout, MSBFIRST,myoutput); //Send data
//   shiftOut           (dataPinout, clockPinout, MSBFIRST,myoutput2); //Send data   
   digitalWrite       (latchPinout, HIGH);

   Serial.print    ("   ");       //sets a loop so the input can be bitread
   for (a=0;a<8;a++)              //so that leading 0's line up in the monitor
   Serial.print    (bitRead (myinput,(7-a))); //bit read the contents of input
   Serial.print    ("  ");
   Serial.println   (myinput);//print myinput in decimal
   
   myinput  = shiftIn (dataPinin, clockPinin, MSBFIRST);//read first register
   /*hct4021 register 1    bit 0 WO1       bit 1 WO2       bit 2 WO3        bit 3 WO4
             (myinput)     bit 4 TS12c     bit 5 TS12d     bit 6            bit 7
   74ls595   register 1    bit 0 WO1 red   bit 1 WO1 yel   bit 2 WO1 yel2   bit 3 WO1 grn
             (myoutput)    bit 4 WO2 red   bit 5 WO2 yel   bit 6 WO2 yel2   bit 7 WO2 grn
   74ls595   register 2    bit 0 WO3 red   bit 1 WO3 yel   bit 2 WO3 grn    bit 3 WO4 red
             (myoutput2)   bit 4 WO4 grn   bit 5 21  red   bit 6 21  yel    bit 7 21  grn
              
   */ 
   if (bitRead(myinput,0) == 1)//read bit WO1
   {
   for (i=0;i<4;i++)
     {
       output(shift1[i],shift3[i]);//WO1 to red
     }
   }
   if (bitRead(myinput,1) == 1)//read bit WO2
   {
     for (i=0;i<4;i++)
     {
       output(shift1[i],shift4[i]);//WO1 to yel
       output(shift2[i],shift3[i]);//WO2 to red
     }
   }
   if (bitRead(myinput,2) == 1)//read bit WO3
   {
     if (bitRead(myoutput,1) == 1)//is WO1 yel
       {
         for (i=0;i<4;i++)       
         output(shift1[i],shift5[i]);//WO1 to yel yel
       }
     for (i=0;i<4;i++)
     {
       output(shift2[i],shift4[i]);//WO2 to yel
     }
     //for (i=0;i<4;i++)
     count;
     {
       output2(shift1[i],shift3[i]);//WO3 to red
     }
   }
   if (bitRead(myinput,3) == 1)//read bit WO4
   {
     if (bitRead(myoutput,2) == 1)//is WO1 yel yel
       {     
       for (i=0;i<4;i++)
         output(shift1[i],shift6[i]);//WO1 to grn
       }
     if (bitRead(myoutput,5) == 1)//is WO2 yel
       {
       count;
         output(shift2[i],shift5[i]);//WO2 to yel yel
       }
       count;
         output2(shift1[i],shift4[i]);//WO3 to yel
       count;
         output2(shift1[i],shift6[i]);//WO4 red
   }
     
//     for (i=0;i<4;i++)
//     {
//       output(shift2[i],shift5[i]);//WO2 to yel yel
//       output(shift1[i],shift4[i]);
//     }
   
}
void output(int first,int second)//do void output using the first array in the 
          //instruction above(shift1[bit i])wherever the variable first is below
          //and then the second array in the instruction above (shift2[bit i])
          //wherever second appears below.
{
     bitWrite(myoutput,first,second);//fill array (myoutput,bit,high or low)
     Serial.print (first);
     Serial.print ("  ");
     Serial.println (second);
     delay(5);
 //    bitWrite(myoutput,1,0);//put WO1 yel  low
 //    bitWrite(myoutput,2,0);//put WO1 yel2 low
 //    bitWrite(myoutput,3,0);//put WO1 grn  low      
}
void output2(int first,int second)//do void output using the first array in the 
          //instruction above(shift1[bit i])wherever the variable first is below
          //and then the second array in the instruction above (shift2[bit i])
          //wherever second appears below.
{
     bitWrite(myoutput2,first,second);//fill array (myoutput,bit,high or low)
     Serial.print (first);
     Serial.print ("  ");
     Serial.println (second);
     delay(5);     
}
void count()
{
     for (i=0;i<4;i++);//can this replace all the instances above?
}

but I am curious, I want to reuse
for (i=0;i<4;i++)
by putting it in a function and calling it but when I do it fails to work.
i.e. if I do

 for (i=0;i<4;i++)
     {
       output(shift1[i],shift4[i]);//WO1 to yel
       output(shift2[i],shift3[i]);//WO2 to red
     }

it works fine but if I do

     count;
     {
       output(shift1[i],shift4[i]);//WO1 to yel
       output(shift2[i],shift3[i]);//WO2 to red
     }

void count()
{
     for (i=0;i<4;i++)
}

it fails, I assume because there is a ; after the count that then stops it seeing the lines in the brackets.
Am I right and is there a way out of this so I don't have to do the line every time.

Am I right and is there a way out of this so I don't have to do the line every time.

No you are wrong. What you are trying to do makes no sense at all.

I'm sure if you were to spend less time looking through the forums for things to criticise and more time reading the topics you will understand better.

What you are trying to do makes no sense at all.

I am trying to save space by calling a function instead of writing the same line of text every time I need it.
I want to know if there is any way of doing so in this instance.
It works when I don't need brackets around the next lines.

No there is not a way to call a function and have it do that. You just want to replace the text, a function would replace the whole block of code.

You could write a function that does the whole thing including the two calls to output() in the for loop. If you need information for that to work then pass it to said function you create.

If you just want to replace text there's always the preprocessor macros and defines but that would be a very slippery and dangerous slope in this case.

If you just want to save the typing, select the for loop and press control+c on your keyboard. That will copy it to the clipboard. Then anytime you want to type it you just press control+v and it will insert that text right in there with one keystroke.

thank you Delta_G. The more I am playing with it the more I realise that my previous statement is wrong.

It works when I don't need brackets around the next lines.

is incorrect, it is failing to work.
I think I understand

if you need information for that to work then pass it to said function you create.

Do you mean if the values are different in different lines I can pass different values to be sent to the output function each time?
lines such as

       output(shift1[i],shift4[i]);//WO1 to yel
       output(shift2[i],shift3[i]);//WO2 to red

are different every time.
I can't, at this moment, see how to do that but I will work on it.
Thanks for the reply.

matelot:
I am trying to save space by calling a function instead of writing the same line of text every time I need it.
I want to know if there is any way of doing so in this instance.
It works when I don't need brackets around the next lines.

No; that code is not even remotely close to valid c++ and makes no sense at all.

matelot:
I am trying to save space by calling a function instead of writing the same line of text every time I need it.

Can't you just use a #define? I just tried this - it works:

#include <stdio.h>

#define count for(i=0;i<4;i++)

int main (void)
{
        int i;
        count {
                fprintf (stdout, "loop the loop %d\n", i);
        }
        return 0;
}

This would work:

#define count for (int i=0;i<4;i++)
    ...

     count
     {
       output(shift1[i],shift4[i]);//WO1 to yel
       output(shift2[i],shift3[i]);//WO2 to red
     }

But it ranks highly on the list of the most AWFUL ways of coding anything. Stick to writing out the for loop.

(EDIT: Krupski beat me to it.)


What you could also do is this:

/*
	3 shift registers in, 2 out.
 
 Drive 2 banks of 8 LED's with 2 banks of 8 switches (with 1 extra input bank added for future expansion).
 
 The circuit:
 	Connected to 3 cd 4021's with the inputs via switches to 5v and with 10k resistors to 0v.
 Connected to 2 74LS595's with their outputs connected via resistors and LED's to 0v.
 
 	Created 09.11.13
 	By matelot
 	Modified ------------
 	By ----------- 
 set up CD4021 with
 pin1 - i/p 1      pin16 - vcc
 pin2 - n/c        pin15 - i/p 2      
 pin3 - data in    pin14 - i/p 3
 pin4 - i/p 5      pin13 - i/p 4
 pin5 - i/p 6      pin12 - n/c
 pin6 - i/p 7      pin11 - n/c
 pin7 - i/p 8      pin10 - clock
 pin8 - gnd         pin9  - ser/par switch (latch)
 bias inputs with resistors to gnd and put a switch on each input to vcc
 */
//connected to 2 cd4021's with the pin 11 on the first connected to pin 3 on the second.
const int latchPinin  =  5; //4021 pin 9
const int dataPinin   =  6;  //4021 pin 3
const int clockPinin  =  7; //4021 pin 10
/*
set up 74ls595 with
 pin1 - o/p 2      pin16 - vcc
 pin2 - o/p 3      pin15 - o/p 1      
 pin3 - o/p 4      pin14 - data i/p
 pin4 - o/p 5      pin13 - gnd
 pin5 - o/p 6      pin12 - latch
 pin6 - o/p 7      pin11 - clock
 pin7 - o/p 8      pin10 - vcc
 pin8 - gnd        pin9  - to pin 14 of next register
 connected to 2 47LS595's with the pin 9 on the first connected to pin 12 on the second.
 myinput bits are 1.WO1 2.WO2 3.WO3 4.WO4 
 myoutput bits are 1.WO1red
 */
const int dataPinout  =  8;   //595 pin 14
const int latchPinout =  9;  //595 pin 11
const int clockPinout = 10; //595 pin 12
int myinput;
int myoutput = 136;
int myoutput2;
int a;
int i;
int shift1[] = {
  0,1,2,3};
int shift2[] = {
  4,5,6,7};
int shift3[] = {
  1,0,0,0};
int shift4[] = {
  0,1,0,0};
int shift5[] = {
  0,1,1,0};
int shift6[] = {
  0,0,0,1};

void setup()
{
  Serial.begin(9600);
  pinMode(dataPinout,  OUTPUT);
  pinMode(latchPinout, OUTPUT);
  pinMode(clockPinout, OUTPUT);
  pinMode(latchPinin,  OUTPUT);
  pinMode(clockPinin,  OUTPUT); 
  pinMode(dataPinin,    INPUT);
}

void count(void (*fptr)(int, int), int* a, int* b)
{
  for (i=0;i<4;i++) {
    fptr(a[i],b[i]);
  }
}
void loop()
{
  delayMicroseconds  (20);
  digitalWrite       (clockPinin,HIGH);/*puts the clockpin high so the first
   bit is read before the register is clocked on*/
  digitalWrite       (latchPinin,1);  //set latch to 1 to collect parallel data
  delayMicroseconds  (20);                //pause to let digitalWrite finish
  digitalWrite       (latchPinin,0); //set latch to 0 to transmit data serially
  digitalWrite       (latchPinout, LOW); 
  digitalWrite       (clockPinin,HIGH); 
  shiftOut           (dataPinout, clockPinout, MSBFIRST,myoutput); //Send data
  //   shiftOut           (dataPinout, clockPinout, MSBFIRST,myoutput2); //Send data   
  digitalWrite       (latchPinout, HIGH);

  Serial.print    ("   ");       //sets a loop so the input can be bitread
  for (a=0;a<8;a++)              //so that leading 0's line up in the monitor
      Serial.print    (bitRead (myinput,(7-a))); //bit read the contents of input
  Serial.print    ("  ");
  Serial.println   (myinput);//print myinput in decimal

  myinput  = shiftIn (dataPinin, clockPinin, MSBFIRST);//read first register
  /*hct4021 register 1    bit 0 WO1       bit 1 WO2       bit 2 WO3        bit 3 WO4
   (myinput)     bit 4 TS12c     bit 5 TS12d     bit 6            bit 7
   74ls595   register 1    bit 0 WO1 red   bit 1 WO1 yel   bit 2 WO1 yel2   bit 3 WO1 grn
   (myoutput)    bit 4 WO2 red   bit 5 WO2 yel   bit 6 WO2 yel2   bit 7 WO2 grn
   74ls595   register 2    bit 0 WO3 red   bit 1 WO3 yel   bit 2 WO3 grn    bit 3 WO4 red
   (myoutput2)   bit 4 WO4 grn   bit 5 21  red   bit 6 21  yel    bit 7 21  grn
   
   */
  if (bitRead(myinput,0) == 1)//read bit WO1
  {
    count(&output,shift1,shift3);//WO1 to red
  }
  if (bitRead(myinput,1) == 1)//read bit WO2
  {
    for (i=0;i<4;i++)
    {
      output(shift1[i],shift4[i]);//WO1 to yel
      output(shift2[i],shift3[i]);//WO2 to red
    }
  }
  if (bitRead(myinput,2) == 1)//read bit WO3
  {
    if (bitRead(myoutput,1) == 1)//is WO1 yel
    {
      count(&output,shift1,shift5);//WO1 to yel yel
    }
    count(output,shift2,shift4);//WO2 to yel
    count(output2,shift1,shift3);//WO3 to red
  }
  if (bitRead(myinput,3) == 1)//read bit WO4
  {
    if (bitRead(myoutput,2) == 1)//is WO1 yel yel
    {     
      count(&output,shift1,shift6);//WO1 to grn
    }
    if (bitRead(myoutput,5) == 1)//is WO2 yel
    {
      count(&output,shift2,shift5);//WO2 to yel yel
    }
    count(&output2,shift1,shift4);//WO3 to yel
    count(&output2,shift1,shift6);//WO4 red
  }

  //     for (i=0;i<4;i++)
  //     {
  //       output(shift2[i],shift5[i]);//WO2 to yel yel
  //       output(shift1[i],shift4[i]);
  //     }

}
void output(int first,int second)//do void output using the first array in the 
//instruction above(shift1[bit i])wherever the variable first is below
//and then the second array in the instruction above (shift2[bit i])
//wherever second appears below.
{
  bitWrite(myoutput,first,second);//fill array (myoutput,bit,high or low)
  Serial.print (first);
  Serial.print ("  ");
  Serial.println (second);
  delay(5);
  //    bitWrite(myoutput,1,0);//put WO1 yel  low
  //    bitWrite(myoutput,2,0);//put WO1 yel2 low
  //    bitWrite(myoutput,3,0);//put WO1 grn  low      
}
void output2(int first,int second)//do void output using the first array in the 
//instruction above(shift1[bit i])wherever the variable first is below
//and then the second array in the instruction above (shift2[bit i])
//wherever second appears below.
{
  bitWrite(myoutput2,first,second);//fill array (myoutput,bit,high or low)
  Serial.print (first);
  Serial.print ("  ");
  Serial.println (second);
  delay(5);     
}

Though that would be far far far less efficient that just using a for loop properly.

thank you gents and I will use that if I start to run out of space and can't work out how to get all the lines to work in one function
i.e.

     for (i=0;i<4;i++)
     {
       output(variable1[i],variable2[i]);//WO1 to yel
       output(variable3[i],variable4[i]);//WO2 to red

I am doing that a bit tongue in cheek,
I am trying to work out if it is possible to do something along these lines as a function so please don't berate me for the format above. :slight_smile:

I'm sure if you were to spend less time looking through the forums for things to criticise and more time reading the topics you will understand better.

I am sure that if you managed to learn basic C syntax, your project would proceed more effectively and you would waste less of our time and yours.

This crap

     count;
     {
       output(shift1[i],shift4[i]);//WO1 to yel
       output(shift2[i],shift3[i]);//WO2 to red
     }

void count()
{
     for (i=0;i<4;i++)
}

is not C and not C++ and doesn't even look remotely sensible.

I will use that if I start to run out of space and can't work out how to get all the lines to work in one function

It saves exactly zero space in the finished code as run on the processor. All it saves are lines in the source code and they are not limited.

michinyon:

I'm sure if you were to spend less time looking through the forums for things to criticise and more time reading the topics you will understand better.

I am sure that if you managed to learn basic C syntax, your project would proceed more effectively and you would waste less of our time and yours.

This crap

     count;

{
      output(shift1[i],shift4[i]);//WO1 to yel
      output(shift2[i],shift3[i]);//WO2 to red
    }

void count()
{
    for (i=0;i<4;i++)
}




is not C and not C++ and doesn't even look remotely sensible.

That it is crap, I would agree, and yet it is only a semicolon away from compiling :smiley: (of course, it won't do anything remotely useful)