<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="bbPress/1.0.2" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>LeafLabs Garden &#187; Topic: Button example program on mini</title>
		<link>http://forums.leaflabs.com/topic.php?id=851</link>
		<description>A place to share, learn, and grow...</description>
		<language>en-US</language>
		<pubDate>Fri, 22 Jan 2016 00:18:56 +0000</pubDate>
		<generator>http://bbpress.org/?v=1.0.2</generator>
		<textInput>
			<title><![CDATA[Search]]></title>
			<description><![CDATA[Search all topics from these forums.]]></description>
			<name>q</name>
			<link>http://forums.leaflabs.com/search.php</link>
		</textInput>
		<atom:link href="http://forums.leaflabs.com/rss.php?topic=851" rel="self" type="application/rss+xml" />

		<item>
			<title>robodude666 on "Button example program on mini"</title>
			<link>http://forums.leaflabs.com/topic.php?id=851#post-5137</link>
			<pubDate>Sat, 11 Jun 2011 08:31:23 +0000</pubDate>
			<dc:creator>robodude666</dc:creator>
			<guid isPermaLink="false">5137@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;&#60;code&#62;isButtonPressed()&#60;/code&#62; is simply poorly named and its functionality is therefore misleading. If you were to use Objective-C's verbose naming scheme it should be called &#60;code&#62;wasButtonPressedAndThenReleased()&#60;/code&#62;. In other words:&#60;/p&#62;
&#60;pre&#62;&#60;code&#62;uint8 isButtonPressed() {
    if (digitalRead(BOARD_BUTTON_PIN)) {
        delay(BUTTON_DEBOUNCE_DELAY);
        while (digitalRead(BOARD_BUTTON_PIN))
            ;
        return true;
    }
    return false;
}&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;It checks to see if the button was pressed. It then waits a debounce period (1ms), and waits for the button to be released. You may want to try this function for your application:&#60;/p&#62;
&#60;pre&#62;&#60;code&#62;bool isButtonDown()
{
    if (digitalRead(BOARD_BUTTON_PIN))
	{
        delay(1); // 1ms debounce perioud
        return digitalRead(BOARD_BUTTON_PIN) ? true : false;
    }
    return false;
}&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;This &#60;code&#62;isButtonDown()&#60;/code&#62; function simply checks to see if the button is down after a debounce period. With this new function, your code should work as expected:&#60;/p&#62;
&#60;pre&#62;&#60;code&#62;void loop()
{

	if(isButtonDown()) // Is the Button down?
	{
		toggleLED(); // Toggle the LED
		while(isButtonDown()); // Wait for button to be released
	}
}&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;The last &#60;code&#62;while(isButtonDown());&#60;/code&#62; is required or else &#60;code&#62;isButtonDown()&#60;/code&#62; will continuously return &#60;code&#62;true&#60;/code&#62; if the button remains down. The while statement, as you previously expected it to, waits for the button to be released. In addition, because of this new &#34;release&#34; check no debounce delay is required in your application itself.&#60;/p&#62;
&#60;p&#62;-robodude666
&#60;/p&#62;</description>
		</item>
		<item>
			<title>Darth Maker on "Button example program on mini"</title>
			<link>http://forums.leaflabs.com/topic.php?id=851#post-5133</link>
			<pubDate>Sat, 11 Jun 2011 00:30:56 +0000</pubDate>
			<dc:creator>Darth Maker</dc:creator>
			<guid isPermaLink="false">5133@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;The button example program does not work properly.  While you hold down the button, the LED oscillates off and on rapidly.  I suppose it is possible that that is proper operation, it would make more sense to have the switch to the opposite state when the button is pressed.  &#60;/p&#62;
&#60;p&#62;This is true in release 0.0.11, using a brand spanking new Maple Mini.&#60;/p&#62;
&#60;p&#62;This should be fixed with the simple addition of a while(isButtonPressed()); statement inside the if block.  However, this does not seem to help, even with a 1ms delay added for debounce.&#60;/p&#62;
&#60;p&#62;Ok, I added a 10ms delay, and it seems to work properly now, except that the toggle is happening when I release the button, instead on press like it should.&#60;/p&#62;
&#60;p&#62;&#60;code&#62;void setup() {&#60;br /&#62;
  // Initialize the built-in LED pin as an output:&#60;br /&#62;
  pinMode(BOARD_LED_PIN, OUTPUT);&#60;br /&#62;
  // Initialize the built-in button (labeled BUT) as an input:&#60;br /&#62;
  pinMode(BOARD_BUTTON_PIN, INPUT);&#60;br /&#62;
}&#60;/p&#62;
&#60;p&#62;void loop() {&#60;br /&#62;
    // Check if the button is pressed.&#60;br /&#62;
    if (isButtonPressed())&#60;br /&#62;
    {&#60;br /&#62;
        // If so, turn the LED from on to off, or from off to on:&#60;br /&#62;
        toggleLED();&#60;/p&#62;
&#60;p&#62;        while(isButtonPressed());&#60;br /&#62;
        delay(10);&#60;br /&#62;
    }&#60;br /&#62;
}&#60;/code&#62;
&#60;/p&#62;</description>
		</item>

	</channel>
</rss>
