Editing existing sketch

Hi, I downloaded an Arduino sketch from a site on YouTube which was applied to 12 RGB' s through 5 Shift registers. The sketch has a total of 12 effects. What I am trying to do is isolate each effect to make a sketch for each one. So, I have copied the first section of the sketch including the void set up. I then attached all the variables for effect #3. When I try to run the sketch I get an error message stating : ledOff was not declared in this scope.
I am a complete novice with this and any help or pointers would be appreciated
David.
int dataPin = 12;
int enablePin = 11;
int latchPin = 10;
int clockPin = 9;

#define number_of_74hc595s 5
#define numOfRegisterPins number_of_74hc595s * 8
#define numRGBLeds 12

boolean registers[numOfRegisterPins];

int redPin[] = {0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33 };
int greenPin[] = {1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34 };
int bluePin[] = {2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35 };

void setup() {
Serial.begin(9600);

pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(enablePin, OUTPUT);

digitalWrite(enablePin, LOW);
for (int i = numOfRegisterPins - 1; i >= 0; i--) {
registers[i] = LOW;
}
ledOff(0);
}

//////////////////////////////////////////////////////effect_3
void effect_3(int repeatEffect) {
for (int i = 1; i <= repeatEffect; i++) {
for (int i = 0; i <= 11; i++) {
registersWrite(redPin[i], HIGH);
delay(100);
} ledOff(100);
for (int i = 11; i >= 0; i--) {
registersWrite(redPin[i], HIGH);
registersWrite(bluePin[i], HIGH);
delay(100);
} ledOffR(100);

for (int i = 0; i <= 11; i++) {
  registersWrite(greenPin[i], HIGH);
  delay(100);
} ledOff(100);

for (int i = 11; i >= 0; i--) {
  registersWrite(redPin[i], HIGH);
  registersWrite(greenPin[i], HIGH);
  delay(100);
} ledOffR(100);

for (int i = 0; i <= 11; i++) {
  registersWrite(bluePin[i], HIGH);
  delay(100);
} ledOff(100);

for (int i = 11; i >= 0; i--) {
  registersWrite(bluePin[i], HIGH);
  registersWrite(greenPin[i], HIGH);
  delay(100);
} ledOffR(100);

for (int i = 0; i <= 11; i++) {
  registersWrite(bluePin[i], HIGH);
  registersWrite(redPin[i], HIGH);
  registersWrite(greenPin[i], HIGH);
  delay(100);
} ledOff(100);

}
}

Firstly, can you format your code correctly? It's almost impossible to see what's going in its current state.

It looks like ledOff() is a function that isn't defined anywhere in your code. Maybe more important is that there isn't even a void loop(). Possibly you posted your code incorrectly here? Can you link the site/course you're following for this project?

Hi Seanboe,
How do I format code?

https://www.youtube.com/watch?v=c5dpqXnZFqw&ab_channel=MrProjectsoPedia

Thanks for your help.

David.

When you reply, there should be buttons at the top of the text box. One of them should look like this: </>. When you click it, it should auto paste the markdown formatting for code blocks. Just replace "type or paste code here" with your code.

Assuming you copied the code from this GitHub link, it looks like you didn't copy it all, which would explain the error. Maybe try recopying the code and running it again?

You did not copy enough of the code. You need to copy the loop function (void loop, although never call it this as it is wrong) as well as the ledOff function into your code. There are probably more functions that are used that are missing as well as the compiler stops at the first big mistake. These are likely to be functions called in the loop function for the other effects, so those need to be removed from the loop function if you want to isolate a section of code.

To format code, assuming you have it in the Arduino IDE, select it all and select Auto format. Then highlight it all again and choose copy for forum. Then simply paste it into your post.

However from the questions you are asking it looks as your skill level at the moment is not up to what you want to do. It is like asking how to remove the drive shaft of a car when you don’t know how to even open the door.

I would recommend that you start off looking at the examples in the IDE, and trying to read them and follow what they do. You will learn the structure of code from this. Then try and see if you can change the behaviour of the code by making simple changes, and get the feel of the language.

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

#define number_of_74hc595s 5
#define numOfRegisterPins number_of_74hc595s * 8
#define numRGBLeds 12

