Hey folks,
I wrote my first program for an Arduino UnoR3 (well, I cobbled bits here and there in fairness). It's a simple luxometer that gives feedback in a few ways. Everything was working until I rewired it and changed a few things and now the red LED of the RGB LED will not accept analog settings. It's like the line of code doesn't exist. It's hitting the if correctly because I've put serial outputs at several points and it's running through the function correctly. I've changed several of the outputs here to digitalwrite and those function properly. Analog calls result in no red, orange, or yellow light. I've moved pin 3 around, no dice. I resat the LED. No difference.
And yeah I know I'm controlling a lot of things with one potentiometer. I only have the one! I started to comment this code for my friend who is trying to pick up programming.
Am I doing something dumb?
#define pVIN 5 //Voltage out
#define pRes 10000 //photocell resistance
//includes
#include <LiquidCrystal.h>
#include <Servo.h>
//pins
//LiquidCrystal lcd(7,9,10,11,12,13);
LiquidCrystal lcd (13, 11, 10, 8, 7, 9);
int redLED = 3;
int greenLED = 5;
int blueLED = 6;
int speaker = 4;
//servos
Servo captainServo;
//global vars
int sleep = 500; //long delay
int nap = 150; //medium delay
int wink = 10; //short delay
int threshold = 100; //threshold between light and dark in lumens
int brightness = 0; //LED intensity
double potPos = A0; //position of the potentiometer (volume knobby thing)
bool blinkerToggle = 1; //whether the LEDs have blinked or not
int lux = 0; //lumens
int currentFrequency = 133; //frequency to pump to the speaker by default
int beats = 1; //number of times the buzzer should play a frequency
int beatLength = 100; //length of the beats the buzzer should play
int lastNote = 0; //record of the last note that was played
bool startup = 0; //whether the LCD has done it's startup routine
int servoPos; //position of the pointless servo motor I'm fucking with
//functions
void red();
void orange();
void yellow();
void green();
void cyan();
void magenta();
void blue();
void ledToggle();
int sensorToLumens(int raw);
void play (char note, int beats);
void setup()
{
//init output pins
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);
pinMode(blueLED, OUTPUT);
//init input pins;
pinMode(A0, INPUT);
pinMode(A1, INPUT);
//attach servos
captainServo.attach(2);
//begin lcd com
lcd.begin(16, 2);
lcd.clear();
//begin serial com
Serial.begin(9600);
}
void loop()
{
//vars
double photoDat = A1;
//init LCD
lcd.setCursor(0, 0);
lcd.print("Lux: ");
lcd.print(lux);
if (startup == 0)
{
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("MartinDeLuxmeter");
startup = 1;
delay(1000);
lcd.clear();
}
//poll sensors
//poll photo cell
photoDat = analogRead(A1);
//poll potentiometer
potPos = analogRead(A0);
currentFrequency = (potPos / 2) + 20;
//convert pot to brightness voltage (0-255)
brightness = map(potPos, 0, 1023, 0, 255);
lux = sensorToLumens(photoDat);
//determine color and toggle LED
if (lux < threshold)
{
//lcd output
lcd.setCursor(0, 1);
lcd.write("It's dark. ");
Serial.print("It's dark. ");
//blinker limiter
if (blinkerToggle == 1)
{
blinkerToggle = 0;
ledToggle();
}
if (lux > 0 && lux <= 15)
{
red();
Serial.println("You need my red light.");
if (lastNote != 0)
{
play('c', 2);
lastNote = 1;
}
}
if (lux > 15 && lux <= 30)
{
orange();
if (lastNote != 1)
{
play('d', 2);
Serial.println("You need my orange light.");
lastNote = 1;
}
}
if (lux > 30 && lux <= 45)
{
yellow();
if (lastNote != 2)
{
play('e', 2);
Serial.println("You need my yellow light.");
lastNote = 2;
}
}
if (lux > 45 && lux <= 60)
{
green();
if (lastNote != 3)
{
play('f', 2);
Serial.println("You need my green light.");
lastNote = 3;
}
}
if (lux > 60 && lux <= 75)
{
cyan();
if (lastNote != 4)
{
play('g', 2);
Serial.println("You need my cyan light.");
lastNote = 4;
}
}
if (lux > 75 && lux <= 90)
{
blue();
if (lastNote != 5)
{
play('a', 2);
Serial.println("You need my blue light.");
lastNote = 5;
}
}
if (lux > 90 && lux <= 100)
{
magenta();
if (lastNote != 6)
{
play('b', 2);
Serial.println("You need my magenta light.");
lastNote = 6;
}
}
}
else
{
//lcd output
lcd.setCursor(0, 1);
lcd.write("It's bright.");
Serial.println("It's bright.");
if (blinkerToggle == 0)
{
blinkerToggle = 1;
ledToggle();
}
}
//fuck with servos for no reason
servoPos = map(potPos, 0, 1023, 20, 160);
captainServo.write(servoPos);
//serial output
Serial.print("Lux: ");
Serial.println(lux);
delay(nap);
lcd.clear();
}
void red()
{
digitalWrite(redLED, HIGH);
analogWrite(greenLED, 0);
analogWrite(blueLED, 0);
Serial.print("RED LED ON");
}
void orange()
{
digitalWrite(redLED, HIGH);
analogWrite(greenLED, brightness / 2);
analogWrite(blueLED, 0);
}
void yellow()
{
digitalWrite(redLED, HIGH);
analogWrite(greenLED, brightness);
analogWrite(blueLED, 0);
}
void green()
{
analogWrite(redLED, 0);
analogWrite(greenLED, brightness);
analogWrite(blueLED, 0);
}
void cyan()
{
analogWrite(redLED, 0);
analogWrite(greenLED, brightness);
analogWrite(blueLED, brightness);
}
void magenta()
{
analogWrite(redLED, brightness);
analogWrite(greenLED, 0);
analogWrite(blueLED, brightness);
}
void blue()
{
analogWrite(redLED, 0);
analogWrite(greenLED, 0);
analogWrite(blueLED, brightness);
}
void ledToggle()
{
//turn off all LEDs
analogWrite(redLED, 0);
analogWrite(greenLED, 0);
analogWrite(blueLED, 0);
//blink red twice
analogWrite(redLED, 100);
tone(speaker, currentFrequency, beats * beatLength);
delay(beats * beatLength);
delay(nap);
analogWrite(redLED, 0);
delay(nap);
analogWrite(redLED, 100);
tone(speaker, currentFrequency, beats * beatLength);
delay(beats * beatLength);
delay(nap);
analogWrite(redLED, 0);
}
int sensorToLumens(int raw)
{
// Conversion rule
float Vout = float(raw) * (pVIN / float(1023));// Conversion analog to voltage
float RLDR = (pRes * (pVIN - Vout)) / Vout; // Conversion voltage to resistance
int phys = 500 / (RLDR / 1000); // Conversion resitance to lumen
return phys;
}
void play (char note, int beats)
{
int numNotes = 14;
int x = 0;
char notes[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C', 'D', 'E', 'F', 'G', 'A', 'B', ' '};
int frequencies[] = {131, 147, 165, 175, 196, 220, 247, 262, 294, 330, 349, 392, 440, 494, 0};
for (x = 0; x < numNotes; x++)
{
if (notes[x] == note)
{
currentFrequency = frequencies[x];
}
}
//play the frequency associated with the note
tone(speaker, currentFrequency, beats * beatLength);
//set lastNote record
lastNote = x;
delay(beats * beatLength);
delay(50);
}