Simple question - reversing?

Hi everyone,

I have a simple question:

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?

Thanks.

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.

PaulS:

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\
outputValue = map(sensorValue, 0, 1023, 0, 255);  
  // change the analog out value:
  analogWrite(analogOutPin, outputValue);
outputValue = sensorValue / 4;  
  // change the analog out value:
  analogWrite(analogOutPin, 255 - outputValue);

AWOL:

outputValue = map(sensorValue, 0, 1023, 0, 255);  

// change the analog out value:
  analogWrite(analogOutPin, outputValue);






outputValue = sensorValue / 4; 
  // change the analog out value:
  analogWrite(analogOutPin, 255 - outputValue);

Thanks for the reply.

This doesn't seem to work however - the LED remains bright and does not react to light condition at all?

Post your code and your schematic.

AWOL:
Post your code and your schematic.

 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

So what is the serial monitor showing?

The sensor value changes, but the output only goes from 1-9

See reply #7

Sorry, I'm a bit confused...That is what the serial monitor is showing?

You can see numbers in your serial monitor.
I can't see those numbers.

Something like this?

sensor = 38	 output = 9
sensor = 38	 output = 9
sensor = 34	 output = 8
sensor = 37	 output = 9
sensor = 32	 output = 8
sensor = 37	 output = 9
sensor = 36	 output = 9
sensor = 29	 output = 7
sensor = 12	 output = 3
sensor = 3	 output = 0
sensor = 3	 output = 0
sensor = 2	 output = 0
sensor = 3	 output = 0
sensor = 2	 output = 0
sensor = 4	 output = 1
sensor = 4	 output = 1
sensor = 13	 output = 3
sensor = 36	 output = 9
sensor = 36	 output = 9
sensor = 39	 output = 9
sensor = 32	 output = 8
sensor = 37	 output = 9
sensor = 37	 output = 9
sensor = 36	 output = 9
sensor = 39	 output = 9
sensor = 32	 output = 8
sensor = 37	 output = 9
sensor = 37	 output = 9
sensor = 36	 output = 9
sensor = 40	 output = 10
sensor = 33	 output = 8
sensor = 6	 output = 1
sensor = 4	 output = 1
sensor = 4	 output = 1
sensor = 3	 output = 0
sensor = 3	 output = 0
sensor = 3	 output = 0
sensor = 3	 output = 0
sensor = 3	 output = 0
sensor = 3	 output = 0
sensor = 3	 output = 0
sensor = 3	 output = 0
sensor = 3	 output = 0
sensor = 4	 output = 1
sensor = 3	 output = 0
sensor = 13	 output = 3
sensor = 5	 output = 1
sensor = 6	 output = 1
sensor = 10	 output = 2
sensor = 18	 output = 4
sensor = 12	 output = 3
sensor = 5	 output = 1
sensor = 5	 output = 1
sensor = 3	 output = 0
sensor = 4	 output = 1
sensor = 6	 output = 1
sensor = 7	 output = 1
sensor = 4	 output = 1
sensor = 4	 output = 1
sensor = 3	 output = 0
sensor = 4	 output = 1
sensor = 3	 output = 0
sensor = 4	 output = 1
sensor = 3	 output = 0
sensor = 8	 output = 2
sensor = 15	 output = 3
sensor = 25	 output = 6
sensor = 13	 output = 3
sensor = 5	 output = 1
sensor = 4	 output = 1
sensor = 4	 output = 1
sensor = 4	 output = 1
sensor = 4	 output = 1
sensor = 4	 output = 1
sensor = 4	 output = 1
sensor = 4	 output = 1
sensor = 4	 output = 1
sensor = 5	 output = 1
sensor = 4	 output = 1
sensor = 6	 output = 1
sensor = 17	 output = 4
sensor = 39	 output = 9
sensor = 32	 output = 8
sensor = 37	 output = 9
sensor = 38	 output = 9
sensor = 35	 output = 8
sensor = 39	 output = 9
sensor = 32	 output = 8
sensor = 37	 output = 9
sensor = 38	 output = 9
sensor = 35	 output = 8
sensor = 39	 output = 9
sensor = 32	 output = 8
sensor = 36	 output = 9
sensor = 38	 output = 9
sensor = 5	 output = 1
sensor = 5	 output = 1
sensor = 4	 output = 1
sensor = 4	 output = 1
sensor = 4	 output = 1
sensor = 4	 output = 1
sensor = 3	 output = 0
sensor = 4	 output = 1
sensor = 3	 output = 0
sensor = 4	 output = 1
sensor = 3	 output = 0
sensor = 4	 output = 1
sensor = 4	 output = 1
sensor = 4	 output = 1
sensor = 4	 output = 1
sensor = 5	 output = 1
sensor = 33	 output = 8
sensor = 39	 output = 9
sensor = 33	 output = 8
sensor = 36	 output = 9
sensor = 38	 output = 9
sensor = 35	 output = 8
sensor = 39	 output = 9
sensor = 34	 output = 8
sensor = 36	 output = 9
sensor = 39	 output = 9
sensor = 34	 output = 8
sensor = 39	 output = 9
sensor = 34	 output = 8
sensor = 36	 output = 9
sensor = 14	 output = 3
sensor = 4	 output = 1
sensor = 4	 output = 1
sensor = 3	 output = 0
sensor = 4	 output = 1
sensor = 3	 output = 0
sensor = 3	 output = 0
sensor = 3	 output = 0
sensor = 4	 output = 1
sensor = 4	 output = 1
sensor = 4	 output = 1
sensor = 5	 output = 1
sensor = 23	 output = 5
sensor = 33	 output = 8
sensor = 39	 output = 9
sensor = 36	 output = 9
sensor = 36	 output = 9
sensor = 39	 output = 9
sensor = 33	 output = 8
sensor = 39	 output = 9
sensor = 36	 output = 9
sensor = 35	 output = 8
sensor = 6	 output = 1
sensor = 4	 output = 1
sensor = 4	 output = 1
sensor = 3	 output = 0
sensor = 4	 output = 1
sensor = 3	 output = 0
sensor = 3	 output = 0
sensor = 3	 output = 0
sensor = 4	 output = 1
sensor = 3	 output = 0
sensor = 4	 output = 1
sensor = 4	 output = 1
sensor = 5	 output = 1
sensor = 9	 output = 2
sensor = 39	 output = 9
sensor = 33	 output = 8
sensor = 38	 output = 9
sensor = 36	 output = 9
sensor = 34	 output = 8
sensor = 39	 output = 9
sensor = 33	 output = 8
sensor = 38	 output = 9
sensor = 36	 output = 9
sensor = 35	 output = 8
sensor = 14	 output = 3
sensor = 4	 output = 1
sensor = 4	 output = 1
sensor = 3	 output = 0

So, what is the sensor value for the brightest light source you have?

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.

With the brightest light source I have gets a reading of 1000?
However, it does go dimmer! So that's solved, thank you :slight_smile:

Another question though: how do I calibrate these values?
Also, how can you make the transition between light to dark smoother?
Like a fading effect?

Thanks!

how do I calibrate these 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?

PaulS:

how do I calibrate these 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).