Hello,
I'm trying to code a program made up of borrowed code form different folks, I have the parts working separately, so I know its me that is the issue. What happens, code runs, I get one serial port reading displayed and th rest of the code functions. What I desire, the code to check if the light is on to prevent freaking out my neighbors with flashing lights in the shop.
I assume I need to nest the loop somehow, beyond that my experience is nil.
Thank you.
// name your pins:
int photocellPin = A0; // Photoresistor at Arduino analog pin A0
int red = 12; //stop
int yellow = 11; // floor it
int green = 10; //Go!!!
int val = 0;
// the setup routine runs once when you press reset:
void setup() {
Serial.begin(9600);
// initialize the digital pin as an output.
pinMode(red, OUTPUT);
pinMode(yellow, OUTPUT);
pinMode(green, OUTPUT);
pinMode(photocellPin, INPUT);// Set pResistor - A0 pin as an input
}
int var = 0; //defines and sets initial value for variables used below
int var1 = 0; //defines and sets initial value for variables used below
// the loop routine runs over and over again forever:
void loop() {
val = analogRead(photocellPin); // read the value from the sensor
Serial.println(val); //The serial will print the light value
// sets initial value for pins so that lights start as "off"
digitalWrite(green, HIGH);
digitalWrite(yellow, HIGH);
digitalWrite(red, HIGH);
if (val > 200){
while(var < 25){
// repeats normal cycle 25 times
digitalWrite(green, LOW); // turns the green light on
delay(20000); // holds the green light on for 20 seconds
digitalWrite(green, HIGH); // turns the green light off
delay(600); // slight pause between lights
digitalWrite(yellow, LOW); //turns the yellow light on
delay(4000); //holds the yellow light for 4 seconds (watch out for that red-light camera!)
digitalWrite(yellow, HIGH); //turns the yellow light off
delay(600); //slight pause between lights
digitalWrite(red, LOW); //turns the red light on
delay(20000); //holds the red light on for 20 seconds
digitalWrite(red, HIGH); //turns the red light off
delay(600); //slight pause between lights
var++;} //adds 1 to variable "var" for repeat count
// after 25 cycles above, the light switches to "power outage mode", flashing red
delay(600); //slight delay
var1=0; //resets variable "var1" to 0
while(var1 < 120) {
// repeats power outage cycle 120 times - 2 minutes
digitalWrite(red, LOW);
delay(600);
digitalWrite(red, HIGH);
delay(400);
var1++;}
var = 0;
//switches back to normal cycle after "power outage" cycle is done
while(var < 25){
// back to normal light cycle for 25 cycles
digitalWrite(green, LOW); // turn the LED on (HIGH is the voltage level)
delay(20000); // wait for a second
digitalWrite(green, HIGH); // turn the LED off by making the voltage LOW
delay(600); // wait for a second
digitalWrite(yellow, LOW);
delay(4000);
digitalWrite(yellow, HIGH);
delay(600);
digitalWrite(red, LOW);
delay(20000);
digitalWrite(red, HIGH);
delay(600);
var++;}
delay(600);
//switches to "late night cycle" flashing yellow for 2 minutes, similar to flashing red above
var1=0;
while(var1 < 120) {
digitalWrite(yellow, LOW);
delay(600);
digitalWrite(yellow, HIGH);
delay(400);
var1++;}
var = 0;
}
else{
digitalWrite(green, HIGH);
digitalWrite(yellow, HIGH);
digitalWrite(red, HIGH);
}
}