Coding for light sensor project

The "Eleven" is our Uno-equivalent Arduino-compatible board, but with a number of improvements including prototyping area, a mini-USB connector, LEDs mounted near the edge, and the D13 LED isolated using a FET. [Product page]
Post Reply
Jeremyf
Posts: 2
Joined: Sat Apr 02, 2016 11:12 am

Coding for light sensor project

Post by Jeremyf » Sat Apr 02, 2016 11:22 am

Hi,

I have a small problem with a "while" statement - how do I end it?

The idea is that the light (an LED for now) turns on when it goes dark, it stays on for 5 seconds and I want it to stay off until it resets once it gets light again.

When the sketch gets to the while statement it stops measuring/printing in serial monitor (highligthed in red text) I must need to stop the while statement but I don't know how to and can't find the solution (or maybe can but don't understand it).

can anyone offer a solution?

many Thanks.

JF

Sketch:

Code: Select all

// Define analog pin
int sensorPin = 0;
int lightPin = 2;

// Variables
int lightState = 0;
int lowThreshold = 10;
int highTreshold = 40;

// Setup
void setup() {
 // Init serial
 Serial.begin(9600);
 pinMode(lightPin,OUTPUT);
 
}
// Main loop
void loop() {
 // Get temperature
 int sensorValue = analogRead(sensorPin);
 // Put temperature on the serial port
 Serial.println(sensorValue,DEC);
 
 if (sensorValue < lowThreshold){
    digitalWrite(lightPin, HIGH);
    delay (5000);
    digitalWrite(lightPin, LOW);
  [color=#FF0000]  while (sensorValue < lowThreshold);[/color]
     
// need to put in code so that until light is restored it won't go back to low state and light stays off
 
 if (sensorValue > highTreshold) 
    digitalWrite(lightPin, LOW);
  }
 
 // Wait for 1 sec
 delay(1000);
}

andrew
Freetronics Staff
Freetronics Staff
Posts: 978
Joined: Sun Jul 14, 2013 7:06 am
Location: Melbourne, Australia
Contact:

Re: Coding for light sensor project

Post by andrew » Sun Apr 03, 2016 5:33 am

Try the following sketch. When the light level is below 10 the LED turns on for five seconds then turns off.

Code: Select all

// Define analogue pin
int sensorPin = 0;
int lightPin = 2;

// Variables
int lightState = 0;
int lowThreshold = 10;
int highTreshold = 40;

// Setup
void setup() {
  // Init serial
  Serial.begin(9600);
  pinMode(lightPin, OUTPUT);

}

// Main loop
void loop() 
{
  // Get temperature
  int sensorValue = analogRead(sensorPin);
  
  // Put temperature on the serial port
  Serial.println(sensorValue, DEC);

  if (sensorValue < lowThreshold)  // if dark
  {
    digitalWrite(lightPin, HIGH);
    delay (5000);
    digitalWrite(lightPin, LOW);
   }

  // Wait for 1 sec
  delay(1000);
}


Jeremyf
Posts: 2
Joined: Sat Apr 02, 2016 11:12 am

Re: Coding for light sensor project

Post by Jeremyf » Mon Apr 04, 2016 10:28 am

Hi Andrew,

Thanks, you're correct it does turn off, then waits 1 second and comes back on for 5 seconds etc.

What I want is for it to turn off and stay off until the light sensor "sees" the light again.

The application is for a dawn/dusk sensor on a solar system. Eventually the LED will be replaced by a light and there will be some other programming and circuitry to work out whether there's been enough solar to turn the light back on the next day and how long to turn it on for to use about 5-10% of the stored capacity.

I have a solar panel that benefits from generating rather then trickle charging an already well charged battery.

If I was doing this in BASIC (showing how long it's been since I did any programming) I would use a nested if/loop that when the high level was reached would GOTO the beginning but I can't seem to find out how to isolate the program criteria and then move it to the next stage. Apologies for dodgy terminology but hope you can see what I'm trying to do.

JF

Post Reply