Thank you PaulS for the advice. I finally kept searching and turns out the answers were in the Tutorial section of the main Arudino.cc site the whole time. Face Palm Doh!
Any way I wanted to share the working sketches and the Arduino layout in case anyone else wanted to give it a shot. As always, advice is welcome and if you do this please share your experience. ![]()
Arduino Layout:
Arduino Code:
/*
This is the Arduino half of the code to enable you to controll an RGB color swatch on your PC using an Arduino and three potentiometers
This code was adapted from a tutorial called Serial Call and Response in ASCII and can be found at http://www.arduino.cc/en/Tutorial/SerialCallResponseASCII
The original code and this adaptation are both in the public domain.
A personal thank you to all the authors of this tutorial!
*/
int RedVal = 0;
int GreenVal = 0;
int BlueVal = 0;
int inByte = 0; // incoming serial byte
int RedPotPin= A0;
int GreenPotPin=A1;
int BluePotPin= A2;
void setup()
{
// start serial port at 9600 bps and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
pinMode(RedPotPin, INPUT);
pinMode(GreenPotPin, INPUT);
pinMode(BluePotPin, INPUT);
establishContact(); // send a byte to establish contact until receiver responds
}
void loop()
{
// if we get a valid byte, read analog ins:
if (Serial.available() > 0) {
// get incoming byte:
inByte = Serial.read();
// I had to map my potentiometers values as they did not all have the same resistance.
RedVal = map(analogRead(RedPotPin), 0, 1023, 0, 255);
GreenVal = map(analogRead(GreenPotPin), 33, 972, 0, 255);
BlueVal = map(analogRead(BluePotPin), 0, 1023, 0, 255);
// send sensor values:
Serial.print(RedVal);
Serial.print(",");
Serial.print(GreenVal);
Serial.print(",");
Serial.println(BlueVal);
}
}
void establishContact() {
while (Serial.available() <= 0) {
Serial.println("0,0,0"); // send an initial string
delay(300);
}
}
Processing Code:
/*
This is the Processing half of the code to enable you to controll an RGB color swatch on your PC using an Arduino and three potentiometers
This code was adapted from a tutorial called Serial Call and Response in ASCII and can be found at http://www.arduino.cc/en/Tutorial/SerialCallResponseASCII
The original code and this adaptation are both in the public domain.
A personal thank you to all the authors of this tutorial!
*/
import processing.serial.*; // import the Processing serial library
Serial myPort; // The serial port
float BrightnessRed;
float BrightnessGreen;
float BrightnessBlue;
void setup() {
size(640,480);
myPort = new Serial(this, "COM3" , 9600);
// read bytes into a buffer until you get a linefeed (ASCII 10):
myPort.bufferUntil('\n');
}
void draw() {
background(BrightnessRed,BrightnessGreen,BrightnessBlue);
}
// serialEvent method is run automatically by the Processing applet
// whenever the buffer reaches the byte value set in the bufferUntil()
// method in the setup():
void serialEvent(Serial myPort) {
// read the serial buffer:
String myString = myPort.readStringUntil('\n');
// if you got any bytes other than the linefeed:
myString = trim(myString);
// split the string at the commas
// and convert the sections into integers:
int sensors[] = int(split(myString, ','));
// print out the values you got:
for (int sensorNum = 0; sensorNum < sensors.length; sensorNum++) {
print("Sensor " + sensorNum + ": " + sensors[sensorNum] + "\t");
}
// add a linefeed after all the sensor values are printed:
println();
if (sensors.length > 1) {
BrightnessRed =sensors[0];
BrightnessGreen = sensors [1];
BrightnessBlue= sensors [2];
}
// send a byte to ask for more data:
myPort.write("A");
}
