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?
//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
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.
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().
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.
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!