// Dino -- Ambient Display Creature // // By James Kim, dzeikei1@hotmail.com // // Last Updated 8/11/2006 int servoPin = 2; // Control pin for servo motor int lastPulse = 0; // the time in milliseconds of the last pulse int refreshTime = 20; // the time needed in between pulses int pulse = 0; int servoPos = 50; int redPin = 9; // Green LED, connected to digital pin 9 int greenPin = 10; // Yellow LED, connected to digital pin 10 int bluePin = 11; // Red LED, connected to digital pin 11 int in = -1; int redVal = 0; int greenVal = 0; int blueVal = 0; void setup() { pinMode(redPin, OUTPUT); pinMode(greenPin, OUTPUT); pinMode(bluePin, OUTPUT); pinMode(servoPin, OUTPUT); Serial.begin(9600); } void loop(){ if (Serial.available() > 0){ in = Serial.read(); if( in >= 30 && in <= 70 ){ servoPos = in; if (millis() - lastPulse >= refreshTime){ pulse = servoPos * 20 + 500; digitalWrite(servoPin, HIGH); // Turn the motor on delayMicroseconds(pulse); // Length of the pulse sets the motor position digitalWrite(servoPin, LOW); // Turn the motor off lastPulse = millis(); // save the time of the last pulse } } // servo position if( in == 101 ){ redVal = 0; greenVal = 0; blueVal = 0; } if( in == 102 ){ redVal = 0; } if( in == 103 ){ greenVal = 0; } if( in == 104 ){ blueVal = 0; } if( in >= 105 && in <= 108 ){ redVal = redVal + (in - 104) * 64; } // red LED increase if( in >= 109 && in <= 112 ){ redVal = redVal - (in - 108); } // red LED decrease if( in >= 113 && in <= 116){ greenVal = greenVal + (in - 112) * 64; } // green LED increase if( in >= 117 && in <= 120){ greenVal = greenVal - (in - 116); } // green LED decrease if( in >= 121 && in <= 124){ blueVal = blueVal + (in - 120) * 64; } // blue LED increase if( in >= 125 && in <= 128){ blueVal = blueVal - (in - 124) * 4; } // blue LED decrease if(redVal > 255){ redVal = 255; } if(greenVal > 255){ greenVal = 255; } if(blueVal > 255){ blueVal = 255; } if(redVal < 0){ redVal = 0; } if(greenVal < 0){ greenVal = 0; } if(blueVal < 0){ blueVal = 0; } analogWrite(redPin, redVal); analogWrite(greenPin, greenVal); analogWrite(bluePin, blueVal); } }