Skip to content

Instantly share code, notes, and snippets.

@eightlines
Created January 22, 2013 03:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eightlines/4591820 to your computer and use it in GitHub Desktop.
Save eightlines/4591820 to your computer and use it in GitHub Desktop.
Adjusts RoboBrrd's eye color based on the ambient light within the room. The RGB-HSV Color Wheel Script by Eduardo A. Flores Verduzco provides the Color Wheel functionality. Adjust the upper and lower Photo Cell limit thresholds to manipulate the degree to which the color changes.
#include <Streaming.h>
const int r = 3; // R PIN
const int g = 5; // G PIN
const int b = 6; // B PIN
const int leds[] = {r, g, b}; // RGB PINS
const int photoCellL = A0; // LEFT PHOTOCELL
const int photoCellR = A1; // RIGHT PHOTOCELL
const int lowerPhotoCellLimit = 600; // PHOTOCELL ADJUSTMENT THRESHOLDS
const int upperPhotoCellLimit = 1024; // 0-1024
void setup() {
Serial.begin(9600);
for (int i = 0; i < 3; i++) {
pinMode(leds[i], OUTPUT); // RGB LED SETUP
}
pinMode(photoCellL, INPUT); // PHOTOCELL SETUP
pinMode(photoCellR, INPUT);
}
void loop() {
// Uncomment the following line to see the Photo Cell sensor readings in your Serial Monitor
// These numbers may be useful to determine the range of values you are receiving from the Photo Cells
// Adjust the lower and upper Photo Cell limits above accordingly
//Serial << "PHOTOCELL L: " << analogRead(photoCellL) << "; PHOTOCELL R: " << analogRead(photoCellR) << ";" << endl;
// Determine the average light reading from the PhotoCells
double avgPhotoCell = (analogRead(photoCellL) + analogRead(photoCellR)) / 2;
// Convert the average reading to a range of 0-360
int avgPhotoCellMap = (int)map(avgPhotoCell, lowerPhotoCellLimit, upperPhotoCellLimit, 0, 360);
// Set the LEDs to a color on the color wheel
setLedColorHSV(avgPhotoCellMap, 1, 1);
}
// Color Wheel LED
// Created 1 January 2011
// By Eduardo A. Flores Verduzco
// http://eduardofv.com/read_post/179-Arduino-RGB-LED-HSV-Color-Wheel-
void setLedColorHSV(int h, double s, double v) {
double r = 0;
double g = 0;
double b = 0;
double hf = h / 60.0;
int i = (int)floor(h / 60.0);
double f = h / 60.0 - i;
double pv = v * (1 - s);
double qv = v * (1 - s * f);
double tv = v * (1 - s * (1 - f));
switch (i) {
case 0:
r = v;
g = tv;
b = pv;
break;
case 1:
r = qv;
g = v;
b = pv;
break;
case 2:
r = pv;
g = v;
b = tv;
break;
case 3:
r = pv;
g = qv;
b = v;
break;
case 4:
r = tv;
g = pv;
b = v;
break;
case 5:
r = v;
g = pv;
b = qv;
break;
}
int red = constrain((int)255 * r, 0, 255);
int green = constrain((int)255 * g, 0, 255);
int blue = constrain((int)255 * b, 0, 255);
setLedColor(red, green, blue);
}
void setLedColor(int red, int green, int blue) {
analogWrite(leds[0], red);
analogWrite(leds[1], green/3);
analogWrite(leds[2], blue/3);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment