'Color' was not declared in this scope error

I’m hoping someone is able to assist.

Two codes are included below. The first compiles and runs fine by itself. The second gives me an error in the Boolean line “Color is not declared in this scope”. I don’t see a glaring difference that would be causing the error. I’m guessing I have either placed something in the wrong location while combining or deleted something from the first code that should have remained.

The idea is to read the number of flashes of a red LED and email the results. The serial monitor works flawlessly in the first example and the email portion works fine for the second. It’s when I’m combining them that I get a problem. I’m learning things through trial and retry and I’m ok with that. I know my code is likely not as clean as it could be and I’m ok with that too right now. Not looking for anyone to rewrite the whole thing for me, just help me see the “error” in my ways. :confused:

The sensor is an Adafruit TCS34725. Running on a Nano with first code.
Want to hook the sensor directly to an ESP-8266-01 to compact everything - coding first. Haven’t been able to try it yet.

Thanks for any help.

Code 1 - Count the flashes and print the results to the monitor:

#include <Wire.h>
#include "Adafruit_TCS34725.h"

#define MAX_OFFSET 1000  // the maximum diff you found between readings

typedef struct {  // struct to hold raw color data 
   uint16_t red;
   uint16_t green;
   uint16_t blue;
   uint16_t clear;
} Color;


Color LEDred = {1000,0,0,1000}; //here you enter your minimum recorded values for red in the order of the struct members above

int rcount = 0;
int offcount = 0;

Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X);

void setup() {
  rcount = 0;
  
  Serial.begin(9600);

  if (tcs.begin()) {
    Serial.println("Found sensor");
  } else {
    Serial.println("No TCS34725 found ... check your connections");
    while (1); // halt!
  }
  
}


void loop() {

  uint16_t clear, red, green, blue;

  tcs.setInterrupt(false);      // turn on LED

  delay(1000);  // takes 500ms to read 

  tcs.getRawData(&red, &green, &blue, &clear);
  Serial.print("C:\t"); Serial.print(clear);
  Serial.print("\tR:\t"); Serial.print(red);
  Serial.print("\tG:\t"); Serial.print(green);
  Serial.print("\tB:\t"); Serial.println(blue);


  tcs.setInterrupt(true);  // turn off LED
  if (compare_color (&LEDred, red, green, blue, clear))
  {
    rcount++;
    results();
  }
  else {offcount++;}
}

boolean compare_color(Color * std, uint16_t r, uint16_t g, uint16_t b, uint16_t c)
  {
    if ((r - std->red <= MAX_OFFSET) && (r - std->red >= 0) &&
      (g - std->green <= MAX_OFFSET) && (g - std->green >= 0) &&
      (b - std->blue <= MAX_OFFSET) && (b - std->blue >= 0) &&
      (c - std->clear <= MAX_OFFSET) && (c - std->clear >= 0))
  {
      return true;
  }
      else {
        return false;
           }
  }

void results()
   {
   if ((rcount >= 1)&&(offcount <10))
      { offcount = 0;
        }
              
    else if ((rcount > 1) && (offcount >= 10))
       {  finalMessage ();
       }

    else
       {Serial.print("Rcount = ");
       Serial.print(rcount);
       Serial.print("      Offcount = ");
       Serial.println(offcount);
       Serial.println("Error in calcs");
       delay (5000);
       exit(0);
       }
     
   }

void finalMessage()   
          {          
          Serial.println();
          Serial.print("Done. The Final Count  = ");
          Serial.println(rcount-1);
          delay(5000);
          exit(0);
          }

Code #2 - do the same thing but no monitor - email instead.

#include <Wire.h>
#include "Adafruit_TCS34725.h"
#include <ESP8266WiFi.h>


#define MAX_OFFSET 1000  // the maximum diff you found between readings
#define DHTPIN 1  //GPIO1 (Tx)
#define DHTTYPE  DHT11
#define ONE_WIRE_BUS 3 // GPIO3=Rx

const char* SSID = "MySSID";
const char* PASS = "MySSIDPW";
char server[] = "mail.server.com";

ADC_MODE(ADC_VCC);
WiFiClient client;

typedef struct 
  {  // struct to hold raw color data 
  uint16_t red;
  uint16_t green;
  uint16_t blue;
  uint16_t clear;
  } 
 
Color;

