Code Stacking Issues (Educator needs help)

I have a student in one of my classes that is using an Arduino for project and he is soliciting my help. Problem is it is beyond my expertise. He has a LED strip that he is trying to control via a LDR. He found some code online and has really just stacked the two codes together.

#include <Adafruit_NeoPixel.h>
#define PIN 6
#define NUM_LEDS 34
const int ledPin = 13;   //the number of the LED pin
const int ldrPin = A0;  //the number of the LDR pin

// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);  //initialize the LED pin as an output
  pinMode(ldrPin, INPUT);   //initialize the LDR pin as an input
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}

// *** REPLACE FROM HERE ***
void loop() {
  int ldrStatus = analogRead(ldrPin);   //read the status of the LDR value

  //check if the LDR status is <= 900
  //if it is, the LED is HIGH

   if (ldrStatus <=900) {

    strip.begin();               //turn LED on
    Serial.println("LDR is DARK, LED is ON");
    
   }
  else {

    strip.show();          
  }
  
 RGBLoop();
}

void RGBLoop(){
  for(int j = 0; j < 3; j++ ) {
    // Fade IN
    for(int k = 0; k < 256; k++) {
      switch(j) {
        case 0: setAll(k,0,0); break;
        case 1: setAll(0,k,0); break;
        case 2: setAll(0,0,k); break;
      }
      showStrip();
      delay(3);
    }
    // Fade OUT
    for(int k = 255; k >= 0; k--) {
      switch(j) {
        case 0: setAll(k,0,0); break;
        case 1: setAll(0,k,0); break;
        case 2: setAll(0,0,k); break;
      }
      showStrip();
      delay(3);
    }
  }
}
// *** REPLACE TO HERE ***

void showStrip() }{
 #ifdef ADAFRUIT_NEOPIXEL_H
   // NeoPixel
   strip.show();
 #endif
 #ifndef ADAFRUIT_NEOPIXEL_H
   // FastLED
   FastLED.show();
 #endif
}
{

void setPixel(int Pixel, byte red, byte green, byte blue) }{
 #ifdef ADAFRUIT_NEOPIXEL_H
   // NeoPixel
   strip.setPixelColor(Pixel, strip.Color(red, green, blue));
 #endif
 #ifndef ADAFRUIT_NEOPIXEL_H
   // FastLED
   leds[Pixel].r = red;
   leds[Pixel].g = green;
   leds[Pixel].b = blue;
 #endif

void setAll(byte red, byte green, byte blue) }
{
  for(int i = 0; i < NUM_LEDS; i++ ) {
    setPixel(i, red, green, blue);
  }
  showStrip();
}
}

The error we keep getting is 'setAll' was not declared in this scope. Anyone out there willing to help out a desperate 7th grade teacher?

Error.txt (8.06 KB)

Your student will just need to go trough the code line by line and understand what he is doing. The braces are all over the place and he will need to remove some of them.

This page shows you how each function needs to look like.

Hint for each open brace there is a closing brace. Try to identify which belong together. If you mark one with the cursor, the editor will tell you which one it thinks belongs to it. See if you agree. :slight_smile:

The error message says you are calling a function but the compiler can not "see" it. The function is there but the wrong curly braces make the function inaccessible.

Additionaly Go to File -> Preferences -> Settings and Set Compiler Warnings to ALL. This will print a lot of "orange text" during compilation which will help find issues.

Right click, 'Auto Format' is a friend

#include <Adafruit_NeoPixel.h>
#define PIN 6
#define NUM_LEDS 34
const int ledPin = 13;   //the number of the LED pin
const int ldrPin = A0;  //the number of the LDR pin

// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);  //initialize the LED pin as an output
  pinMode(ldrPin, INPUT);   //initialize the LDR pin as an input
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}

