celem icon

ldrtest.ino

celem | PRO | 12/03/12 08:26:17 PM UTC | 0 ⭐ | 621 👁️ | Never ⏰ | []
C |

2.4 KB

|

None

|

0 👍

/

0 👎

/* 
** Plot photoresistor(LDR) response curve
** Edward I. Comer December 2012
 */
// Defines
#define DELAYTIME 10000     // Delay 10 seconds
#define MAXLIGHTLEVEL 25    // Max brightness level
#define STARTLIGHTLEVEL 9      // Starting point
#define INCREMENT 1     // increment to boost light
 
// constants won't change. Used here to 
// set pin numbers:
const int localLEDPin =  13;         // the number of the LED pin
const int ldrPin = 1;                // pin number with LDR
const int lightPin = 3;              // pin for flashlight
int LightLevel = STARTLIGHTLEVEL;    // light brightness 0-to-255
int ldrValue = 0;                    // Value read
 
void setup() {
  // set the digital pin as output:
  pinMode(localLEDPin, OUTPUT); // set pin 13 as output
  Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
//  Serial.println("ldrtest.ino running");
//  Serial.println("waiting 60 seconds");
  analogWrite(lightPin,0);    // settle at starting light level
  delay(10000);     // wait 10 secs for LDR to settle
//  delay(30000);       // wait 30 secs for LDR to settle
  //Serial.println("\"Light value\",\"LDR value\"");
}
 
void shutdown()
{
    analogWrite(lightPin,0);    // Turn light off
    Serial.print("END");
    Serial.write(0x0a);         // newline character for Linux
    // Rapid blink indicates completion of test
    while(1){  // endless loop
        digitalWrite(localLEDPin, HIGH);   // sets the LED on
    delay(500);                  // waits for a second
    digitalWrite(localLEDPin, LOW);    // sets the LED off
    delay(500);                  // waits for a second
  }
}
 
void loop()
{
    // Turn on local LED to indicate test start
    digitalWrite(localLEDPin, HIGH);        // local LED on
    analogWrite(lightPin,LightLevel);   // set light brightness
    delay(DELAYTIME);                   // wait for LDR to settle
    ldrValue = analogRead(ldrPin);      // read LDR value
    // Turn on local LED to indicate test stopped
    digitalWrite(localLEDPin, LOW);     // local LED off
 
        Serial.print(LightLevel, DEC);      // Print CSV format X-axis
        //Serial.print(", ");
        Serial.print(" ");
        Serial.print(ldrValue, DEC);        // Print CSV format Y-axis
        Serial.write(0x0a);                 // newline character for Linux
    
    LightLevel += INCREMENT;            // increment light brightness
    if(LightLevel > MAXLIGHTLEVEL){
        shutdown();
    }
    LightLevel &= 255;                  // limit variable to 1-byte
}

Comments