Color LEDred = {1000,0,0,1000}; //here you enter your minimum recorded values for red in the order of the struct members above

int rcount = 0;
int offcount = 0;

Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X);


void setup() 
  {
  //I2C stuff
  Wire.pins(0, 2);
  Wire.begin(0, 2);
  
  rcount = 0;

  if (tcs.begin()) 
    {
      //Serial.println("Found sensor");
    } 
  else 
    {
    //Serial.println("No TCS34725 found ... check your connections");
    while (1); // halt!
    }
  }


void loop() 
  {
  
  uint16_t clear, red, green, blue;

  tcs.setInterrupt(false);      // turn on LED

  delay(1000);  // takes 500ms to read 

  tcs.getRawData(&red, &green, &blue, &clear);
  Serial.print("C:\t"); Serial.print(clear);
  Serial.print("\tR:\t"); Serial.print(red);
  Serial.print("\tG:\t"); Serial.print(green);
  Serial.print("\tB:\t"); Serial.println(blue);


  tcs.setInterrupt(true);  // turn off LED
  if (compare_color (&LEDred, red, green, blue, clear))
    {
    rcount++;
    results();
    }
  else {offcount++;}
 
  }


boolean compare_color(Color * std, uint16_t r, uint16_t g, uint16_t b, uint16_t c)
  {
  if ((r - std->red <= MAX_OFFSET) && (r - std->red >= 0) &&
      (g - std->green <= MAX_OFFSET) && (g - std->green >= 0) &&
      (b - std->blue <= MAX_OFFSET) && (b - std->blue >= 0) &&
      (c - std->clear <= MAX_OFFSET) && (c - std->clear >= 0))
    {
    return true;
    }
  
  else 
    {
    return false;
    }
  }
typedef struct {  // struct to hold raw color data
   uint16_t red;
   uint16_t green;
   uint16_t blue;
   uint16_t clear;
} Color;

and

typedef struct
  {  // struct to hold raw color data
  uint16_t red;
  uint16_t green;
  uint16_t blue;
  uint16_t clear;
  }
 
Color;

Aren't exactly the same, though the white space shouldn't make a difference.

The compiler told you EXACTLY where the problem was, but you elected to keep that information to yourself. Why?

In both of your code, your indenting is atrocious. Put every { on a line BY ITSELF. Put every } on a line BY ITSELF. Use Tools + Auto Format, so you at least look like you know what you are doing.

Thanks for the critique and the reply.

I didn't keep where the error was to myself at all. I stated very clearly in the third sentence it was in the Boolean line of the code. So to be perfectly clear:

boolean compare_color(Color * std, uint16_t r, uint16_t g, uint16_t b, uint16_t c)

That's where it pops with the error.

I tried working through the error on my own and came here for some help in identifying what I'm sure is something simple that I don't see in my code. Even if you choose to slap me around for my formatting, you didn't offer anything in the way of a solution. I thought this community was about helping each other out. It's not like I was posting I didn't know how to plug my serial into the board or select the correct com port. I'm 46 years old and learning this stuff for the first time. Sorry to have bothered you PaulS.

Anyone else be willing to help a guy out who's willing to learn and someday hopefully be of benefit to others?

Arduino's automatic prototype generation strikes again. It doesn't like where you've defined the Color struct. You can either add your own prototype for the 'compare_color()' function:

typedef struct 
  {  // struct to hold raw color data 
  uint16_t red;
  uint16_t green;
  uint16_t blue;
  uint16_t clear;
  } 
 
Color;

boolean compare_color(Color * std, uint16_t r, uint16_t g, uint16_t b, uint16_t c);

Or, move the struct definition to the top of the file.

BTW, @PaulS is correct, your indenting is atrocious. It takes absolutely no effort to type 'ctrl-t' in the IDE to fix that. Otherwise, it looks like you're intentionally trying to make it difficult for people willing to help you for free.

Thank you so much!

I moved the definition to the top and it compiled without error.

Now that I know what the issue was, I can dig into understanding why it happened the way it did and how to avoid it happening in the future.

As for my code structure - I wasn't aware that a format tool existed in the developer. I will certainly use that from now on. I in no way intended to make helping me any more difficult given the fact this is people helping people for free. For that I apologize to all including @PaulS.

I'm going to spend some time researching and looking at code formatting and structure to clean up my codes from hereon out.

Thank you again for your help.