For my project, I created a photosensor controlled RGB led. As you cover each of the three photosensors either the Red, Green, or Blue value will go down based on which respective photosensor is covered.
#include "Adafruit_NeoPixel.h"
#define PIXEL_PIN 11
#define PIXEL_COUNT 1
#define PIXEL_TYPE NEO_RGB + NEO_KHZ800
#define RED_PIN 0
#define GRN_PIN 1
#define BLU_PIN 2
#define VLOWR (2.75 * (1024.0/5.0))
#define VHIGHR (3.95 * (1024.0/5.0))
#define VLOWG (2.4 * (1024.0/5.0))
#define VHIGHG (3.72 * (1024.0/5.0))
#define VLOWB (2.4 * (1024.0/5.0))
#define VHIGHB (3.72 * (1024.0/5.0))
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
void setup()
{
// Initialize the serial UART at 9600 bits per second.
Serial.begin(9600);
// Initialize NeoPixels and turn them off.
pixels.begin();
lightPixels(pixels.Color(0, 0, 0));
}
void loop()
{
// Read the voltage on the sensor input. This function returns a value
// between 0 and 1023 representing a voltage between 0 and 5V.
int redSensor = analogRead(RED_PIN);
int grnSensor = analogRead(GRN_PIN);
int bluSensor = analogRead(BLU_PIN);
// Compute proportional signals to drive the LEDS by mapping an input range
// to the output range.
//
// The PWM output is scaled from 0 to 255.
//
// The input range for a typical photocell with a 5.6K bias resistor is
// centered around 4 volts. This can be verified using a voltmeter to
// check the range of inputs on A0 and adjust the following values. Note
// the use of the scaling factor to express voltage in terms of the input
// range:
int red = constrain(map(redSensor, VLOWR, VHIGHR, 0, 255), 0, 255);
int green = constrain(map(grnSensor, VLOWG, VHIGHG, 0, 255), 0, 255);
int blue = constrain(map(bluSensor, VLOWB, VHIGHB, 0, 255), 0, 255);
Serial.print("\nRED: ");
Serial.print(red);
Serial.print("\nGREEN: ");
Serial.print(green);
Serial.print("\nBLUE: ");
Serial.print(blue);
// Emit PWM signals with a proportional average power; the LEDs will have
// variable brightness. The constrain function will ensure the values stay
// within the correct limits.
lightPixels(pixels.Color(red, green, blue));
// Delay for a short interval to create a periodic sampling rate.
delay(200);
}
void lightPixels(uint32_t color) {
for (int i = 0; i < PIXEL_COUNT; ++i) {
pixels.setPixelColor(i, color);
}
pixels.show();
}
Leave a Reply
You must be logged in to post a comment.