element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Arduino Loop Help Needed
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Suggested Answer
  • Replies 1 reply
  • Answers 1 answer
  • Subscribers 387 subscribers
  • Views 599 views
  • Users 0 members are here
  • arduino uno rev3
  • program
  • im an idiot
Related

Arduino Loop Help Needed

slackerisme
slackerisme over 5 years ago

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);

}

}

  • Sign in to reply
  • Cancel

Top Replies

  • Gough Lui
    Gough Lui over 5 years ago +2 suggested
    Next time, it may help to place the code in a special code block by using the Insert -> Syntax Highlighting -> c++ feature. Your code seems to have a lot of strange stylistic issues including inconsistent…
Parents
  • Gough Lui
    0 Gough Lui over 5 years ago

    Next time, it may help to place the code in a special code block by using the Insert -> Syntax Highlighting -> c++ feature. Your code seems to have a lot of strange stylistic issues including inconsistent indenting, spaces, etc. Regardless, from my understanding, you've got a traffic-light style light which is active-low that you are controlling to display a four different sequences (normal, power outage, normal, late night) repeatedly, but only if the lights are "on" in the shop (i.e. LDR senses light above a threshold).

     

    The problem is that, in the way you have structured your code, the Arduino is only going to read the photocell once every time it finishes the full sequence due to the way you have wrapped the whole block inside an "if" statement. This is probably not what you intended - instead you probably wanted the photocell to be checked fairly often so that once the shop lights go out, the traffic lights go out soon thereafter.

     

    There are many different approaches you can take - one of them is to embed the check for the photodiode value "all over the place" (e.g. at every place where the lights might be turned on) - probably best to wrap it into a function to save repetition. This has the advantage of relatively fast response, but also results in spaghetti code and perhaps more delays than is necessary.

     

    Instead, I suggest that you remove the initial read for the photodiode at the beginning and instead move the photodiode check into your while loops, so that they "break out" in case of light exceeding the given value -

    e.g. while (var<25 && analogRead(photocellPin)>200) where the && operation means a "logical and". The problem will be that you will need also to cater for the situation where the photocellPin value goes high mid-loop and a light is stuck on - hence leaving the off code in the loop should help. In good programming style, I've redefined the threshold number using a #define so that it can be changed in the header just once, and changed the indenting to be a little more comprehensible (at least, to me). As I don't have your equipment, I have no idea how this code will behave but perhaps you can give it a try:

    #define PHOTOCELL_THRESHOLD 200
    
    // 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!!!
    
    // 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() {
      // sets initial value for pins so that lights start as "off"
      digitalWrite(green, HIGH);
      digitalWrite(yellow, HIGH);
      digitalWrite(red, HIGH);
    
      var = 0;
      while(var < 25 && analogRead(photocellPin) > PHOTOCELL_THRESHOLD){
        // 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 && analogRead(photocellPin) > PHOTOCELL_THRESHOLD) {
        // 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 && analogRead(photocellPin) > PHOTOCELL_THRESHOLD){
        // 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 && analogRead(photocellPin) > PHOTOCELL_THRESHOLD) {
        digitalWrite(yellow, LOW);
        delay(600);
        digitalWrite(yellow, HIGH);
        delay(400);
        var1++;
      }
    }

     

    Also note that in your code, you don't actually need both var and var1 - they could have both been var as you don't use both concurrently. The one potential downside is that this version does not print the photocell value over serial anymore. If you want to do this, you could instead add the code to read at the end of the while loops instead - e.g.:

    # Must add int val = 0; declaration at the beginning
    # Prior to commencing the loop, val must contain present photodiode reading - i.e. val=analogRead(photocellPin); at a minimum
    
      while(var < 25 && val > PHOTOCELL_THRESHOLD){
        // 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++;
        val = analogRead(photocellPin);   // read the value from the sensor  
        Serial.println(val);   //The serial will print the light value
      }  //adds 1 to variable "var" for repeat count

    There is potential for a lot of improvement in the code, but at least I hope this code is closer to what you were hoping to have.

     

    - Gough

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
Reply
  • Gough Lui
    0 Gough Lui over 5 years ago

    Next time, it may help to place the code in a special code block by using the Insert -> Syntax Highlighting -> c++ feature. Your code seems to have a lot of strange stylistic issues including inconsistent indenting, spaces, etc. Regardless, from my understanding, you've got a traffic-light style light which is active-low that you are controlling to display a four different sequences (normal, power outage, normal, late night) repeatedly, but only if the lights are "on" in the shop (i.e. LDR senses light above a threshold).

     

    The problem is that, in the way you have structured your code, the Arduino is only going to read the photocell once every time it finishes the full sequence due to the way you have wrapped the whole block inside an "if" statement. This is probably not what you intended - instead you probably wanted the photocell to be checked fairly often so that once the shop lights go out, the traffic lights go out soon thereafter.

     

    There are many different approaches you can take - one of them is to embed the check for the photodiode value "all over the place" (e.g. at every place where the lights might be turned on) - probably best to wrap it into a function to save repetition. This has the advantage of relatively fast response, but also results in spaghetti code and perhaps more delays than is necessary.

     

    Instead, I suggest that you remove the initial read for the photodiode at the beginning and instead move the photodiode check into your while loops, so that they "break out" in case of light exceeding the given value -

    e.g. while (var<25 && analogRead(photocellPin)>200) where the && operation means a "logical and". The problem will be that you will need also to cater for the situation where the photocellPin value goes high mid-loop and a light is stuck on - hence leaving the off code in the loop should help. In good programming style, I've redefined the threshold number using a #define so that it can be changed in the header just once, and changed the indenting to be a little more comprehensible (at least, to me). As I don't have your equipment, I have no idea how this code will behave but perhaps you can give it a try:

    #define PHOTOCELL_THRESHOLD 200
    
    // 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!!!
    
    // 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() {
      // sets initial value for pins so that lights start as "off"
      digitalWrite(green, HIGH);
      digitalWrite(yellow, HIGH);
      digitalWrite(red, HIGH);
    
      var = 0;
      while(var < 25 && analogRead(photocellPin) > PHOTOCELL_THRESHOLD){
        // 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 && analogRead(photocellPin) > PHOTOCELL_THRESHOLD) {
        // 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 && analogRead(photocellPin) > PHOTOCELL_THRESHOLD){
        // 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 && analogRead(photocellPin) > PHOTOCELL_THRESHOLD) {
        digitalWrite(yellow, LOW);
        delay(600);
        digitalWrite(yellow, HIGH);
        delay(400);
        var1++;
      }
    }

     

    Also note that in your code, you don't actually need both var and var1 - they could have both been var as you don't use both concurrently. The one potential downside is that this version does not print the photocell value over serial anymore. If you want to do this, you could instead add the code to read at the end of the while loops instead - e.g.:

    # Must add int val = 0; declaration at the beginning
    # Prior to commencing the loop, val must contain present photodiode reading - i.e. val=analogRead(photocellPin); at a minimum
    
      while(var < 25 && val > PHOTOCELL_THRESHOLD){
        // 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++;
        val = analogRead(photocellPin);   // read the value from the sensor  
        Serial.println(val);   //The serial will print the light value
      }  //adds 1 to variable "var" for repeat count

    There is potential for a lot of improvement in the code, but at least I hope this code is closer to what you were hoping to have.

     

    - Gough

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
Children
No Data
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube