Hello!
First off I am very new too Arduino so I'm not really too good at this. I need a photocell to make 2 Led's blink when its covered. Now with this code i got both Leds to turn on.
const int sensorPin = A0;
const int left = 13;
const int right = 12;
int sensorValue = 0;
int sensorMin = 1023;
int sensorMax = 0;
void setup() {
pinMode(left, OUTPUT);
pinMode(right, OUTPUT);
pinMode(sensorPin, INPUT);
}
void loop() {
sensorValue = analogRead(sensorPin);
sensorValue = map(sensorValue, sensorMin, sensorMax, 255, 0);
sensorValue = constrain(sensorValue, 255, 0);
analogWrite(left, sensorValue);
delay(500);
analogWrite(left, sensorValue);
analogWrite(right, sensorValue);
delay(250);
analogWrite(right, sensorValue);
}
I tried to use the delay to make it blink, but i dont think that is how it works, if anyone has any ideas of how to do this please help!!!
Rather than writing the photocell's value to the LED, I think you should try an approach like this:
In a simple sketch, print the value if the analogRead to Serial, then you'll see what values you get in various lightings, and then decide on your threshold for deciding it's "covered" or "open".
Then in an if statement, use that threshold as the test value, so that if the value is less than threshold do thing A, and if it's more that, do thing B.
By far the best way to blink LEDs (and by extension, control other things independently based on times) is the BlinkWithoutDelay approach, and all members here will encourage you to embrace that thinking. You would do the BlinkWithoutDelay stuff in one of the legs of the if statement.
Look at the code here, or in the IDE go File > Examples > Digital.
I find this video a good way to understand BlinkWithoutDelay.
@JimboZA okay well i made progress. I got it to blink without delay, and the serial to read the lightlevel but i dont know how to connect them. I need the photocell to make 2 Leds to blink one at 500 millis and another at 250 millis. I am completly stuck and have no clue what to do. sorry!
const int left = 13; //led pins
const int right = 12;
int sensorPin = A0;
int sensorValue = 0;
int minLight; //Serial intigers???
int maxLight;
int lightLevel;
int adjustedLightLevel;
int ledState = LOW;
long previousMillis = 0;
long interval = 1000;
void setup() {
Serial.begin(9600); //Set up Serial Reads
lightLevel=analogRead(sensorPin);
minLight=lightLevel-20;
maxLight=lightLevel;
pinMode(left, OUTPUT); //set Pin modes
pinMode(right, OUTPUT);
//pinMode(sensorPin, INPUT); (not sure if this is needed)
}
void loop(){
lightLevel=analogRead(sensorPin);
if(minLight>lightLevel){
minLight=lightLevel;
}
if(maxLight<lightLevel){
maxLight=lightLevel;
}
adjustedLightLevel = map(lightLevel, minLight, maxLight, 0, 100);
Serial.println(adjustedLightLevel);
//delay(50); //(not sure if this can be used here.)
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
digitalWrite(left, ledState);
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
digitalWrite(right, ledState);}
}
}
JimboZA:
Rather than writing the photocell's value to the LED, I think you should try an approach like this:
In a simple sketch, print the value if the analogRead to Serial, then you'll see what values you get in various lightings, and then decide on your threshold for deciding it's "covered" or "open".
Then in an if statement, use that threshold as the test value, so that if the value is less than threshold do thing A, and if it's more that, do thing B.
By far the best way to blink LEDs (and by extension, control other things independently based on times) is the BlinkWithoutDelay approach, and all members here will encourage you to embrace that thinking. You would do the BlinkWithoutDelay stuff in one of the legs of the if statement.
Look at the code here, or in the IDE go File > Examples > Digital.
I find this video a good way to understand BlinkWithoutDelay.
I'm on my way out soon for our local Saturday parkrun and some Saturday chores, but I'll give you a hand on that later.
Ok, here you go. Sorry, yesterday ran away from me.
I don't usually provide turnkey code, but I had this basically "on the shelf" and just had to add the LDR stuff.
This sketch puts all the BlinkWithoutDelay stuff into a function called bwod(), and since there are 2x LEDs I have bwod2() as well. (A more elegant, future version will be to pass the LED number to the function but that's for another day.)
At the top of the sketch you set the timings for the 2x LEDs. I cater for on and off times being different, but have set them the same here.
The LDR is wired in a voltage divider with the nalog reading taken from the point between the LDR and a 10k resistor. Then by printing the LDR value I decided on the threshold, and the "if" invokes the bwods depending on if we're over or under that threshold. Change the value in the line near the top depending on your lighting and individual LDR. In the "else" of the "if" I turn the LEDs off: they might have been in middle of an on stage when the LDR changed its mind.
So have a look at this and see if it makes sense:
/* Blink without Delay
by Jim.... this one puts bwod code in a function and
allows for differing off and on times
and for that to be with 2x independent LEDs
An LDR is used as a switch to blink or not to blink
Turns on and off a light emitting diode(LED) connected to a digital
pin, without using the delay() function. This means that other code
can run at the same time without being interrupted by the LED code.
The circuit:
* 2x LEDs attached from digital pin 11 and 12 to ground.
* LDR in a divider across 5V with 10k, analog pin 0
created 2005
by David A. Mellis
modified 8 Feb 2010
by Paul Stoffregen
and by JimboZa June 2015
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
*/
// constants won't change. Used here to
// set pin numbers:
const int ledPin = 11; // the number of the LED pin
const int led2Pin = 12;
const int ldrPin = 0; //analog
// and ldr threshold
const int ldrThreshold = 500;
// Variables will change:
//ldr
int ldrReading;
//led1
int ledState = LOW; // ledState used to set the LED
long previousMillis = 0; // will store last time LED was updated
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval; // interval at which to blink (milliseconds)
long onTime=500;
long offTime=500;
//led2
int led2State = LOW; // ledState used to set the LED
long previousMillis2 = 0; // will store last time LED was updated
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval2; // interval at which to blink (milliseconds)
long onTime2=250;
long offTime2=250;
void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
pinMode(led2Pin, OUTPUT);
//we'll print the ldr values to help choose threshold
//Serial.begin(9600);
}
void loop()
{
// here is where you'd put code that needs to be running all the time.
//and bwod() is where all the bwod stuff is done
// but we only do the bwod if ldr says so...
ldrReading = analogRead(ldrPin);
//Serial.println(ldrReading); //comment out when threshold is decided
if (ldrReading > ldrThreshold)
{
bwod();
bwod2();
}
else
{
//turn leds off in case they were in middle of an on
digitalWrite(ledPin, LOW);
digitalWrite(led2Pin, LOW);
}
}// end of loop
void bwod()
{
// check to see if it's time to blink the LED; that is, if the
// difference between the current time and last time you blinked
// the LED is bigger than the interval at which you want to
// blink the LED.
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
{
ledState = HIGH;
interval=onTime;
}
else
{
ledState = LOW;
interval=offTime;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
} //end bwod
void bwod2()
{
// check to see if it's time to blink the LED; that is, if the
// difference between the current time and last time you blinked
// the LED is bigger than the interval at which you want to
// blink the LED.
unsigned long currentMillis2 = millis();
if(currentMillis2 - previousMillis2 > interval2) {
// save the last time you blinked the LED
previousMillis2 = currentMillis2;
// if the LED is off turn it on and vice-versa:
if (led2State == LOW)
{
led2State = HIGH;
interval2=onTime2;
}
else
{
led2State = LOW;
interval2=offTime2;
}
// set the LED with the ledState of the variable:
digitalWrite(led2Pin, led2State);
}
}//end bwod2
Yes! It works! Thank you so much! Okay! So I pretty much understand all of the code up until you get into the bwod() I'm not quite sure what exactly the code inside means. I know that the explanation is there, but i don't quite get it. However! Now as far as the project goes, I need to get 3 more sensors to affect the leds. Would i Just run the other Ldr on separate pins and make the code accoringly or should i just splice the wires going to pin 1 and just have pin 1 run all of the Ldrs any ideas? (ps. Im sorry for my incompetence... im new at this.)
You're welcome...
The code inside the bwod() function, is really just the bwod code from here, bundled up in a function so that it can be repeated and called ad hoc and can control many LEDs independently.
For more LDRs, I'd be very inclined to put them on their own pins, then you can mix-n-match their results with ands and ors in the if statements. Something along the lines of:
if (LDR1_val > LDR1_threshold && LDR2_val < LDR2_threshold) //&& means "and"
{
// do certain stuff with the LEDs
}
else if (LDR3_val < LDR3_threshold || LDR4_val < LDR4_threshold) // || means "or"
{
// do different stuff
}
Read up on those && and || things here
[BTW]
This work prompted me to create a library to do bwod stuff over the weekend. I'm reluctant to post it yet, I need to tidy it up a bit. That will completely hide all the heavy lifting, and all you need to so is make an instance of a BWOD class, and give it the LED pin number and on and off times. There may well be similar libraries around already.....
[/BTW]
Okay. That actually makes sense. in the bwod void you basicy just defined what the bwod function is using the blink without delay code. I think I get it. Yeah I'm going to have to toy around with the the other Ldrs, see if i can figure some code to determine that when any of the 3 ldrs are covered the lights blink. I will probably get stuck again
but I'm going to have to learn the code some how!
heh sooo. I may be stuck again.
// constants won't change. Used here to
// set pin numbers:
const int ledPin = 11; // the number of the LED pin
const int led2Pin = 12;
const int ldrPin = 0; //analog
const int ldrPin2= 1;
const int ldrPin3= 2;
const int ldrPin4= 3;
// and ldr threshold
const int ldrThreshold = 500;
const int ldrThreshold2 = 500;
const int ldrThreshold3 = 500;
const int ldrThreshold4 = 500;
// Variables will change:
//ldr
int ldrReading;
int ldrReading2;
int ldrReading3;
int ldrReading4;
//led1
int ledState = LOW; // ledState used to set the LED
long previousMillis = 0; // will store last time LED was updated
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval; // interval at which to blink (milliseconds)
long onTime=500;
long offTime=500;
//led2
int led2State = LOW; // ledState used to set the LED
long previousMillis2 = 0; // will store last time LED was updated
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval2; // interval at which to blink (milliseconds)
long onTime2=250;
long offTime2=250;
void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
pinMode(led2Pin, OUTPUT);
//we'll print the ldr values to help choose threshold
//Serial.begin(9600);
}
void loop()
{
// here is where you'd put code that needs to be running all the time.
//and bwod() is where all the bwod stuff is done
// but we only do the bwod if ldr says so...
ldrReading = analogRead(ldrPin);
//Serial.println(ldrReading); //comment out when threshold is decided
if (ldrReading < ldrThreshold)
{
bwod();
bwod2();
}
else
{
//turn leds off in case they were in middle of an on
digitalWrite(ledPin, LOW);
digitalWrite(led2Pin, LOW);
}
ldrReading2 = analogRead(ldrPin2);
if (ldrReading2 < ldrThreshold2)
{
bwod();
bwod2();
}
else
{
digitalWrite(ledPin, LOW);
digitalWrite(led2Pin, LOW);
}
ldrReading3 = analogRead(ldrPin3);
if (ldrReading3 < ldrThreshold3)
{
bwod();
bwod2();
}
else
{
digitalWrite(ledPin, LOW);
digitalWrite(led2Pin, LOW);
}
ldrReading4 = analogRead(ldrPin4);
if (ldrReading4 < ldrThreshold4)
{
bwod();
bwod2();
}
else
{
digitalWrite(ledPin, LOW);
digitalWrite(led2Pin, LOW);
}
}// end of loop
void bwod()
{
// check to see if it's time to blink the LED; that is, if the
// difference between the current time and last time you blinked
// the LED is bigger than the interval at which you want to
// blink the LED.
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
{
ledState = HIGH;
interval=onTime;
}
else
{
ledState = LOW;
interval=offTime;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
} //end bwod
void bwod2()
{
// check to see if it's time to blink the LED; that is, if the
// difference between the current time and last time you blinked
// the LED is bigger than the interval at which you want to
// blink the LED.
unsigned long currentMillis2 = millis();
if(currentMillis2 - previousMillis2 > interval2) {
// save the last time you blinked the LED
previousMillis2 = currentMillis2;
// if the LED is off turn it on and vice-versa:
if (led2State == LOW)
{
led2State = HIGH;
interval2=onTime2;
}
else
{
led2State = LOW;
interval2=offTime2;
}
// set the LED with the ledState of the variable:
digitalWrite(led2Pin, led2State);
}
}//end bwod2
I want all 4 sensors to do the same thing that the 1st sensor did. When any of the 4 are covered to have the lights blink. It seems simple and I feel like the code I added should do that, I have them all wired the same but for some reason only the first sensor will affect anything. Im not sure what to do.
Every time a subsequent test fails, it switches them both off in that test's "else", I think.
Why don't you do all the readings one after the other and then have one "if" which will OR all the readings like this:
ldrReading = analogRead(ldrPin);
ldrReading2 = analogRead(ldrPin2);
//.... etc
if (ldrReading < ldrThreshold || ldrReading2 < ldrThreshold2 etc etc)
{
//do stuff
}
That way, which ever test is passed, the bwods kick in, and only if they ALL fail, switch the LEDs off.
Oh my gosh man your a life saver! I got it to work with this code
void loop()
{
// here is where you'd put code that needs to be running all the time.
//and bwod() is where all the bwod stuff is done
// but we only do the bwod if ldr says so...
ldrReading = analogRead(ldrPin);
ldrReading2= analogRead(ldrPin2);
ldrReading3= analogRead(ldrPin3);
ldrReading4= analogRead(ldrPin4);
//Serial.println(ldrReading); //comment out when threshold is decided
if (ldrReading < ldrThreshold || ldrReading2 < ldrThreshold2 || ldrReading3 < ldrThreshold3 || ldrReading4 < ldrThreshold4)
{
bwod();
bwod2();
}
else
{
//turn leds off in case they were in middle of an on
digitalWrite(ledPin, LOW);
digitalWrite(led2Pin, LOW);
}
}
Eventually i'm going to need to add..... two more Led's to this to blink at the same time... I...am not sure how thats going to work but Ill worry about that when I have too.
But seriously I want to thank you for all the help thus far. Not only has my project come a long way and is working but I have learned so much! So in the future I wont have to bother you about this. Your super awesome and really good at this stuff haha thanks again.
You're welcome...
Here's a debugging hint: use Serial.println("appropriate message of choice"); to get your code to tell you where it's gone. In the previous code with all those ifs and elses, you could have chucked a bunch of prints in liberally and then would have got a message like "in the ldrReading2 < ldrThreshold2 if" or "turning LEDs off in the ldrReading2 < ldrThreshold2 else part" kind of thing.
sorry if Im annoying but One last quick question. I decided to toy around with the 3rd and 4th Led. This code isnt working.
/* Blink without Delay
by Jim.... this one puts bwod code in a function and
allows for differing off and on times
and for that to be with 2x independent LEDs
An LDR is used as a switch to blink or not to blink
Turns on and off a light emitting diode(LED) connected to a digital
pin, without using the delay() function. This means that other code
can run at the same time without being interrupted by the LED code.
The circuit:
* 2x LEDs attached from digital pin 11 and 12 to ground.
* LDR in a divider across 5V with 10k, analog pin 0
created 2005
by David A. Mellis
modified 8 Feb 2010
by Paul Stoffregen
and by JimboZa June 2015
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
*/
// constants won't change. Used here to
// set pin numbers:
const int ledPin = 11; // the number of the LED pin
const int led2Pin = 12;
const int led3Pin = 10;
const int led4Pin = 9;
const int ldrPin = 0; //analog
const int ldrPin2= 1;
const int ldrPin3= 2;
const int ldrPin4= 3;
// and ldr threshold
const int ldrThreshold = 500;
const int ldrThreshold2 = 500;
const int ldrThreshold3 = 500;
const int ldrThreshold4 = 500;
// Variables will change:
//ldr
int ldrReading;
int ldrReading2;
int ldrReading3;
int ldrReading4;
//led1
int ledState = LOW; // ledState used to set the LED
long previousMillis = 0; // will store last time LED was updated
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval; // interval at which to blink (milliseconds)
long onTime=500;
long offTime=500;
//led2
int led2State = LOW; // ledState used to set the LED
long previousMillis2 = 0; // will store last time LED was updated
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval2; // interval at which to blink (milliseconds)
long onTime2=250;
long offTime2=250;
//led3
int led3State = LOW;
long previousMillis3 = 0;
long interval3;
long onTime3 =500;
long offTime3 =500;
//led4
int led4State = LOW;
long previousMillis4 = 0;
long interval4;
long onTime4 = 250;
long offTime4 = 250;
void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
pinMode(led2Pin, OUTPUT);
//we'll print the ldr values to help choose threshold
//Serial.begin(9600);
}
void loop()
{
// here is where you'd put code that needs to be running all the time.
//and bwod() is where all the bwod stuff is done
// but we only do the bwod if ldr says so...
ldrReading = analogRead(ldrPin);
ldrReading2= analogRead(ldrPin2);
ldrReading3= analogRead(ldrPin3);
ldrReading4= analogRead(ldrPin4);
//Serial.println(ldrReading); //comment out when threshold is decided
if (ldrReading < ldrThreshold || ldrReading2 < ldrThreshold2 || ldrReading3 < ldrThreshold3 || ldrReading4 < ldrThreshold4)
{
bwod();
bwod2();
bwod3();
bwod4();
}
else
{
//turn leds off in case they were in middle of an on
digitalWrite(ledPin, LOW);
digitalWrite(led2Pin, LOW);
digitalWrite(led3Pin, LOW);
digitalWrite(led4Pin, LOW);
}
}// end of loop
void bwod()
{
// check to see if it's time to blink the LED; that is, if the
// difference between the current time and last time you blinked
// the LED is bigger than the interval at which you want to
// blink the LED.
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
{
ledState = HIGH;
interval=onTime;
}
else
{
ledState = LOW;
interval=offTime;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
} //end bwod
void bwod2()
{
// check to see if it's time to blink the LED; that is, if the
// difference between the current time and last time you blinked
// the LED is bigger than the interval at which you want to
// blink the LED.
unsigned long currentMillis2 = millis();
if(currentMillis2 - previousMillis2 > interval2) {
// save the last time you blinked the LED
previousMillis2 = currentMillis2;
// if the LED is off turn it on and vice-versa:
if (led2State == LOW)
{
led2State = HIGH;
interval2=onTime2;
}
else
{
led2State = LOW;
interval2=offTime2;
}
// set the LED with the ledState of the variable:
digitalWrite(led2Pin, led2State);
}
}//end bwod2
void bwod3()
{
unsigned long currentMillis3 = millis();
if(currentMillis3 - previousMillis3 > interval3) {
previousMillis3 = currentMillis3;
if (led3State == LOW)
{
led3State = HIGH;
interval3=onTime3;
}
else
{
led3State = LOW;
interval3=offTime3;
}
digitalWrite(led3Pin, led3State);
}
}
void bwod4()
{
unsigned long currentMillis4 = millis();
if(currentMillis4 - previousMillis4 > interval4) {
previousMillis4 = currentMillis4;
if (led4State == LOW)
{
led4State = HIGH;
interval3=onTime4;
}
else
{
led4State = LOW;
interval4=offTime4;
}
digitalWrite(led4Pin, led4State);
}
}
I made a 3rd and 4th bwod, shouldn't it technically work the same way? by just adding a 3rd and 4th bwod(), pin 9 and 10 as the leds, the on and offs ect shouldn't it work like the 1st and 2nd? why isnt this working. its probably simple and Im just overlooking it. Some sort of overlap of code or just simply something i missed. thanks haha.
You didn't set the 3rd and 4th LED pins as output, and iirc, pins default to inputs 
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
pinMode(led2Pin, OUTPUT);
// <<<<<<<<<<<<<<<<<<<<<<<<< add 2 more
OH MY GOSH! Im such an idiot
Alright thank you a TON! Its now fully working thanks to you :).This has helped so much. Thanks again!
I have adapted those messy bwod() functions to a library, which makes it easier to implement: same code, it's just hidden in a library. Come the weekend I'll test it more and publish it: then you could incorporate that into your code simply to make the sketch cleaner. Library is done and working, but I want to just tidy it up a bit and put some comments in.
This code, as an example, sets up 2x BWODs with just a few lines. The functionality's in the library...
/* Blink without Delay in a class in a library
*/
#include <BWOD.h>
BWOD myBWOD;
BWOD anotherBWOD;
void setup() {
myBWOD.attach(8, 97, 110); //pin, on interval, off interval
anotherBWOD.attach(14, 1798, 1015);
}
void loop()
{
myBWOD.manageBlink();
anotherBWOD.manageBlink();
}
ooookay. so im not sure if your still viewing this forum but I need help again. I upgraded my board to the mega2560 and I need to get an additional 7 sensors (11 in total) to run off of the same board but I need LDR's 1-4 to activate 4 leds and LDR's 5-11 to activate a separate 4 leds. I'm fairly sure this code that I just finished should work, but I'm getting a weird error saying i haven't declared LedPin 6-8 even though I have! I've been looking over this code for a good 2 hours trying to figure out what I did wrong but I can't come up with anything.... any ideas?
const int ledPin = 11; // the number of the LED pin (Switches)
const int led2Pin = 12;
const int led3Pin = 10;
const int led4Pin = 9;
const int led5Pin = 8;
const int led6pin = 7;
const int led7pin = 6;
const int led8pin = 5;
const int ldrPin = 0; //analog (LDR's)
const int ldrPin2= 1;
const int ldrPin3= 2;
const int ldrPin4= 3;
const int ldrPin5= 4;
const int ldrPin6= 5;
const int ldrPin7= 6;
const int ldrPin8= 7;
const int ldrPin9= 8;
const int ldrPin10= 9;
const int ldrPin11= 10;
// and ldr threshold
const int ldrThreshold = 500;
const int ldrThreshold2 = 500;
const int ldrThreshold3 = 500;
const int ldrThreshold4 = 500;
const int ldrThreshold5 = 500;
const int ldrThreshold6 = 500;
const int ldrThreshold7 = 500;
const int ldrThreshold8 = 500;
const int ldrThreshold9 = 500;
const int ldrThreshold10 = 500;
const int ldrThreshold11 = 500;
// Variables will change:
//ldr
int ldrReading;
int ldrReading2;
int ldrReading3;
int ldrReading4;
int ldrReading5;
int ldrReading6;
int ldrReading7;
int ldrReading8;
int ldrReading9;
int ldrReading10;
int ldrReading11;
//led1
int ledState = LOW; // ledState used to set the LED
long previousMillis = 0; // will store last time LED was updated
long interval; // interval at which to blink (milliseconds)
long onTime=500;
long offTime=500;
//led2
int led2State = LOW; // ledState used to set the LED
long previousMillis2 = 0; // will store last time LED was updated
long interval2; // interval at which to blink (milliseconds)
long onTime2=250;
long offTime2=250;
//led3
int led3State = LOW;
long previousMillis3 = 0;
long interval3;
long onTime3 =500;
long offTime3 =500;
//led4
int led4State = LOW;
long previousMillis4 = 0;
long interval4;
long onTime4 = 250;
long offTime4 = 250;
//led 5
int led5State = LOW;
long previousMillis5 = 0;
long interval5;
long onTime5 = 250;
long offTime5 = 250;
//led 6
int led6State = LOW;
long previousMillis6 = 0;
long interval6;
long onTime6 = 500;
long offTime6 = 500;
//led 7
int led7State = LOW;
long previousMillis7 = 0;
long interval7;
long onTime7 = 500;
long offTime7 = 500;
//led 8
int led8State = LOW;
long previousMillis8 = 0;
long interval8;
long onTime8 = 250;
long offTime8 = 250;
void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
pinMode(led2Pin, OUTPUT);
pinMode(led3Pin, OUTPUT);
pinMode(led4Pin, OUTPUT);
pinMode(led5Pin, OUTPUT);
pinMode(led6Pin, OUTPUT);
pinMode(led7Pin, OUTPUT);
pinMode(led8Pin, OUTPUT);
}
void loop()
{
ldrReading = analogRead(ldrPin);
ldrReading2= analogRead(ldrPin2);
ldrReading3= analogRead(ldrPin3);
ldrReading4= analogRead(ldrPin4);
ldrReading5= analogRead(ldrPin5);
ldrReading6= analogRead(ldrPin6);
ldrReading7= analogRead(ldrPin7);
ldrReading8= analogRead(ldrPin8);
ldrReading9= analogRead(ldrPin9);
ldrReading10= analogRead(ldrPin10);
ldrReading11= analogRead(ldrPin11);
//Serial.println(ldrReading); //comment out when threshold is decided
if (ldrReading < ldrThreshold || ldrReading2 < ldrThreshold2 || ldrReading3 < ldrThreshold3 || ldrReading4 < ldrThreshold4)
{
bwod();
bwod2();
bwod3();
bwod4();
}
else
{
//turn leds off in case they were in middle of an on
digitalWrite(ledPin, LOW);
digitalWrite(led2Pin, LOW);
digitalWrite(led3Pin, LOW);
digitalWrite(led4Pin, LOW);
}
if (ldrReading5 < ldrThreshold5 || ldrReading6 < ldrThreshold6 || ldrReading7 < ldrReading7 || ldrReading8 < ldrReading8 || ldrReading9 < ldrReading9 || ldrReading10 < ldrReading10 || ldrReading11 < ldrReading11)
{
bwod5();
bwod6();
bwod7();
bwod8();
}
else
{
digitalWrite(ledPin5, LOW);
digitalWrite(ledPin6, LOW);
digitalWrite(ledPin7, LOW);
digitalWrite(ledPin8, LOW);
}
}// end of loop
void bwod()
{
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
{
ledState = HIGH;
interval=onTime;
}
else
{
ledState = LOW;
interval=offTime;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
} //end bwod
void bwod2()
{
unsigned long currentMillis2 = millis();
if(currentMillis2 - previousMillis2 > interval2) {
// save the last time you blinked the LED
previousMillis2 = currentMillis2;
// if the LED is off turn it on and vice-versa:
if (led2State == LOW)
{
led2State = HIGH;
interval2=onTime2;
}
else
{
led2State = LOW;
interval2=offTime2;
}
// set the LED with the ledState of the variable:
digitalWrite(led2Pin, led2State);
}
}//end bwod2
void bwod3()
{
unsigned long currentMillis3 = millis();
if(currentMillis3 - previousMillis3 > interval3) {
previousMillis3 = currentMillis3;
if (led3State == LOW)
{
led3State = HIGH;
interval3=onTime3;
}
else
{
led3State = LOW;
interval3=offTime3;
}
digitalWrite(led3Pin, led3State);
}
}
void bwod4()
{
unsigned long currentMillis4 = millis();
if(currentMillis4 - previousMillis4 > interval4) {
previousMillis4 = currentMillis4;
if (led4State == LOW)
{
led4State = HIGH;
interval4=onTime4;
}
else
{
led4State = LOW;
interval4=offTime4;
}
digitalWrite(led4Pin, led4State);
}
}
void bwod5()
{
unsigned long currentMillis5 = millis();
if(currentMillis5 - previousMillis5 > interval5) {
previousMillis5 = currentMillis5;
if (led5State == LOW)
{
led5State = HIGH;
interval5=onTime5;
}
else
{
led4State = LOW;
interval5=offTime5;
}
digitalWrite(led5Pin, led5State);
}
}
void bwod6()
{
unsigned long currentMillis6 = millis();
if(currentMillis6 - previousMillis6 > interval6) {
previousMillis6 = currentMillis6;
if (led6State == LOW)
{
led6State = HIGH;
interval6=onTime6;
}
else
{
led6State = LOW;
interval6=offTime6;
}
digitalWrite(led6Pin, led6State);
}
}
void bwod7()
{
unsigned long currentMillis7 = millis();
if(currentMillis7 - previousMillis7 > interval7) {
previousMillis7 = currentMillis7;
if (led7State == LOW)
{
led7State = HIGH;
interval7=onTime7;
}
else
{
led7State = LOW;
interval7=offTime7;
}
digitalWrite(led7Pin, led7State);
}
}
void bwod8()
{
unsigned long currentMillis8 = millis();
if(currentMillis8 - previousMillis8 > interval8) {
previousMillis8 = currentMillis8;
if (led8State == LOW)
{
led8State = HIGH;
interval8=onTime8;
}
else
{
led8State = LOW;
interval8=offTime8;
}
digitalWrite(led8Pin, led8State);
}
}