element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • 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 & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • 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
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • 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 Sketch Question?
  • 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 3 replies
  • Answers 1 answer
  • Subscribers 401 subscribers
  • Views 349 views
  • Users 0 members are here
  • i/o
  • source_code
  • buttons
  • loop
  • sketch
  • program
  • programming
  • code
  • array
  • arduino
Related

Arduino Sketch Question?

fechrxy
fechrxy over 12 years ago

So I have a sketch that using an array, digitally reads five pins connected to buttons then writes to the serial monitor whethor or not they are on or off. But when i turn the switch on or off it won't just write On/Off once it will write it alot of times because its part of a loop. Does any one know how to make the sketch below only print On/Off once or basically how to make something not loop in a loop? Maybe with a boolean? Sorry i ask so many questions, thanks!

 

 

 

 

 

  • Sign in to reply
  • Cancel

Top Replies

  • Former Member
    Former Member over 12 years ago in reply to fechrxy +1
    Tony, You have LastButtonState[ThisPin] = ButtonState[ThisPin]; in the "else" part, but it looks like you should be setting LastButtonState regardless of whether ButtonState is HIGH or LOW. That would…
Parents
  • ntewinkel
    0 ntewinkel over 12 years ago

    Hi Tony!

     

    No worries, I think questions are a big reason these forums exist image

     

     

    I don't see your sketch attached, but to show a status change only once in a loop you can keep a variable that holds the latest status, and if it's different from the current status, print the message and update the variable.

     

    kinda like this (loosely based on the Button example)...

     

    int lastbuttonstate = 0;

    int newbuttonstate = 0;

     

    loop {

     

      newbuttonstate = digitalRead(buttonPin); // probably needs some debouncing

        

       if (newbuttonstate != lastbuttonstate) {  

     

         if (newbuttonstate == HIGH) {

              Serial.println("ON");

          }

          else {

              Serial.println("OFF");

         }

     

         lastbuttonstate = newbuttonstate;

       }

     

    }

     

    I think that will do the trick!

     

    ps, how did your relay project turn out?

     

    Cheers,

    -Nico

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • fechrxy
    0 fechrxy over 12 years ago in reply to ntewinkel

    Thanks for the response! I put in your code and this is definitely the closest it has been to working. However it still has a few problems like when I hit/hold any one of the buttons it says "on on on on on on" and when I release the button it does not say off. Those are the only issues I can foresee at the moment so if you know of any fix for them I would love to hear it. And my relay project is going great! I am almost ready to install  the project into my home lighting system. Thanks so much for all your help!

     

     

    static byte ButtonPins[5] = {2, 3, 4, 5, 6};

    const int PinCount = (5);

    int ThisPin;

     

     

    void setup()

    {

      Serial.begin(9600);

     

      for (ThisPin = 0; ThisPin < PinCount; ThisPin++)

      {

      pinMode(ButtonPins[ThisPin], INPUT); 

      }

    }

     

     

     

     

    void loop()

    {

    static byte ButtonState[PinCount];

    static byte LastButtonState[PinCount];

     

    for (ThisPin = 0; ThisPin < PinCount; ThisPin++)

    {

      ButtonState[ThisPin] = (digitalRead(ButtonPins[ThisPin]));

     

    if (ButtonState[ThisPin] != LastButtonState[ThisPin])

      {

     

    if (ButtonState[ThisPin] == HIGH)

        {

      Serial.println(ThisPin, DEC);

      Serial.println(" ON");

      }

        

    else

       {

       Serial.println(ThisPin, DEC);

       Serial.println(" OFF");

       LastButtonState[ThisPin] = ButtonState[ThisPin];

       }

       }

       }

    }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 12 years ago in reply to fechrxy

    Tony,

        You have

           LastButtonState[ThisPin] = ButtonState[ThisPin];

    in the "else" part, but it looks like you should be setting

    LastButtonState regardless of whether ButtonState is HIGH or LOW.

     

    That would involve just moving it down a line after the first "}",

    like Nico's code.  You also probably want to initialize LastButtonState

    to 0, like Nico showed.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Reply
  • Former Member
    0 Former Member over 12 years ago in reply to fechrxy

    Tony,

        You have

           LastButtonState[ThisPin] = ButtonState[ThisPin];

    in the "else" part, but it looks like you should be setting

    LastButtonState regardless of whether ButtonState is HIGH or LOW.

     

    That would involve just moving it down a line after the first "}",

    like Nico's code.  You also probably want to initialize LastButtonState

    to 0, like Nico showed.

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