Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
#include <Servo.h>
/* SENSOR SETUP DECK 1
Color Sensor Arduino
----------- --------
VCC 5V
GND GND
ColorPins[0] 2
ColorPins[1] 3
ColorPins[2] 4
ColorPins[3] 5
ColorPins[4] 6
OE GND
DECK 2
VCC 5V
GND GND
ColorPins[0] 8
ColorPins[1] 9
ColorPins[2] 10
ColorPins[3] 11
ColorPins[4] 12
OE GND
*/
//Color Sensor Outputs and Inputs
const int ColorPinsA[5] = {2, 3, 4, 5, 6};
const int ColorPinsB[5] = {8, 9, 10, 11, 12};
//Color Variables R G B
int ColorValuesA[3] = {0, 0, 0};
//Color Variables R G B
int ColorValuesB[3] = {0, 0, 0};
//Current Deck A Color Black White
int DeckA_Color[2] = {0, 0};
//Last Color States Black White
int lastDeckA_Color[2] = {0, 0};
//Current Deck B Color Red Green Blue
int DeckB_Color[3] = {0, 0, 0};
//Last Deck B Color Red Green Blue
int lastDeckB_Color[3] = {0, 0, 0};
int mappedSlider = 0;
int lastSlider = 0;
//Speed Slider
int slider;
//Speed potentiometer
int speedPot;
//Mapping slider to continues angle
int motorMap;
//Servo motor
Servo plateservo;
void setup() {
//Start speaking serial
Serial.begin(9600);
//Initialise the color sensor pins
colorInit(1);
colorInit(2);
//If there is no serial send over x (120) over to cSound for initialisation
while(Serial.available() == 0){
Serial.write('x');
delay(500);
}
}
void loop() {
if(Serial.available() > 0){
readCrossFader();
//Deck A
colorRead(1);
//Deck B
colorRead(2);
//Read slider and write motor speed accordingly
motorControl();
//Process the color sensors and store array values - 1st argument is delay interval second is deck (1,2)
//Deck A
colorProcess(25, 1);
//Deck B
colorProcess(25, 2);
//Deck A
sendSerial(1);
//Deck B
sendSerial(2);
//Prints debug serial on/off for all - argumentt accepts deck A/B (1/2);
//printDebugSerial(1);
//printDebugSerial(2);
//Prints the actual readings from the sensor - argumentt accepts deck A/B (1/2);
//printColorSensorValue(1);
//printColorSensorValue(2);
//Equal current color states to last color states
matchLastColorState();
}
}
void readCrossFader(){
slider = analogRead(A0);
mappedSlider = map(slider, 0, 1023, 10, 20);
if (mappedSlider != lastSlider){
Serial.write(mappedSlider);
}
}
void colorInit(int deck) {
if(deck == 1){
//Init the color sensor output pins
for (int i = 0; i <= 3; i++) {
pinMode(ColorPinsA[i], OUTPUT);
}
//Init the color sensor input pin
pinMode(ColorPinsA[4], INPUT);
//Turn the LED on
digitalWrite(ColorPinsA[0], HIGH);
digitalWrite(ColorPinsA[1], HIGH);
} else if (deck == 2){
//Init the color sensor output pins
for (int i = 0; i <= 3; i++) {
pinMode(ColorPinsB[i], OUTPUT);
}
//Init the color sensor input pin
pinMode(ColorPinsB[4], INPUT);
//Turn the LED on
digitalWrite(ColorPinsB[0], HIGH);
digitalWrite(ColorPinsB[1], HIGH);
}
}
void colorRead(int deck) {
if(deck == 1){
digitalWrite(ColorPinsA[2], LOW);
digitalWrite(ColorPinsA[3], LOW);
//count OUT, pRed, RED
ColorValuesA[0] = pulseIn(ColorPinsA[4], digitalRead(ColorPinsA[4]) == HIGH ? LOW : HIGH);
digitalWrite(ColorPinsA[3], HIGH);
//count OUT, pBLUE, BLUE
ColorValuesA[1] = pulseIn(ColorPinsA[4], digitalRead(ColorPinsA[4]) == HIGH ? LOW : HIGH);
digitalWrite(ColorPinsA[2], HIGH);
//count OUT, pGreen, GREEN
ColorValuesA[2] = pulseIn(ColorPinsA[4], digitalRead(ColorPinsA[4]) == HIGH ? LOW : HIGH);
} else if (deck == 2){
digitalWrite(ColorPinsB[2], LOW);
digitalWrite(ColorPinsB[3], LOW);
//count OUT, pRed, RED
ColorValuesB[0] = pulseIn(ColorPinsB[4], digitalRead(ColorPinsB[4]) == HIGH ? LOW : HIGH);
digitalWrite(ColorPinsA[3], HIGH);
//count OUT, pBLUE, BLUE
ColorValuesB[1] = pulseIn(ColorPinsB[4], digitalRead(ColorPinsB[4]) == HIGH ? LOW : HIGH);
digitalWrite(ColorPinsA[2], HIGH);
//count OUT, pGreen, GREEN
ColorValuesB[2] = pulseIn(ColorPinsB[4], digitalRead(ColorPinsB[4]) == HIGH ? LOW : HIGH);
}
}
void colorProcess(int interval, int deck) {
if(deck == 1){
if (ColorValuesA[0] >= 32 && ColorValuesA[0] <= 34 && ColorValuesA[1] <= 32 && ColorValuesA[1] >= 30 && ColorValuesA[2] >= 39 && ColorValuesA[2] <= 41) {
//It's Black
DeckA_Color[0] = HIGH;
} else {
DeckA_Color[0] = LOW;
}
if (ColorValuesA[0] <= 8 && ColorValuesA[0] >= 7 && ColorValuesA[1] <= 8 && ColorValuesA[1] >= 6 && ColorValuesA[2] <= 9 && ColorValuesA[2] >= 7) {
//It's White
DeckA_Color[1] = HIGH;
} else {
DeckA_Color[1] = LOW;
}
}
if (deck == 2){
if (ColorValuesB[0] >= 8 && ColorValuesB[1] >= 8 && ColorValuesB[2] >= 8 && ColorValuesB[0] <= 9 && ColorValuesB[1] <= 9 && ColorValuesB[2] <= 9 || ColorValuesB[0] >= 6 && ColorValuesB[0] <= 7 && ColorValuesB[1] >= 6 && ColorValuesB[1] <= 7 && ColorValuesB[2] >= 6 && ColorValuesB[2] <= 7) {
//It's Red
DeckB_Color[0] = HIGH;
} else {
DeckB_Color[0] = LOW;
}
if (ColorValuesB[0] >= 28 && ColorValuesB[0] <= 30 && ColorValuesB[1] >= 28 && ColorValuesB[1] <= 30 && ColorValuesB[2] >= 28 && ColorValuesB[2] <= 30 || ColorValuesB[0] >=23 && ColorValuesB[0] <=24 && ColorValuesB[1] >=23 && ColorValuesB[1] <=24 && ColorValuesB[2] >=23 && ColorValuesB[2] <=24) {
//It's Green
DeckB_Color[1] = HIGH;
} else {
DeckB_Color[1] = LOW;
}
if (ColorValuesB[0] >= 31 && ColorValuesB[0] <= 32 && ColorValuesB[1] >= 31 && ColorValuesB[1] <= 32 && ColorValuesB[2] >= 31 && ColorValuesB[2] <= 32) {
//It's Blue
DeckB_Color[2] = HIGH;
} else {
DeckB_Color[2] = LOW;
}
}
//Interval
delay(interval);
}
void sendSerial(int deck){
if(deck == 1){
//Deck A
if (DeckA_Color[0] == HIGH && lastDeckA_Color[0] == LOW) {
//It's black
Serial.write('1');
}
if (DeckA_Color[1] == HIGH && lastDeckA_Color[1] == LOW) {
//It's white
Serial.write('2');
}
} else if(deck == 2){
//Deck B
if (DeckB_Color[0] == HIGH && lastDeckB_Color[0] == LOW) {
//If it's red but it wasn't red
Serial.write("3");
} else if (DeckB_Color[1] == HIGH && lastDeckB_Color[1] == LOW) {
//If it's green but it wasn't green
Serial.write("4");
// Serial.println("Green");
} else if (DeckB_Color[2] == HIGH && lastDeckB_Color[2] == LOW) {
//If it's blue but it wasn't blue
Serial.write("5");
// Serial.println("Blue");
}
}
delay(50);
}
void printDebugSerial(int deck){
if(deck == 1){
//PRINT THE COLOR STATE (HIGH/LOW) -FOR DEVELOPMENT
Serial.print(DeckA_Color[0]);
Serial.print(',');
Serial.println(DeckA_Color[1]);
} else if (deck == 2){
Serial.print(DeckB_Color[0]);
Serial.print(",");
Serial.print(DeckB_Color[1]);
Serial.print(",");
Serial.println(DeckB_Color[2]);
}
}
void printColorSensorValue(int deck){
if(deck == 1){
//PRINT THE RGB SENSOR DATA FOR DEVELOPMENT
//Red
Serial.print(ColorValuesA[0]);
//Space
Serial.print(' ');
//Blue
Serial.print(ColorValuesA[1]);
//Space
Serial.print(' ');
//Green
Serial.println(ColorValuesA[2]);
} else if(deck == 2){
//PRINT THE RGB SENSOR DATA FOR DEVELOPMENT
//Red
Serial.print(ColorValuesB[0]);
//Space
Serial.print(' ');
//Blue
Serial.print(ColorValuesB[1]);
//Space
Serial.print(' ');
//Green
Serial.println(ColorValuesB[2]);
}
}
void motorControl(){
//Connect the servo
plateservo.attach(7);
speedPot = analogRead(A1);
//Serial.println(slider);
//Map the analog values to the servo range
motorMap = map(speedPot, 0, 1023, 88, 65);
if (speedPot <= 4){
plateservo.detach();
} else {
plateservo.attach(7);
}
//Serial.println(slider);
//Write the servo angle with the mapped values
plateservo.write(motorMap);
//plateservo.detach();
delay(10);
}
void matchLastColorState(){
//Deck A
for (int i = 0; i <= sizeof(DeckA_Color); i++){
DeckA_Color[i] = lastDeckA_Color[i];
}
//Deck B
for (int i = 0; i <= sizeof(DeckB_Color); i++){
DeckB_Color[i] = lastDeckB_Color[i];
}
lastSlider = mappedSlider;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment