Stopwatch using a photoresistor

Hello everybody,
I would like to create a stopwatch using my Arduino Uno.

The program is very simple, the chronometer will have to start only when a photoresistor will detect a light source. The program will have to record how long this light will remain on and at the end report the result via the serial monitor (only at the end). Also, to see if the code works correctly, I would like a LED to remain ON when the stopwatch is actually recording the passage of time.

This is the code I tried to write by my own.

int UVOUT = A0; //Output from the sensor
int x = 400;
int pinLed = 2;
unsigned long time;

void setup() {
Serial.begin(9600);

pinMode(UVOUT, INPUT);
pinMode(pinLed, OUTPUT);

Serial.println("UV Stopwatch");
}

void loop() {
int uvLevel = analogRead(UVOUT);

if (uvLevel > x){

digitalWrite(pinLed, HIGH);
Serial.println("UV Detection - Started");
time = millis();
}

Serial.print("Total Time: ");
Serial.println(time);
}

If anyone could give me some advice or help me with the drafting of the code it would be much appreciated !

Thx in advance,
Giacomo.

something like this (compiles but not tested)?

int UVOUT = A0; //Output from the sensor
int x = 400;
int pinLed = 2;
unsigned long oldtime;
int UV_Detected = 0;

void setup() {
  Serial.begin(9600);

  pinMode(UVOUT, INPUT);
  pinMode(pinLed, OUTPUT);
  digitalWrite(pinLed, LOW);

  Serial.println("UV Stopwatch");
}


void loop() {
  int uvLevel = analogRead(UVOUT);

  if (uvLevel > x) {

    digitalWrite(pinLed, HIGH);
    oldtime = millis();
    Serial.println("UV Detection - Started");
    Serial.print("Total Time: ");
    UV_Detected = 1;
  }

  //continue checking is input is above threshold
  while (uvLevel > x) {
    uvLevel = analogRead(UVOUT);
  }

  if (UV_Detected == 1) {
    Serial.println(millis() - oldtime);
    digitalWrite(pinLed, LOW);
    UV_Detected = 0;
  }
}

hope that helps! :wink:

sherzaad:
something like this (compiles but not tested)?

int UVOUT = A0; //Output from the sensor

int x = 400;
int pinLed = 2;
unsigned long oldtime;
int UV_Detected = 0;

void setup() {
 Serial.begin(9600);

pinMode(UVOUT, INPUT);
 pinMode(pinLed, OUTPUT);
 digitalWrite(pinLed, LOW);

Serial.println("UV Stopwatch");
}

void loop() {
 int uvLevel = analogRead(UVOUT);

if (uvLevel > x) {

digitalWrite(pinLed, HIGH);
   oldtime = millis();
   Serial.println("UV Detection - Started");
   Serial.print("Total Time: ");
   UV_Detected = 1;
 }

//continue checking is input is above threshold
 while (uvLevel > x) {
   uvLevel = analogRead(UVOUT);
 }

if (UV_Detected == 1) {
   Serial.println(millis() - oldtime);
   digitalWrite(pinLed, LOW);
   UV_Detected = 0;
 }
}




hope that helps! ;)

Hi sherzaad,
thank you for your fast reply and for this code revision !

I have two other questions regarding the sensor module I have to use.

I don't need to detect the wavelenght of my light (UV), just if it's ON or OFF, can I use a normal light sensor (GL5528 photoresistor or LM393 module for example) or I have to use a specific UV detector like ML8511 ?

Morover,I noticed that many of the common uv sensors for arduino have a maximum peak of 370nm, the light source I use varies from 390nm to 470nm. Not having to detect the wavelength of the light but only if it is on or off, is it possible to use one of these sensors or would it not detect the presence of a uv light with a longer wavelength ?

Thank you again !

Giacomo