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. ![]()
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;
}
}