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 problem with if else to run different sequences
  • 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 Verified Answer
  • Replies 2 replies
  • Subscribers 391 subscribers
  • Views 324 views
  • Users 0 members are here
Related

problem with if else to run different sequences

peteypieca
peteypieca over 10 years ago

I am new to Arduino. I have a GE Fanuc PLC that controls the LEDs on my motorcycle. It runs through six different sequences. The problem is it only has six outputs. I want to replace it with an Arduino UNO. I have the Knight rider sketch which I am building off of. I have added two other flashing sequences and have each selected by different combinations of two switches. My problem is the first ( Knight rider) sequence works, but when I change the switches all sequences seem to run instead of only the one which is selected. The IF / ELSE statements do not seem to be working.

Here is a copy of my code. Thanks for any help that you can give.

 

//LED Sequencer with variable speed and program/sequence select

 

int prog1=13;

int prog2=12;

int speedpot0 = 0;

int speedpot1 = 1;

int pinArray[] = {1, 2, 3, 4, 5, 6, 7, 8,9,10,11};

int count = 0;

int timer = 30;

int timer2 = 1000;

int timer3 = 50;

int timer4 = 250;

int led1=1;

int led2=2;

int led3=3;

int led4=4;

int led5=5;

int led6=6;

int led7=7;

int led8=8;

int led9=9;

int led10=10;

int led11=11;

 

 

 

 

 

 

 

 

void setup(){

pinMode (prog1, INPUT);

pinMode (prog2, INPUT);

 

   for (count=0;count<11;count++) {

    pinMode(pinArray[count], OUTPUT);

      }

}

 

 

void loop() {

 

if (digitalRead(prog1)==HIGH && digitalRead(prog2)==LOW)

  {

  int val0 = analogRead(speedpot0);

  val0 = constrain(val0, 100,900);

  int timer = map(val0,100, 900, 0, 50);

 

   analogWrite(speedpot0, timer);

 

  for (count=0;count<10;count++) {

   digitalWrite(pinArray[count], HIGH);

   delay(timer);

   digitalWrite(pinArray[count + 1], HIGH);

   delay(timer);

   digitalWrite(pinArray[count], LOW);

   delay(timer*2);

  }

  for (count=10;count>0;count--) {

   digitalWrite(pinArray[count], HIGH);

   delay(timer);

   digitalWrite(pinArray[count - 1], HIGH);

   delay(timer);

   digitalWrite(pinArray[count], LOW);

   delay(timer*2);

  }

}

 

 

else if (digitalRead(prog1)==LOW && digitalRead(prog2)== HIGH);

  {

      int val1 = analogRead(speedpot1);

      val1 = constrain(val1, 100,900);

      int timer3 = map(val1,100, 900, 0, 1000);

    analogWrite (speedpot1, timer3);

    digitalWrite (led1, HIGH);

    digitalWrite (led2, HIGH);

    digitalWrite (led3, HIGH);

    digitalWrite (led4, HIGH);

    digitalWrite (led5, HIGH);

    digitalWrite (led6, HIGH);

    digitalWrite (led7, HIGH);

    digitalWrite (led8, HIGH);

    digitalWrite (led9, HIGH);

    digitalWrite (led10, HIGH);

    digitalWrite (led11, HIGH);

 

 

  delay(timer3);

 

    digitalWrite (led1, LOW);

    digitalWrite (led2, LOW);

    digitalWrite (led3, LOW);

    digitalWrite (led4, LOW);

    digitalWrite (led5, LOW);

    digitalWrite (led6, LOW);

    digitalWrite (led7, LOW);

    digitalWrite (led8, LOW);

    digitalWrite (led9, LOW);

    digitalWrite (led10, LOW);

    digitalWrite (led11, LOW);

   

    delay(timer3); }

 

  

       if (digitalRead(prog1)==HIGH && digitalRead(prog2)== HIGH);

    {

     int val1 = analogRead(speedpot1);

      val1 = constrain(val1, 100,900);

      int timer3 = map(val1,100, 900, 0, 1000);

    analogWrite (speedpot1, timer3);

   

    digitalWrite (led1, HIGH);

    digitalWrite (led2, HIGH);

    digitalWrite (led3, HIGH);

    digitalWrite (led4, HIGH);

    digitalWrite (led5, HIGH);

    digitalWrite (led6, LOW);

    digitalWrite (led7, LOW);

    digitalWrite (led8, LOW);

    digitalWrite (led9, LOW);

    digitalWrite (led10, LOW);

   digitalWrite (led11, LOW);

 

 

delay(timer3);

 

   digitalWrite (led1, LOW);

   digitalWrite (led2, LOW);

   digitalWrite (led3, LOW);

   digitalWrite (led4, LOW);

   digitalWrite (led5, LOW);

   digitalWrite (led6, HIGH);

   digitalWrite (led7, HIGH);

   digitalWrite (led8, HIGH);

   digitalWrite (led9, HIGH);

   digitalWrite (led10, HIGH);

   digitalWrite (led11, HIGH);

   

   delay(timer3); }

 

 

   

      if (digitalRead(prog1)== LOW && digitalRead(prog2) == LOW);

  {  digitalWrite (led1, HIGH);

  digitalWrite (led2, HIGH);

    digitalWrite (led3, HIGH);

    digitalWrite (led4, HIGH);

    digitalWrite (led5, HIGH);

    digitalWrite (led6, HIGH);

    digitalWrite (led7, HIGH);

    digitalWrite (led8, HIGH);

    digitalWrite (led9, HIGH);

    digitalWrite (led10, HIGH);

    digitalWrite (led11, HIGH); }

 

 

}

  • Sign in to reply
  • Cancel
