// Constants
#define DELAY 500 // Delay between two measurements in ms
#define VIN 5 // V power voltage
#define R 10000 //ohm resistance value
// set pin numbers:
const int buttonPin1 = 4; // the number of the pushbutton1 pin
const int buttonPin2 = A0; // the number of the pushbutton2 pin
const int buttonPin3 = 7; // the number of the pushbutton3 pin
const int ledPin1 = 8; // the number of the LED1 anode(+) pin
const int ledPin2 = 12; // the number of the LED2 anode(+) pin
// Variables will change:
int ledState1 = LOW; // the current state of the LED1
int ledState2 = LOW; // the current state of the LED2
int buttonState1; // the current reading from the input pin1
int buttonState2; // the current reading from the input pin2
int buttonState3; // the current reading from the input pin3
int lastButtonState1 = LOW; // the previous reading from the input pin1
int lastButtonState2 = LOW; // the previous reading from the input pin2
int lastButtonState3 = LOW; // the previous reading from the input pin3
// the following variables are long's because the time, measured in microseconds,
// will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 20000; // the debounce time in micro second
unsigned long randNumber; // Generated random number
unsigned long minRandomNumber = 2000; // minimum number used to specify the range of random number
unsigned long maxRandomNumber = 5000; // maximum number used to specify the range of random number
unsigned long time1,time2;
int button3Pressed = LOW;
int printcount = 0;
int takeReading = LOW;
//following variable help in reading buttons pins
int reading1;
int reading2;
int reading3;
int lux;
//following variable help in reading button corresponding to perticular led i.e LED1 --> BUTTON1 and LED2 --> BUTTON2
int oddNumber ;
int evenNumber ;
void setup()
{
pinMode(buttonPin2, INPUT);
pinMode(ledPin1, OUTPUT);
//enabling serial communication
Serial.begin(9600);
// set initial LED state
digitalWrite(ledPin1, ledState1);
// if analog input pin 0 is unconnected, random analog
// noise will cause the call to randomSeed() to generate
// different seed numbers each time the sketch runs.
// randomSeed() will then shuffle the random function.
randomSeed(analogRead(0));
}
void loop()
{
reading1 = analogRead(A0);
lux=sensorRawToPhys(reading1);
Serial.print("Raw value from sensor= ");
Serial.println(reading1); // the analog reading
Serial.print("Physical value from sensor = ");
Serial.print(lux); // the analog reading
Serial.println(" lumen"); // the analog reading
if (lux > 50)
{
digitalWrite(ledPin1, LOW);
}
else
{
digitalWrite(ledPin1, HIGH);
}
delay(DELAY);
}
int sensorRawToPhys(int raw){
// Conversion rule
float Vout = float(raw) * (VIN / float(1023));// Conversion analog to voltage
float RLDR = (R * (VIN - Vout))/Vout; // Conversion voltage to resistance
int phys=500/(RLDR/1000); // Conversion resitance to lumen
return phys;
}