So I've got this code for a night light I'm working on. It will automatically turn on and off with day/night. There are three trim pots for settings - one for RGB cycle speed, one for LED brightness and one for the darkness setting for the on/off control. I have the code in for the brightness but the problem is I can't figure out where to insert the int for the brightness to change it.
I also can't seem to get the color cycling to go fast. With the speed pot turned to full it cycles pretty slow - about 3 minutes or so for a full cycle. I'm looking for like 15-20 seconds for a full cycle on max.
Here's the code:
// Outputs
const int redPin = 6; // Red LED pin connected to digital output 6
const int grnPin = 5; // Green LED pin connected to digital output 5
const int bluPin = 3; // Blue LED pin connected to digital output 3
// Inputs
const int brtPin = A0; // Brightness pot connected to A0
const int spdPin = A1; // Speed pot hooked to A1
const int lghtPot = A2; // Light setting pot hooked to A3
const int lghtPin = A3; // Light sensor hooked to A3
// Smoothing settings for light sensor
const int numReadings = 100; // Number of readings to average
int readings[numReadings]; // The readings from the analog input
int readIndex = 0; // The index of the current reading
unsigned long total = 0; // The running total
int average = 0; // The average
int spdOutVal = 0;
int spdVal = 0;
int lghtPotVal = 0;
int lightPot = 0;
int lghtOutVal = 0;
int lghtVal = 0;
int brtPotVal = 0;
int brtVal = 0;
int brtOutVal = 0;
void color(int r, int g, int b)
{
analogWrite(redPin, r); // write current values to LED pins
analogWrite(grnPin, g);
analogWrite(bluPin, b);
}
void hsv(float H, float S, float V)
{
// http://www.easyrgb.com/index.php?X=MATH&H=21#text21
int var_i;
float R, G, B, var_1, var_2, var_3, var_h;
if (S == 0) {
R = V;
G = V;
B = V;
}
else {
var_h = H * 6;
if (var_h == 6) var_h = 0; // H must be < 1
var_i = int(var_h) ; // or ... var_i = floor( var_h )
var_1 = V * (1 - S );
var_2 = V * (1 - S * (var_h - var_i));
var_3 = V * (1 - S * (1 - (var_h - var_i)));
if (var_i == 0) {
R = V;
G = var_3;
B = var_1;
}
else if (var_i == 1) {
R = var_2;
G = V;
B = var_1;
}
else if (var_i == 2) {
R = var_1;
G = V;
B = var_3;
}
else if (var_i == 3) {
R = var_1;
G = var_2;
B = V;
}
else if (var_i == 4) {
R = var_3;
G = var_1;
B = V;
}
else {
R = V;
G = var_1;
B = var_2;
}
}
color(255 * R, 255 * G, 255 * B); // RGB results from 0 to 255
}
unsigned long time;
void setup() {
Serial.begin(9600); // Initialze seial communication
for (int thisReading = 0; thisReading < numReadings; thisReading++) { // Initialize all the readings to zero
readings[thisReading] = 0;
}
pinMode(spdPin, INPUT); // Set speed pin to input
pinMode(brtPin, INPUT); // Set the brightness pin to input
pinMode(lghtPot, INPUT); // Set the light setting pot to input
pinMode(lghtPin, INPUT); // Set the light sensor pin to input
pinMode(redPin, OUTPUT); // Set red pin to output
pinMode(grnPin, OUTPUT); // Set green pin to output
pinMode(bluPin, OUTPUT); // Set blue pin to output
time = millis();
}
void loop() {
Serial.print("Speed Setting = ");
Serial.print(spdVal);
Serial.print("\t Speed = ");
Serial.print(spdOutVal);
Serial.print("\t Light Sensor Value = ");
Serial.print(lghtVal);
Serial.print("\t Light Pot Value = ");
Serial.print(lghtPotVal);
brtVal = analogRead(brtPotVal); // Read the value of the brightness pot
brtOutVal = map(brtVal, 0, 1023, 0, 255); // Map the pot value to max PWM
Serial.print("\t Brightness = ");
Serial.print(brtOutVal);
lghtPotVal = analogRead(lghtPot); // Read the value of the light pot
lghtVal = analogRead(lghtPin); // Read the value of the light sensor
// Averaging functions
total = total - readings[readIndex]; // Subtract the last reading
readings[readIndex] = analogRead(lghtVal); // Read from the light sensor
total = total + readings[readIndex]; // Add the reading to the total
readIndex = readIndex + 1; // Advance to the next position in the array
if (readIndex >= numReadings) { // If we're at the end of the array
readIndex = 0; // Wrap around to the beginning
}
average = total / numReadings; // Calculate the average
Serial.print("\t Light Sensor Average = ");
Serial.println(average);
delay(1); // Delay for stability
if (average >= lghtPotVal) { // If the pot value is lower than the light sensor reading
static float f = 0; // Run color cycling
// Color cycling section
spdVal = analogRead(spdPin); // Read speed control pin
spdOutVal = map(spdVal, 1023, 0, 0, 100); // Map sensor value to between 0 and 100ms
if ((millis() - time) > spdOutVal) { // Add output value in ms to formula
f += 0.0005;
if (f > 1) {
f = 0;
}
hsv(f, 1, 1);
time = millis();
}
}
else{ // Otherwise turn LED's Off
analogWrite(redPin, 1023);
analogWrite(bluPin, 1023);
analogWrite(grnPin, 1023);
}
}