I am in the process of developing a class for using the DFROBOT Color Sensor. I had everything working within the "loop" method of the Arduino .INO file. I then converted this to a class due to the the need of 6 sensors. I have tried for over two hours now trying to get the class to work, what a do know, I can output string literals to the serial directly from within methods of the class. Serial prints of my variables returns no result. When I call a method from the Sketch, i get no return.
#include <string.h>
#include <Sensor.h>
#define aS2 49
#define aS3 47
#define aSensorOut 45
#define bS2 33
#define bS3 31
#define bSensorOut 29
#define enable 23
//Make instance of sensor class with hardware addresses
//-1 means this pin is not used
Sensor sense1(aS2,aS3,aSensorOut,-1);
Sensor sense2(bS2,bS3,bSensorOut,enable);
void setup() {
Serial.begin(9600);
// put your setup code here, to run once:
sense1.ambient();
sense2.ambient();
}
void loop()
{
// put your main code here, to run repeatedly:
Serial.println(tapeColor(sense1.read()) + "^" + blockColor(sense1.read()));
}
String tapeColor(int g)
{
if(g > 2000)
{
return "x";
}
else
{
return "c";
}
}
String blockColor(int g)
{
if(g < 650)
{
return "y";
}
else if(g > 1180 and g < 1210)
{
return "r";
}
else if(g > 880 and g < 940)
{
return "b";
}
else if(g > 1230 and g < 1280)
{
return "g";
}
else
{
return "x";
}
}
Class Header (Sensor.h):
#ifndef H_Sensor
#define H_Sensor
#include <Arduino.h>
class Sensor
{
public:
Sensor(int s2, int s3, int out, int enable);
int read();
int readR();
int readG();
int readB();
int readK();
int ambient();
private:
void state(int s);
void enable();
void disable();
int _s2;
int _s3;
int _out;
int _gain;
int _enable;
const int _r = 0;
const int _b = 1;
const int _k = 2;
const int _g = 3;
};
#endif
Class Code (Sensor.cpp):
#include <Arduino.h>
#include <Sensor.h>
Sensor::Sensor(int s2, int s3, int out, int enable)
{
_s2 = s2;
_s3 = s3;
_out = out;
_enable = enable;
pinMode(_s2, OUTPUT);
pinMode(_s3, OUTPUT);
pinMode(_enable, OUTPUT);
pinMode(_out, INPUT);
}
int Sensor::readR()
{
int val;
enable();
state(_r);
val = pulseIn(_out,LOW);
disable();
return val * _gain;
}
int Sensor::readB()
{
int val;
enable();
state(_b);
val = pulseIn(_out,LOW);
disable();
return val * _gain;
}
int Sensor::readG()
{
int val;
enable();
state(_g);
val = pulseIn(_out,LOW);
disable();
return val * _gain;
}
int Sensor::readK()
{
int val;
enable();
state(_k);
val = pulseIn(_out,LOW);
disable();
if(_gain == 0)
{
return val;
}
else
{
return val * _gain;
}
}
int Sensor::read()
{
return (readR() + readG() + readB())/3;
}
int Sensor::ambient()
{
_gain = 0;
_gain = (10/readK())/3;
}
void Sensor::state(int s)
{
switch(s)
{
case 0:
digitalWrite(_s2, LOW);
digitalWrite(_s3, LOW);
break;
case 1:
digitalWrite(_s2, LOW);
digitalWrite(_s3, HIGH);
break;
case 2:
digitalWrite(_s2, HIGH);
digitalWrite(_s3, LOW);
break;
case 3:
digitalWrite(_s2, HIGH);
digitalWrite(_s3, HIGH);
break;
}
}
void Sensor::enable()
{
if(_enable != -1)
{
digitalWrite(_enable,LOW);
}
}
void Sensor::disable()
{
if(_enable != -1)
{
digitalWrite(_enable,HIGH);
}
}
Also, try printing the return value from you sense1.read() calls directly rather than using them in the calls to tapeColor() and blockColor() just to make sure they look reasonable.
gfvalvo:
Also, try printing the return value from you sense1.read() calls directly rather than using them in the calls to tapeColor() and blockColor() just to make sure they look reasonable.
^ I have tried this in my debugging efforts. There was nothing printed to the serial monitor. I even tried calling the components ('readR()', 'readG()', and 'readB()') individually, with no return.
gfvalvo:
There may be other problems. But, one is that your 'ambient()' function promises to return an 'int' but does not.
^ This is a simple over sight on my part. The 'ambient()' function should have no return.