Skip to content

Instantly share code, notes, and snippets.

@lisa
Created October 19, 2009 01:52
Show Gist options
  • Save lisa/213017 to your computer and use it in GitHub Desktop.
Save lisa/213017 to your computer and use it in GitHub Desktop.
#define SENSORPIN 0
#define PWMPIN 3
int sensorMax = -1;
int sensorMin = 1024;
void setup() { }
void loop() {
delay(10);
analogWrite(PWMPIN,readLDRValue());
}
/*
* Returns the value of the LDR on the scale of 0-255
* Needs defined:
* * SENSORPIN (pin onto which LDR is connected)
* * PWMPIN (PWM pin onto which the LED/output is connected)
*
*/
int readLDRValue() {
int sensorValue = 0; // variable to store the value coming from the sensor
sensorValue = map(analogRead(SENSORPIN),0,1023,0,255);
if (sensorValue > sensorMax)
sensorMax = sensorValue;
if (sensorValue < sensorMin)
sensorMin = sensorValue;
return sensorValue;
}
@cgrobbelaar
Copy link

Hi Lisa,
Just want to check if the lines should not have the value assignment swapped around?:
if (sensorValue > sensorMax)
sensorValue = sensorMax;
if (sensorValue < sensorMin)
sensorValue = sensorMin;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment