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
Cypress Kits
  • Products
  • Dev Tools
  • Cypress Kits
  • More
  • Cancel
Cypress Kits
Forum Blink LED-while loop
  • Blog
  • Forum
  • Documents
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Cypress Kits to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Verified Answer
  • Replies 2 replies
  • Answers 1 answer
  • Subscribers 26 subscribers
  • Views 895 views
  • Users 0 members are here
Related

Blink LED-while loop

Former Member
Former Member over 11 years ago

I'm trying to Blink and LED while Im holding a switch on the board, my initial code for blinking when the switch is on works fine. but now I'm trying to blink it while I'm holding the switch here is what I have for code, Can any one help point out where my error is, Thanks

 

 

#include <project.h>

int main()

{

   

    uint8 SW_Stat; 

  

    for(;;)

    {

     

       

        SW_Stat=(SW1_Read());

        while(SW_Stat==1)

            LED_Write(1);

            CyDelay(500);

            LED_Write(0);

            CyDelay(350);

            LED_Write(1);

            CyDelay(260);

        return 0;

       

        }

}

  • Sign in to reply
  • Cancel
Parents
  • johnbeetem
    0 johnbeetem over 11 years ago

    There are several problems.

     

    First, the C compiler doesn't care about indentation.  It only cares about braces.  So your code:

     

    while(SW_Stat==1)

          LED_Write(1);

          CyDelay(500);

          LED_Write(0);

          CyDelay(350);

          LED_Write(1);

          CyDelay(260);

     

    is going to be interpreted as:

     

    while (SW_Stat==1) {LED_Write(1);}

    CyDelay(500);

    LED_Write(0);

    CyDelay(350);

    LED_Write(1);

    CyDelay(260);

     

    That is, the body of the while loop is the single statement LED_Write(1).

     

    The second problem is that LED_Write(1) doesn't affect SW_Stat, so once SW_Stat == 1, the while loop will never end and the LED will be stuck on.

     

    The third problem is that "return 0" exits main(), ending execution.  Usually embedded programs are infinite loops and execute until someone powers off the device or hits the reset button.  So you don't usually return from main().

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

    There are several problems.

     

    First, the C compiler doesn't care about indentation.  It only cares about braces.  So your code:

     

    while(SW_Stat==1)

          LED_Write(1);

          CyDelay(500);

          LED_Write(0);

          CyDelay(350);

          LED_Write(1);

          CyDelay(260);

     

    is going to be interpreted as:

     

    while (SW_Stat==1) {LED_Write(1);}

    CyDelay(500);

    LED_Write(0);

    CyDelay(350);

    LED_Write(1);

    CyDelay(260);

     

    That is, the body of the while loop is the single statement LED_Write(1).

     

    The second problem is that LED_Write(1) doesn't affect SW_Stat, so once SW_Stat == 1, the while loop will never end and the LED will be stuck on.

     

    The third problem is that "return 0" exits main(), ending execution.  Usually embedded programs are infinite loops and execute until someone powers off the device or hits the reset button.  So you don't usually return from main().

    • 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