Ending a for loop - SOLVED

Hi,
I am basically making a shift register output to one LED at a time. I want it to light LED0, delay, turn LED0 off, move to next LED and keep repeating until the it get to the last one (32nd LED).

The code below does it but it resets from LED0 and keeps looping…as you would expect in the loop!

I want it to go from LED0 through to LED32 and stop. I put the loop in the setup() and left the loop() empty from reading other thread on the forum. The compiler gives me the following error: “A function definition is not allowed here before “(” token”.

//Tutorial https://www.youtube.com/watch?v=cAT07gy4DII
//This program lights up led's 1-32 one by one then stops at number 32.
#include <MultiShiftRegister.h>

int latchPin = 10;
int clockPin = 11;
int dataPin = 12;

MultiShiftRegister msr (4, latchPin, clockPin, dataPin);//Put the total number of latches at the start of the bracket.

void setup() {
  Serial.begin(9600); // open the serial port at 9600 bps:
  pinMode (latchPin, OUTPUT);
  pinMode (clockPin, OUTPUT);
  pinMode (dataPin, OUTPUT);
  msr.shift();



}
void loop() {

  for (int pin = 0; pin <= 31; pin += 1) //(pin +=1) = (pin = pin+1)
  {
    //msr.set_shift(pin);//To set eg '0 'individually, do msr.set(0); msr.shift(); to clear it: msr.clear_shift(0);
    msr.set(pin); //Select LED to turn on
    msr.shift();  //Shift out on register
    delay(250);
    msr.clear_shift(pin); //Turn the selected LED off
    Serial.print (pin);
    delay(250);

  }

}

Please could you give me some hints on how to solve this?

Thanks

Post the code that gave you the compiler error. Post the complete compiler error, not your paraphrased version of it.

//Tutorial https://www.youtube.com/watch?v=cAT07gy4DII
//This program lights up led's 1-32 one by one then stops at number 32.
#include <MultiShiftRegister.h>

int latchPin = 10;
int clockPin = 11;
int dataPin = 12;

MultiShiftRegister msr (4, latchPin, clockPin, dataPin);//Put the total number of latches at the start of the bracket.

void setup() {
  Serial.begin(9600); // open the serial port at 9600 bps:
  pinMode (latchPin, OUTPUT);
  pinMode (clockPin, OUTPUT);
  pinMode (dataPin, OUTPUT);
  msr.shift();
  
  for (int pin = 0; pin <= 31; pin += 1) //(pin +=1) = (pin = pin+1)
  {
    //msr.set_shift(pin);//To set eg '0 'individually, do msr.set(0); msr.shift(); to clear it: msr.clear_shift(0);
    msr.set(pin); //Select LED to turn on
    msr.shift();  //Shift out on register
    delay(250);
    msr.clear_shift(pin); //Turn the selected LED off
    Serial.print (pin);
    delay(250);



}
void loop() {


  }

}

Compiler error: In function ‘void setup()’:
TCM_Harness_Checker:31:13: error: a function-definition is not allowed here before ‘{’ token
void loop() {
^
exit status 1
a function-definition is not allowed here before ‘{’ token

Every '{' must have a matching '}'. You have an imbalance.

I cannot see the { } imbalance.

The setup() has two, the FOR loop has two and the VOID loop has two. It's easier to see in my sketch and they appear to be matching when I highlight them.

Thanks.

So put down a mark for every "{" and "(" on a piece of paper. Cross off a mark every time you encounter a "}" or a ")".

The missing "}" is quite obvious.

I can’t see the closing brace for setup.

Trust me, there is an imbalance. Keep looking.

Right, they are balanced. But your loop() function (it's called a function, not a "void") is defined inside the setup() function. You can't define a function inside of another function. You need to properly end setup() before starting loop().

Thanks all, I moved curly bracket for the setup() and it works a treat!

Right, they are balanced.

Doh!

Here’s a handy tip: Use the IDE’s auto-format tool (ctrl-T) on your code.

If any function’s header does not start in the leftmost column, then you’ve gone wrong.

Here it is, applied to the code from reply #2

//Tutorial https://www.youtube.com/watch?v=cAT07gy4DII
//This program lights up led's 1-32 one by one then stops at number 32.
#include <MultiShiftRegister.h>

int latchPin = 10;
int clockPin = 11;
int dataPin = 12;

MultiShiftRegister msr (4, latchPin, clockPin, dataPin);//Put the total number of latches at the start of the bracket.

void setup() {
  Serial.begin(9600); // open the serial port at 9600 bps:
  pinMode (latchPin, OUTPUT);
  pinMode (clockPin, OUTPUT);
  pinMode (dataPin, OUTPUT);
  msr.shift();

  for (int pin = 0; pin <= 31; pin += 1) //(pin +=1) = (pin = pin+1)
  {
    //msr.set_shift(pin);//To set eg '0 'individually, do msr.set(0); msr.shift(); to clear it: msr.clear_shift(0);
    msr.set(pin); //Select LED to turn on
    msr.shift();  //Shift out on register
    delay(250);
    msr.clear_shift(pin); //Turn the selected LED off
    Serial.print (pin);
    delay(250);



  }
  void loop() {


  }

}

Note how “void loop () {” is indented - this should never happen.

Good tip thanks.

I think the confusions was that I was looking for a missing curly bracket. I should have looked at where the
void setup() curly brackets ended as people hinted!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.