// *** REPLACE FROM HERE ***
void loop() {
  int ldrStatus = analogRead(ldrPin);   //read the status of the LDR value

  //check if the LDR status is <= 900
  //if it is, the LED is HIGH

  if (ldrStatus <= 900) {

    strip.begin();               //turn LED on
    Serial.println("LDR is DARK, LED is ON");

  }
  else {

    strip.show();
  }

  RGBLoop();
}

void RGBLoop() {
  for (int j = 0; j < 3; j++ ) {
    // Fade IN
    for (int k = 0; k < 256; k++) {
      switch (j) {
        case 0: setAll(k, 0, 0); break;
        case 1: setAll(0, k, 0); break;
        case 2: setAll(0, 0, k); break;
      }
      showStrip();
      delay(3);
    }
    // Fade OUT
    for (int k = 255; k >= 0; k--) {
      switch (j) {
        case 0: setAll(k, 0, 0); break;
        case 1: setAll(0, k, 0); break;
        case 2: setAll(0, 0, k); break;
      }
      showStrip();
      delay(3);
    }
  }
}
// *** REPLACE TO HERE ***

void showStrip()
{
#ifdef ADAFRUIT_NEOPIXEL_H
  // NeoPixel
  strip.show();
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
  // FastLED
  FastLED.show();
#endif
}


void setPixel(int Pixel, byte red, byte green, byte blue)
{
#ifdef ADAFRUIT_NEOPIXEL_H
  // NeoPixel
  strip.setPixelColor(Pixel, strip.Color(red, green, blue));
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
  // FastLED
  leds[Pixel].r = red;
  leds[Pixel].g = green;
  leds[Pixel].b = blue;
#endif
}
void setAll(byte red, byte green, byte blue)
{
  for (int i = 0; i < NUM_LEDS; i++ ) {
    setPixel(i, red, green, blue);
  }
  showStrip();
}

You code after finding the formatting errors. You might go through this code and yours to see the differences. I have not ran your code so I do not know if it does what you want.

One thing that really helps me to make sure that the curly brackets are right is to put every { and every } on its own line. Doing that and using autoformat will often make mismatched or missing brackets stand out.

For example:

if(a == b)
{
   // do something
}
else
{
   // do something else
}

One thing that really helps me to make sure that the curly brackets are right is to put every { and every } on its own line. Doing that and using autoformat will often make mismatched or missing brackets stand out.

You can set up Auto Format to put the { } on their own line which is even more convenient.

Create a file named formatter.conf in the same folder as preferences.txt and you can change the formatting preferences

Mine is as follows :

# This configuration file contains a selection of the available options provided by the formatting tool "Artistic Style"
# http://astyle.sourceforge.net/astyle.html
#
# If you wish to change them, don't edit this file.
# Instead, copy it in the same folder of file "preferences.txt" and modify the copy. This way, you won't lose your custom formatter settings when upgrading the IDE
# If you don't know where file preferences.txt is stored, open the IDE, File -> Preferences and you'll find a link

# 2 spaces indentation
indent=spaces=2

# also indent macros
indent-preprocessor

# indent classes, switches (and cases), comments starting at column 1
indent-classes
indent-switches
indent-cases
indent-col1-comments

# put a space around operators
 pad-oper

# put a space after if/for/while
 pad-header

# Move opening brackets onto new line
 --style=allman --style=bsd --style=break -A1

# delete empty lines in functions
 --delete-empty-lines 

# Insert space padding around operators. 
 --pad-oper

groundFungus:
One thing that really helps me to make sure that the curly brackets are right is to put every { and every } on its own line. Doing that and using autoformat will often make mismatched or missing brackets stand out.

For example:

if(a == b)

{
  // do something
}
else
{
  // do something else
}

+1. I also generally put a comment on a closing brace informing the reader what the brace is closing:

void loop( void )
{
    if(a == b)
    {
        // do something

    }//if
    else
    {
        // do something else

    }//else

}//loop

Hi UKHeliBob,

UKHeliBob:
Create a file named formatter.conf in the same folder as preferences.txt and you can change the formatting preferences

nice, I've just modified mine now, thanks

Peter