Hello all,
I am working on a project to sort three different types of marbles into three different boxes.
We are using the Adafruit TCS34725 color sensor for color.
We are using an RGB LED to verify color.
We are using a stepper motor with 4 slots, marble droped in on top, on side it is sensed, on bottom it is dropped, on other side is empty.
We are using a servo to control the redirection of the marble.
I have the necessary libraries imported and the setup code written for the color sensor and the servo, and I am waiting on an H bridge for the stepper.
I am wondering what would be the best way to go about coding this. Should I write a void() function for each of the processes, to rotate stepper, then jump to read color, then jump to switch servo, then to rotate stepper again? If I do that method, how can I transfer variables across multiple functions? Shoud I just write the code all in the loop function, then have it flow from one to the next with if statements? Could I specify variabes and colors for each and then have a simple command to see whether it fits within those properties, then jump to labels that will move it to the position?
Thanks a lot! I really appreciate any advice or examples. Below is the code so far.
//These are the Libraries that MUST be included
#include <Wire.h>
#include "Adafruit_TCS34725.h"
#include <Servo.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 definition
Servo posServo;
//This is the Color sensor setup
byte gammatable[256];
Adafruit_TCS34725 tcs = Adafruit_TCS 34725(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);
//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() {
// put your main code here, to run repeatedly:
}