boolean registers[numOfRegisterPins];

int redPin[]   = {0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33 };
int greenPin[] = {1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34 };
int bluePin[]  = {2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35 };

void setup() {
  Serial.begin(9600);

  pinMode(dataPin,  OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);

  digitalWrite(enablePin, LOW);
  for (int i = numOfRegisterPins - 1; i >=  0; i--) {
    registers[i] = LOW;
  }
  ledOff(0);
}
void loop() {
  effect_3(1);
  ledOff(0);
}
//////////////////////////////////////////////////////effect_3
void effect_3(int repeatEffect) {
  for (int i = 1; i <= repeatEffect; i++) {
    for (int i = 0; i <= 11; i++) {
      registersWrite(redPin[i], HIGH);
      delay(100);
    } ledOff(30);
    for (int i = 11; i >= 0; i--) {
      registersWrite(redPin[i], HIGH);
      registersWrite(bluePin[i], HIGH);
      delay(100);
    } ledOffR(30);

    for (int i = 0; i <= 11; i++) {
      registersWrite(greenPin[i], HIGH);
      delay(100);
    } ledOff(30);

    for (int i = 11; i >= 0; i--) {
      registersWrite(redPin[i], HIGH);
      registersWrite(greenPin[i], HIGH);
      delay(100);
    } ledOffR(30);

    for (int i = 0; i <= 11; i++) {
      registersWrite(bluePin[i], HIGH);
      delay(100);
    } ledOff(30);

    for (int i = 11; i >= 0; i--) {
      registersWrite(bluePin[i], HIGH);
      registersWrite(greenPin[i], HIGH);
      delay(100);
    } ledOffR(30);

    for (int i = 0; i <= 11; i++) {
      registersWrite(bluePin[i], HIGH);
      registersWrite(redPin[i], HIGH);
      registersWrite(greenPin[i], HIGH);
      delay(100);
    } ledOff(30);
  }
}

Hi Guys. Thanks for your input. I have formatted and posted the sketch as Grumpy Mike suggested. Interested to hear your comments.
FYI . The sketch I downloaded for the RGB / Shift Register project worked fine.

Thanks,

David.

Take a look into this tutorial:

Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

best regards Stefan

It looks like you will get exactly the same error because you do not have an ledOff function.

When posting any code, say what happened and what you expect / hope would happen.

I also can’t see this function defined.

as a general hint:

if you have modified your code let it compile again.
The compiler will write either a message that everything compiles
or write errors.

This will guide you to ask questions or will give you the hint what to correct.

best regards Stefan

Hi Stefan,
I took your advice on board and started your suggested tutorial. I completed the Knight Rider project which worked well. I have attached the sketch {Hopefully it is formatted properly}. I have a question if you don't mind. Inside the second set of parenthesis it states:
(int i = 2; i < 10; i++)
The 2 and the 10 I understand. How are the characters i and i++ selected , and what do they mean?

Cheers Mate,

Davy

void setup() {
  // set up pins 2 to 9 as outputs
  for (int i = 2; i < 10; i++) {
    pinMode(i, OUTPUT);
  }
}

// function to switch all LEDs off
void allLEDsOff(void)
{
  for (int i = 2; i < 10; i++) {
    digitalWrite(i, LOW);
  }
}

void loop() {
  // move on LED to the right
  for (int i = 2; i < 9; i++) {
    allLEDsOff();
    digitalWrite(i, HIGH);
    delay(200);
  }
  // move on LED to the left
  for (int i = 9; i > 2; i--) {
    allLEDsOff();
    digitalWrite(i, HIGH);
    delay(200);
  }
}

That’s a great start, so it means your hardware is working. (don’t mess with that !)

Hi Davy,

there are several groups of elements that belong together

int i = 2
define a variable named "i" of variabletype integer assign the variabe the value "2"

i < 10
as long as the condition:
value of variable i is smaller than 10
continue to do "i++"

i++
counting up variable named "i" by one

so the result is
start counting up variable "i" at value 2
go on 3,4,5,6,7,8,9

in the tutorial there is a demo-code that makes this visible in the serial monitor

best regards Stefan

Hi Stefan,
Thanks a lot Mate. That's explained it for me.
Cheers,

Davy.

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