Hello, I have been attempting to make a class in Arduino using A classy solution | Multi-tasking the Arduino - Part 1 | Adafruit Learning System and have encountered a problem. The code throws an Undeclared Identifier error, even though they are in fact declared as far as I can tell(I know I must be missing something, but i'm not sure what is is)
note:I have also tried moving the class to below //definitions, but that made even more errors
Here is the Code
class ColorSensor
{
//variable setup
int rColorStrength; //gives values between 0, and 255
int gColorStrength; // ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
int bColorStrength; // ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
long OnTime; // milliseconds of on-time
long OffTime; // milliseconds of off-time
unsigned int pulseWidth; //can deal with values higher than int
unsigned long previousMillis; // will store last time "void CSS()" was run
public:
ColorSensor(int S2, int S3, int outPin, unsigned long serialPort, long on, long off) // :confused:
{ // :confused:
Serial.begin(serialPort);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
pinMode(outPin, INPUT);
OnTime = on;
OffTime = off;
previousMillis = 0;
}
void Update()
{
// check to see if it's time to change the state of the LED
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= OnTime)
{
previousMillis = currentMillis; // Remember the time
}
else if(currentMillis - previousMillis >= OffTime)
{
previousMillis = currentMillis; // Remember the time
CSS(); //run loop
}
}
void CSS() //Color Sensor Serial
{
//to find red
digitalWrite(S2, LOW);
digitalWrite(S3, LOW);
pulseWidth - pulseIn(outPin, LOW);
rColorStrength = pulseWidth/400. -1;
rColorStrength = (255- rColorStrength);
//to find Green
digitalWrite(S2, HIGH);
digitalWrite(S3, HIGH);
pulseWidth - pulseIn(outPin, LOW);
gColorStrength = pulseWidth/400. -1;
gColorStrength = (255- gColorStrength);
//to find Blue
digitalWrite(S2, LOW);
digitalWrite(S3, HIGH);
pulseWidth - pulseIn(outPin, LOW);
bColorStrength = pulseWidth/400. -1;
bColorStrength = (255- bColorStrength);
Serial.print(rColorStrength);
Serial.print(" , ");
Serial.print(gColorStrength);
Serial.print(" , ");
Serial.print(bColorStrength);
Serial.print("");
}
};
//Definitions
int//Proximity Sensor Definitions
SoIn=1, //Sonar input
SoNi=2; //Sonar output
ColorSensor CSS1(7, 8, 4, 115200, 100, 500); //ColorSensor // :confused:
//S2, S3, outPin, serialPort, on, off // :confused:
//Classes -> Must be located after Definitions
//Setup
void setup()
{
Serial.begin(57600);
pinMode(SoIn, INPUT); pinMode(SoNi, OUTPUT); //all things sonar
}
//-------------------------------The Plan-------------------------------------//
//0)ignore the first alert from sonar caused by leaving the ramp
//1)go forward until wall
// a)check left |===================|
// i)if wall present, LW==0 || The Plan ||
// ii)if wall absent, LW==1 |===================|
// b)check right
// i)if wall present, RW==0
// ii)if wall absent, RW==1
// c)decide weather to check color sensor
// i)L==2 (after 3rd turn has occured only)
// 1)check color sensor and report result to variable "COLOR"
// ii)L><2
// 1)ignore color sensor(this leaves "COLOR" equal to 0)
// d)decide what direction to turn
// i)RW==LW && COLOR==0, freak out(this isn supposed to be able to happen)
// ii)RW>LW && COLOR==0, turn robot right 90 degrees and L++
// iii)RW<LW && COLOR==0, turn robot left 90 degrees and R++
// iv)COLOR==BLUE, execute actions for blue
// v) COLOR==RED, execute actions for red
//-----------------------------------------------------------------------------//
void loop()
{
CSS1.Update();
}
//==================//
// all color sensor //
//==================//
void CoMa() //main color sensor loop
{
}
//==================//
// all sonar //
//==================//
void SoMa() //main sonar loop
{
}
//===================//
//all motor Functions//
//===================//
void RoRi() //Robot right
{
}
void RoLe() //Robot left
{
}
void RoFo() //robot forward
{
}
void LiUp() //Lift up
{
}
void LiDo() //Lift down
{
}
void LoRi() //look right
{
}
void LoLe() //look left
{
}
void LoFo() //look forward
{
}
========================
Here is the error from the Arduino IDE:
/Users/derekgilhousen/Documents/Arduino/sketch_mar31b/sketch_mar31b.ino: In member function 'void ColorSensor::CSS()':
sketch_mar31b:45: error: 'S2' was not declared in this scope
digitalWrite(S2, LOW);
^
sketch_mar31b:46: error: 'S3' was not declared in this scope
digitalWrite(S3, LOW);
^
sketch_mar31b:47: error: 'outPin' was not declared in this scope
pulseWidth - pulseIn(outPin, LOW);
^
exit status 1
'S2' was not declared in this scope
========================
Here is the error from code bender:
(sketch file) DGApproved Maze follower main code.ino:45:18: error: use of undeclared identifier 'S2'
digitalWrite(S2, LOW);
^
(sketch file) DGApproved Maze follower main code.ino:46:18: error: use of undeclared identifier 'S3'
digitalWrite(S3, LOW);
^
(sketch file) DGApproved Maze follower main code.ino:47:26: error: use of undeclared identifier 'outPin'
pulseWidth - pulseIn(outPin, LOW);
^
(sketch file) DGApproved Maze follower main code.ino:51:18: error: use of undeclared identifier 'S2'
digitalWrite(S2, HIGH);
^
(sketch file) DGApproved Maze follower main code.ino:52:18: error: use of undeclared identifier 'S3'
digitalWrite(S3, HIGH);
^
(sketch file) DGApproved Maze follower main code.ino:53:26: error: use of undeclared identifier 'outPin'
pulseWidth - pulseIn(outPin, LOW);
^
(sketch file) DGApproved Maze follower main code.ino:57:18: error: use of undeclared identifier 'S2'
digitalWrite(S2, LOW);
^
(sketch file) DGApproved Maze follower main code.ino:58:18: error: use of undeclared identifier 'S3'
digitalWrite(S3, HIGH);
^
(sketch file) DGApproved Maze follower main code.ino:59:26: error: use of undeclared identifier 'outPin'
pulseWidth - pulseIn(outPin, LOW);
^
9 errors generated.