Hi. After tinkering with some code I finally managed to make this! ;D Here is the code.
// Ipod mood lamp
//By: Ryan Gosiaco
// Red led is MotorA=pin 9
// Green led is MotorB=pin 10
// Blue led is MotorC=pin 11
// I tweaked the code... so have fun!
#define PwmPinMotorA 9
#define PwmPinMotorB 10
#define PwmPinMotorC 11
#define DirectionPinMotorA 12
#define DirectionPinMotorB 13
#define DirectionPinMotorC 8
#define SerialSpeed 9600
#define BufferLength 16
#define LineEnd '#'
char inputBuffer[BufferLength];
void setup()
{
// motor pins must be outputs
pinMode(PwmPinMotorA, OUTPUT);
pinMode(PwmPinMotorB, OUTPUT);
pinMode(PwmPinMotorC, OUTPUT);
pinMode(DirectionPinMotorA, OUTPUT);
pinMode(DirectionPinMotorB, OUTPUT);
pinMode(DirectionPinMotorC, OUTPUT);
Serial.begin(SerialSpeed);
}
// process a command string
void HandleCommand(char* input, int length)
{
Serial.println(input);
if (length < 2) { // not a valid command
return;
}
int value = 0;
// calculate number following command
if (length > 2) {
value = atoi(&input[2]);
}
int* command = (int*)input;
// check commands
// note that the two bytes are swapped, ie 'RA' means command AR
switch(*command) {
case 'FA':
// motor A forwards
analogWrite(PwmPinMotorA, value);
digitalWrite(DirectionPinMotorA, HIGH);
break;
case 'RA':
// motor A reverse
analogWrite(PwmPinMotorA, value);
digitalWrite(DirectionPinMotorA, LOW);
break;
case 'FB':
// motor B forwards
analogWrite(PwmPinMotorB, value);
digitalWrite(DirectionPinMotorB, LOW);
break;
case 'RB':
// motor B reverse
analogWrite(PwmPinMotorB, value);
digitalWrite(DirectionPinMotorB, HIGH);
break;
case 'FC':
// motor C forwards
analogWrite(PwmPinMotorC, value);
digitalWrite(DirectionPinMotorC, LOW);
break;
case 'RC':
// motor C reverse
analogWrite(PwmPinMotorC, value);
digitalWrite(DirectionPinMotorC, HIGH);
break;
default:
break;
}
}
void loop()
{
// get a command string form the serial port
int inputLength = 0;
do {
while (!Serial.available()); // wait for input
inputBuffer[inputLength] = Serial.read(); // read it in
} while (inputBuffer[inputLength] != LineEnd && ++inputLength < BufferLength);
inputBuffer[inputLength] = 0; // add null terminator
HandleCommand(inputBuffer, inputLength);
}
Link to come!
Proccesing code
import oscP5.*;
import netP5.*;
import processing.serial.*;
Serial arduinoPort;
OscP5 oscP5;
float [] fader = new float [4];
void setup() {
oscP5 = new OscP5(this,8000);
arduinoPort = new Serial(this, Serial.list()[0], 9600);
}
void oscEvent(OscMessage theOscMessage) {
String addr = theOscMessage.addrPattern();
if(addr.indexOf("/1/fader") !=-1){
String list[] = split(addr,'/');
int xfader = int(list[2].charAt(5) - 0x30);
if(theOscMessage.get(0).floatValue() !=0){
fader[xfader] = theOscMessage.get(0).floatValue();
}
}
}
void draw() {
//---------------------------------Motor A
if(fader[1] > 0.99){
arduinoPort.write("AF255#");
}
if(fader[1] > 0.89){
arduinoPort.write("AF230#");
}
if(fader[1] > 0.79){
arduinoPort.write("AF205#");
}
if(fader[1] > 0.69){
arduinoPort.write("AF180#");
}
if(fader[1] > 0.59){
arduinoPort.write("AF155#");
}
if(fader[1] > 0.49){
arduinoPort.write("AF130#");
}
if(fader[1] > 0.39){
arduinoPort.write("AF105#");
}
if(fader[1] > 0.29){
arduinoPort.write("AF80#");
}
if(fader[1] > 0.19){
arduinoPort.write("AF55#");
}
if(fader[1] > 0.14){
arduinoPort.write("AF40#");
}
if(fader[1] < 0.09){
arduinoPort.write("AF30#");
}
//--------------------------------Motor B
if(fader[2] > 0.99){
arduinoPort.write("BF255#");
}
if(fader[2] > 0.89){
arduinoPort.write("BF230#");
}
if(fader[2] > 0.79){
arduinoPort.write("BF205#");
}
if(fader[2] > 0.69){
arduinoPort.write("BF180#");
}
if(fader[2] > 0.59){
arduinoPort.write("BF155#");
}
if(fader[2] > 0.49){
arduinoPort.write("BF130#");
}
if(fader[2] > 0.39){
arduinoPort.write("BF105#");
}
if(fader[2] > 0.29){
arduinoPort.write("BF80#");
}
if(fader[2] > 0.19){
arduinoPort.write("BF55#");
}
if(fader[2] > 0.14){
arduinoPort.write("BF40#");
}
if(fader[2] < 0.09){
arduinoPort.write("BF30#");
}
//--------------------------------Motor C
if(fader[3] > 0.99){
arduinoPort.write("CF255#");
}
if(fader[3] > 0.89){
arduinoPort.write("CF230#");
}
if(fader[3] > 0.79){
arduinoPort.write("CF205#");
}
if(fader[3] > 0.69){
arduinoPort.write("CF180#");
}
if(fader[3] > 0.59){
arduinoPort.write("CF155#");
}
if(fader[3] > 0.49){
arduinoPort.write("CF130#");
}
if(fader[3] > 0.39){
arduinoPort.write("CF105#");
}
if(fader[3] > 0.29){
arduinoPort.write("CF80#");
}
if(fader[3] > 0.19){
arduinoPort.write("CF55#");
}
if(fader[3] > 0.14){
arduinoPort.write("CF40#");
}
if(fader[3] < 0.09){
arduinoPort.write("CF30#");
}
//----------------------------stop commands
if(fader[1] < 0.99 && fader[1] > 0.09 ){
arduinoPort.write("AF0#");
}
if(fader[2] < 0.99 && fader[2] > 0.09 ){
arduinoPort.write("BF0#");
}
if(fader[3] < 0.99 && fader[3] > 0.09 ){
arduinoPort.write("CF0#");
}
}
BTW I posted this in interfacing hardware already because I didn’t know. :-/
LINK!!! http://camera.byethost4.com/arduino/ipodmoodlamp/
BTW The ipod app u need is touchosc
EDIT:http://www.youtube.com/watch?v=X8zPbXWM1D8http://www.youtube.com/watch?v=X8zPbXWM1D8