Parents
  • xever
    0 xever over 10 years ago

    Hi Robert,

     

    if/else block do work, however, your code has a trivial bug such that you put a semi-colon at the end of the else if block.  in one of your lines you have the following:


    else if (digitalRead(prog1)==LOW && digitalRead(prog2)== HIGH);


    as you can see you have semi-colon at the end of this line, this will be interpreted similarly as:


    else if (digitalRead(prog1)==LOW && digitalRead(prog2)== HIGH)

    {

        ;

    }


    which basically does nothing and so your code will flow onto the next line regardless the condition.


    Another issue would be your if-else blocks.  I would recommend though that you functionalise your code like:

    void loop() 
    {
        if (digitalRead(prog1) == HIGH && digitalRead(prog2) == LOW)
        {
            led_sequence1();
        }
        else if (digitalRead(prog1) == LOW && digitalRead(prog2) == HIGH)
        {
            led_sequence2();
        }
        else if (digitalRead(prog1) == HIGH && digitalRead(prog2) == HIGH)
        {
            led_sequence3();
        }
        else (digitalRead(prog1) == LOW && digitalRead(prog2) == LOW)
        {  
            led_sequence4();
        }
    }

     

    where each led_sequence is the block of code you have as above.  Having this code structure would make debugging easier and have your code a bit more readable. image  Hope this is helpful. 

     

    Also, I think it would also be helpful if you read/learn the basics of C or C++, as that would accelerate your understanding on how code flows. image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Reject Answer
    • Cancel
Reply
  • xever
    0 xever over 10 years ago

    Hi Robert,

     

    if/else block do work, however, your code has a trivial bug such that you put a semi-colon at the end of the else if block.  in one of your lines you have the following:


    else if (digitalRead(prog1)==LOW && digitalRead(prog2)== HIGH);


    as you can see you have semi-colon at the end of this line, this will be interpreted similarly as:


    else if (digitalRead(prog1)==LOW && digitalRead(prog2)== HIGH)

    {

        ;

    }


    which basically does nothing and so your code will flow onto the next line regardless the condition.


    Another issue would be your if-else blocks.  I would recommend though that you functionalise your code like:

    void loop() 
    {
        if (digitalRead(prog1) == HIGH && digitalRead(prog2) == LOW)
        {
            led_sequence1();
        }
        else if (digitalRead(prog1) == LOW && digitalRead(prog2) == HIGH)
        {
            led_sequence2();
        }
        else if (digitalRead(prog1) == HIGH && digitalRead(prog2) == HIGH)
        {
            led_sequence3();
        }
        else (digitalRead(prog1) == LOW && digitalRead(prog2) == LOW)
        {  
            led_sequence4();
        }
    }

     

    where each led_sequence is the block of code you have as above.  Having this code structure would make debugging easier and have your code a bit more readable. image  Hope this is helpful. 

     

    Also, I think it would also be helpful if you read/learn the basics of C or C++, as that would accelerate your understanding on how code flows. image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • 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