Basically I'm making an LED strip react to light (when it's light it is dim/off, when it's dark it's bright).
I tried with the "example" sketches (the AnalogInOutSerial one), but I can only get it to do the opposite of what I want.
What part of the code do I change in order to "reverse" it?
What part of the code do I change in order to "reverse" it?
The value used as the second argument to the digitalWrite() function. Use (255-val), instead of val, to "reverse" the normal behavior.
I'm sorry, I can't seem to find it?
This example code is in the public domain.
*/
// These constants won't change. They're used to give names
// to the pins used:
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to
int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}
void loop() {
// read the analog in value:
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 0, 255);
// change the analog out value:
analogWrite(analogOutPin, outputValue);
// print the results to the serial monitor:
Serial.print("sensor = " );
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);
// wait 2 milliseconds before the next loop
// for the analog-to-digital converter to settle
// after the last reading:
delay(2); p\
This example code is in the public domain.
*/
// These constants won't change. They're used to give names
// to the pins used:
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to
int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}
void loop() {
// read the analog in value:
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
outputValue = sensorValue / 4;
// change the analog out value:
analogWrite(analogOutPin, 255 - outputValue);
// print the results to the serial monitor:
Serial.print("sensor = " );
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);
// wait 2 milliseconds before the next loop
// for the analog-to-digital converter to settle
// after the last reading:
delay(2);
}
Schematically it is the same as this
but with this light sensor module instead of the potentiometer Light Sensor Module | Freetronics
The sensor values look pretty low for a device that is supposed to be capable of returning values in the range 0 to 1023. The output values look consistent, though, with the input.
You might try removing the divide by 4 part, to get a greater range of PWM values.
First, you have a known light source. Using that, you can determine the sensor reading at several known settings.
But, why? What is the purpose of "calibration" when all you seem to want is to make the LED brighter as the light level gets brighter?
So that I don't need to shine the external light source on it?
With normal room lights on, the LED is still bright, and doesn't react to something like my hand waving over it (which is ultimately what I want to achieve - the LED brightens when there is darkness), but at the moment because the values are so extreme it doesn't do that?
You need to determine the reasonable range of values that the sensor will return, given normal lighting levels. Then, use constrain() and map() to assure that the actual value is always in the normal range, and to convert the normal range to a usable range (0 to 255).