skittle sorter program "if then statement help"

I AM CREATING A SKITTLE SORTING MACHINE FOR SCHOOL AND AM HAVING SOME TROUBLE COMPLETING THE PROGRAM.

I AM VERY NEW TO ARDUINO, SO IT SHOULDN'T BE OVERLY DIFFICULT.

I AM USING THE FOLLOWING PROGRAM TO SENSE COLORS... AND IT WORKS.

MY QUESTION IS HOW TO I IMPLEMENT AN "IF THEN" STATEMENT TO SAY 'IF THE COLORS ARE WITHIN THIS RANGE, CALL IT THIS COLOR, AND MOVE A SERVO TO THIS LOCATION".

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

// Declare sensor object
SFE_ISL29125 RGB_sensor;

void setup()
{
 // Initialize serial communication
 Serial.begin(115200);

 // Initialize the ISL29125 with simple configuration so it starts sampling
 if (RGB_sensor.init())
 {
   Serial.println("Sensor Initialization Successful\n\r");
 }
}

// Read sensor values for each color and print them to serial monitor
void loop()
{
 // Read sensor values (16 bit integers)
 unsigned int red = RGB_sensor.readRed();
 unsigned int green = RGB_sensor.readGreen();
 unsigned int blue = RGB_sensor.readBlue();
 
 // Print out readings, change HEX to DEC if you prefer decimal output
 Serial.print("Red: "); Serial.println(red,DEC);
 Serial.print("Green: "); Serial.println(green,DEC);
 Serial.print("Blue: "); Serial.println(blue,DEC);
 Serial.println();
 delay(2000);
}

It is an Arduino Uno

if ( X >= A && X <= B )
{
    // X is in range { A, B }
}

nburke612:
IT IS AN ARDUINO UNO BTW

Stop shouting at us.

How to use this forum

Please edit your post, select the code, and put it between [code] ... [/code] tags.

You can do that by hitting the "Code" button above the posting area (It looks like a scroll with < > inside it).

I AM VERY NEW TO ARDUINO, SO IT SHOULDN'T BE OVERLY DIFFICULT.

The ISL29125 is and RGB light sensor, it reads three different colors. As a default it will base itself on that and tell you how much Red it can see, how much Green it can see or how much Blue it can see.
This is three different colors. To use this you can call readRed() to get how much red it can see.
In your case you would call RGB_sensor.readRed() to do this.

Unforunately there is no easy way to combine this into one fluent system (AFAIK)
You will basically need to figure out how much red, green, and blue there is in the different skittles.
Read of all theese three colors and then compare it to your reference values.

Ok. I'm trying to follow you guys.

Below is the code I have. But I keep getting the error message:

test.ino: In function 'void setup()':
test.ino:32:3: error: 'myservo' was not declared in this scope
test.ino: In function 'void loop()':
test.ino:40:3: error: 'myservo' was not declared in this scope
Error compiling.

I don't understand why it isn't declaring my servo.
How can I fix this problem? and is my IF THEN code correct?

All help is appreciated.

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

// Declare sensor object
SFE_ISL29125 RGB_sensor;

void setup()
{
  myservo.attach(9);
}


void loop()
{ 
if ( RGB_sensor.readRed() >= 800 && RGB_sensor.readRed() <= 900 )
{
  myservo.write(45); //SERVO TO 45 DEGREES
  delay(2000);    // red is in range { 800, 900 }
}
}
[code/]

I don't remember declaring it like that, but it works... So thank you very much.

Any ideas on how I could say something like...

{
IF red between A&B
AND
IF blue between A&B
AND
IF green between A&B
THEN DO ACTION 1
}
OR
{
IF red between C&D
AND
IF blue between C&D
AND
IF green between C&D
THEN DO ACTION 2
}

Right now, my sensor is only reading the value for red light... Which is OK, but I'd like to get a more accurate reading by incorporating the other colors.

#include <Wire.h>
#include "SFE_ISL29125.h"
#include <Servo.h>

// Declare sensor object
SFE_ISL29125 RGB_sensor;
//Declare servo
Servo myservo;  // create servo object to control a servo

void setup()
{
  myservo.attach(9);
    // Initialize serial communication
  Serial.begin(115200);

  // Initialize the ISL29125 with simple configuration so it starts sampling
  if (RGB_sensor.init())
  {
    Serial.println("Sensor Initialization Successful\n\r");
  }
}


void loop()
{ 
    // Read sensor values (16 bit integers)
  unsigned int red = RGB_sensor.readRed();
  unsigned int green = RGB_sensor.readGreen();
  unsigned int blue = RGB_sensor.readBlue();
  
  // Print out readings, change HEX to DEC if you prefer decimal output
  Serial.print("Red: "); Serial.println(red,DEC);
  Serial.print("Green: "); Serial.println(green,DEC);
  Serial.print("Blue: "); Serial.println(blue,DEC);
  Serial.println();
  delay(2000);
  
if ( RGB_sensor.readRed() >= 300 && RGB_sensor.readRed() <= 400 )
{
  myservo.write(180); //SERVO TO 180 DEGREES
  delay(2000);    // red is in range { 300, 400 }
}

if ( RGB_sensor.readRed() >= 1800 && RGB_sensor.readRed() <= 1900 )
{
  myservo.write(0); //SERVO TO 0 DEGREES
  delay(2000);    // red is in range { 1800, 1900 }
}
}
[code/]

nburke612:
I don't remember declaring it like that, but it works... So thank you very much.

Any ideas on how I could say something like...

{
IF red between A&B
AND
IF blue between A&B
AND
IF green between A&B
THEN DO ACTION 1
}
OR
{
IF red between C&D
AND
IF blue between C&D
AND
IF green between C&D
THEN DO ACTION 2
}

Right now, my sensor is only reading the value for red light... Which is OK, but I'd like to get a more accurate reading by incorporating the other colors.

if (((red>A1)&&(red<B1))&&((blue>A2)&&(blue<B2))&&((green>A3)&&(green<B3)))
{
    // Then you presumably want to do stuff
}