Hello all.
I am working on a color sorting project similar to the skittles sorting machine. I have the code written, and It compiles. My question is this: The code is very ugly as is, and I would like to make it look better. Should I put the color reading in a function, or should I make it in a library? If I made it into a function, how would I return the data that is returned originally?
//These are the Libraries that MUST be included
#include <Wire.h>
#include "Adafruit_TCS34725.h"
#include <Servo.h>
#include <Stepper.h>
//These are the pin definitions
#define redpin 9
#define greenpin 10
#define bluepin 11
#define commonAnode true
//These are the positions
int posMetal = 45;
int posPlastic = 90;
int posWood = 135;
//Servo and Stepper
Stepper rotStep = Stepper(32, 4, 5, 6, 7);
Servo posServo;
//This is the Color sensor setup
byte gammatable[256];
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_2_4MS, TCS34725_GAIN_4X);
void setup() {
Serial.begin(9600);
Serial.println("Recycling Sorting Machine!");
//This is the setup for the color sensor
if (tcs.begin()) {
Serial.println("Found Sensor");
} else {
Serial.println("No sensor found...Check your connections");
while (1);
}
//This sets up the RGB LED outputs.
pinMode(redpin, OUTPUT);
pinMode(greenpin, OUTPUT);
pinMode(bluepin, OUTPUT);
//Speet for stepper motor
rotStep.setSpeed(20);
//Setup for servo
posServo.attach(3);
//This is the gamma table
for (int i=0; i<256; i++) {
float x = i;
x /= 255;
x = pow(x, 2.5);
x *= 255;
if (commonAnode) {
gammatable[i] = 255 - x;
} else {
gammatable[i] = x;
}
//Gamma table end
}
}
void loop()
{
rotStep.step(8); //steps the motor 90 degrees
delay(200); //Gives the program time to drop ball before moving on
This is the part that I would like to be seperated from the rest of the program
//BELOW IS THE COLOR SENSING AND STORAGE
uint16_t clear, red, green, blue;
tcs.setInterrupt(false);// turn on LED
delay(60); // takes 50ms to read
tcs.getRawData(&red, &green, &blue, &clear);
tcs.setInterrupt(true);
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.print(blue);
// Figure out some basic hex code for visualization
uint32_t sum = clear;
float r, g, b;
r = red; r /= sum;
g = green; g /= sum;
b = blue; b /= sum;
r *= 256; g *= 256; b *= 256;
And this is the remaining code
//Now It will take that data and compare
if (1 == 1)//replace with if it matches criteria
{
posServo.write(posMetal);
}
if (1 == 1) //replace with critea for wood
{
posServo.write(posWood);
}
if (1 == 1) //replace with plastic
{
posServo.write(posPlastic);
}
}