Hi,
I have a photocell as an input and an analogue RGB LED as the output for a project I am working on. I would like the RGB LED to become brighter the darker it is at the sensor. However, I want the colour it lights up to be a particular purple (110, 0, 255) for (red, green, blue).
I am not very experienced with scripting, and have been trying to use this script from one of Ladyada's tutorials as a starting point (found at How to use photocells, LDRs, CdS cells, photoresistors!). It does exactly what I want it to do, aside from being able to function with the 3 separate colours of the RGB LED.
int photocellPin = 0; // the cell and 10K pulldown are connected to a0
int photocellReading; // the analog reading from the sensor divider
int LEDpin = 11; // connect Red LED to pin 11 (PWM pin)
int LEDbrightness; //
void setup(void) {
// We'll send debugging information via the Serial monitor
Serial.begin(9600);
}
void loop(void) {
photocellReading = analogRead(photocellPin);
Serial.print("Analog reading = ");
Serial.println(photocellReading); // the raw analog reading
// LED gets brighter the darker it is at the sensor
// that means we have to -invert- the reading from 0-1023 back to 1023-0
photocellReading = 1023 - photocellReading;
//now we have to map 0-1023 to 0-255 since thats the range analogWrite uses
LEDbrightness = map(photocellReading, 0, 1023, 0, 255);
analogWrite(LEDpin, LEDbrightness);
delay(100);
}
So far my script reads as below, but when verified it comes up saying ' expected primary-expression before '[' token' with 'analogWrite(LEDAnalog, , LEDbrightness);' highlighted
//RGB LED pins
int LEDAnalog[] = {3, 5, 6}; //the three pins of the analog LED 3 = redPin, 5 = greenPin, 6 = bluePin
int photocellPin = 0; // the cell and 10K pulldown are connected to a0
int photocellReading; // the analog reading from the sensor divider
int LEDbrightness; //
//Defined Colors (different RGB (red, green, blue) values for colors
const byte PURPLE[] = {110, 0, 255};
void setup() {
for(int i = 0; i < 3; i++){
pinMode(LEDAnalog[i], OUTPUT);} //Set the three LED pins as outputs
Serial.begin(9600);
}
void loop(void) {
photocellReading = analogRead(photocellPin);
Serial.print("Analog reading = ");
Serial.println(photocellReading); // the raw analog reading
// LED gets brighter the darker it is at the sensor
// that means we have to -invert- the reading from 0-1023 back to 1023-0
photocellReading = 1023 - photocellReading;
//now we have to map 0-1023 to 0-255 since thats the range analogWrite uses
LEDbrightness = map(photocellReading, 0, 1023, 0, 255);
analogWrite(LEDAnalog, [PURPLE], LEDbrightness);
delay(100);
}
If anyone could help me get this simple script working it would be amazing!
Thankyou
Zana