<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Electronics Hobbyist &#187; button</title>
	<atom:link href="http://www.theelectronicshobbyist.com/blog/tag/button/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.theelectronicshobbyist.com/blog</link>
	<description>A Passion for Curiosity and Play</description>
	<lastBuildDate>Fri, 20 Jan 2012 14:54:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Arduino 2-Digit 7-Segment Display with Buttons: Sketch &#124; Part 5</title>
		<link>http://www.theelectronicshobbyist.com/blog/arduino-2-digit-7-segment-display-with-buttons-sketch/</link>
		<comments>http://www.theelectronicshobbyist.com/blog/arduino-2-digit-7-segment-display-with-buttons-sketch/#comments</comments>
		<pubDate>Tue, 29 Jun 2010 05:00:26 +0000</pubDate>
		<dc:creator>Natalia</dc:creator>
				<category><![CDATA[Arduino]]></category>
		<category><![CDATA[Project]]></category>
		<category><![CDATA[7-segment display]]></category>
		<category><![CDATA[button]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[sketch]]></category>

		<guid isPermaLink="false">http://www.theelectronicshobbyist.com/blog/?p=280</guid>
		<description><![CDATA[As you could see from last week&#8217;s full Arduino sketch listing, the source code for the 2-digit 7-segment display project using buttons is strikingly similar to the one without the buttons; praise for &#8216;copy and paste&#8216;! (It is worth noting, though, that &#8216;copy and paste&#8216; can be responsible for a higher percentage of bugs than [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>As you could see from last week&#8217;s full <a href="http://www.cutedigi.com/product_info.php?ref=3&#038;products_id=4221&#038;affiliate_banner_id=1" target="_blank">Arduino</a> sketch listing, the source code for the 2-digit 7-segment display project using buttons is strikingly similar to the one without the buttons; praise for &#8216;<em>copy and paste</em>&#8216;! (It is worth noting, though, that &#8216;<em>copy and paste</em>&#8216; can be responsible for a higher percentage of bugs than I&#8217;d care to admit).</p>
<p>There are just a couple of snippets that I would like to comment on:</p>
<p>The first part of the loop() function checks whether either button has been pressed and increments the value of each digit.<br />
<span id="more-280"></span></p>
<pre>  // check button1
  int val1 = digitalRead(BTN1);
  if (val1 == HIGH) {
    digit1++;
    digit1 %= 10;
    delay(10);
  }

  // check button2
  int val2 = digitalRead(BTN2);
  if (val2 == HIGH) {
    digit2++;
    digit2 %= 10;
    delay(10);
  }</pre>
<p>The line</p>
<pre>    digit1 %= 10;</pre>
<p>which is shorthand for</p>
<pre>    digit1 = digit1 % 10;</pre>
<p>accomplishes the same as the line</p>
<pre>    if (count == 10) count = 0;</pre>
<p>that was used on the <a href="http://www.theelectronicshobbyist.com/blog/2010/02/controlling-a-seven-segment-display-using-arduino-part-4-of-4/">sketch for the single-digit 7-segment project</a> a couple of months ago. It updates digit1 with the remainder of the division of itself by 10, which will be zero when digit1 is 10. The latter code looks a bit more obvious for beginner programmers.</p>
<p>The last part of the loop() function refreshes the display, and is very similar to the <a href="http://www.theelectronicshobbyist.com/blog/2010/06/arduino-2-digit-7-segment-display-counter-sketch/">sketch for the 2-digit 7-segment display counter</a>.</p>
<pre>  // display number
  unsigned long startTime = millis();
  for (unsigned long elapsed=0; elapsed &lt; 600; elapsed = millis() - startTime) {
    lightDigit1(numbers[digit1]);
    delay(5);
    lightDigit2(numbers[digit2]);
    delay(5);
  }</pre>
<p>You might also enjoy:<ol>
<li><a href='http://www.theelectronicshobbyist.com/blog/arduino-2-digit-7-segment-display-counter-sketch/' rel='bookmark' title='Arduino 2-Digit 7-Segment Display Counter: Sketch | Part 3'>Arduino 2-Digit 7-Segment Display Counter: Sketch | Part 3</a></li>
<li><a href='http://www.theelectronicshobbyist.com/blog/arduino-2-digit-7-segment-display-with-buttons/' rel='bookmark' title='Arduino 2-Digit 7-Segment Display with Buttons | Part 4'>Arduino 2-Digit 7-Segment Display with Buttons | Part 4</a></li>
<li><a href='http://www.theelectronicshobbyist.com/blog/arduino-2-digit-7-segment-display-counter/' rel='bookmark' title='Arduino 2-Digit 7-Segment Display Counter | Part 1'>Arduino 2-Digit 7-Segment Display Counter | Part 1</a></li>
<li><a href='http://www.theelectronicshobbyist.com/blog/arduino-2-digit-7-segment-display-counter-circuit/' rel='bookmark' title='Arduino 2-Digit 7-Segment Display Counter: Circuit | Part 2'>Arduino 2-Digit 7-Segment Display Counter: Circuit | Part 2</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.theelectronicshobbyist.com/blog/arduino-2-digit-7-segment-display-with-buttons-sketch/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Arduino 2-Digit 7-Segment Display with Buttons &#124; Part 4</title>
		<link>http://www.theelectronicshobbyist.com/blog/arduino-2-digit-7-segment-display-with-buttons/</link>
		<comments>http://www.theelectronicshobbyist.com/blog/arduino-2-digit-7-segment-display-with-buttons/#comments</comments>
		<pubDate>Tue, 22 Jun 2010 05:00:42 +0000</pubDate>
		<dc:creator>Natalia</dc:creator>
				<category><![CDATA[Arduino]]></category>
		<category><![CDATA[Project]]></category>
		<category><![CDATA[7-segment display]]></category>
		<category><![CDATA[button]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[sketch]]></category>
		<category><![CDATA[video]]></category>

		<guid isPermaLink="false">http://www.theelectronicshobbyist.com/blog/?p=267</guid>
		<description><![CDATA[This week we modify the original circuit and sketch to include two buttons, one to control each digit of the display. Here&#8217;s what the setup looks like: And here&#8217;s the complete sketch: // www.TheElectronicsHobbyist.com/blog // Natalia Fargasch Norman // Dual seven-segment LED Display with buttons // Common Anode digit 1 pin 10 // Common Anode [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>This week we modify the original circuit and sketch to include two buttons, one to control each digit of the display.</p>
<p>Here&#8217;s what the setup looks like:</p>
<p><a href="http://www.theelectronicshobbyist.com/blog/wp-content/uploads/2010/06/seven-segment-button.jpg"><img style=' display: block; margin-right: auto; margin-left: auto;'  class="aligncenter size-medium wp-image-268" title="seven-segment-button" src="http://www.theelectronicshobbyist.com/blog/wp-content/uploads/2010/06/seven-segment-button-300x216.jpg" alt="2-Digit 7-Segment Display" width="300" height="216" /></a></p>
<p>And here&#8217;s the complete sketch:<br />
<span id="more-267"></span></p>
<pre>// www.TheElectronicsHobbyist.com/blog
// Natalia Fargasch Norman
// Dual seven-segment LED Display with buttons
// Common Anode digit 1 pin 10
// Common Anode digit 2 pin 5

//       CA1 G  F  A  B
//        |  |  |  |  |      -&gt; pins and segments they control
//   ---------    ---------
//   |   A   |    |   A   |
//  F|       |B  F|       |B
//   |---G---|    |---G---|
//  E|       |C  E|       |C
//   |   D   |    |   D   |
//   ---------    ---------
//        |  |  |  |  |      -&gt; pins and segments they control
//        D  DP E  C CA2         

// Segments that make each number when lit:
// 0 =&gt; -FEDCBA
// 1 =&gt; ----BC-
// 2 =&gt; G-ED-BA
// 3 =&gt; G--DCBA
// 4 =&gt; GF--CB-
// 5 =&gt; GF-DC-A
// 6 =&gt; GFEDC-A
// 7 =&gt; ----CBA
// 8 =&gt; GFEDCBA
// 9 =&gt; GF-DCBA

// Arduino digital pins used to light up
// corresponding segments on the LED display
#define A 3
#define B 2
#define C 6
#define D 8
#define E 7
#define F 4
#define G 5

// Pushbuttons connected to pins 9 and 10
#define BTN1 9
#define BTN2 10

// Pins driving common anodes
#define CA1 13
#define CA2 12

// Pins for A B C D E F G, in sequence
const int segs[7] = { A, B, C, D, E, F, G };

// Segments that make each number
const byte numbers[10] = { 0b1000000, 0b1111001, 0b0100100, 0b0110000, 0b0011001, 0b0010010,
0b0000010, 0b1111000, 0b0000000, 0b0010000 };

int digit1 = 0;
int digit2 = 0;

void setup() {
  pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(C, OUTPUT);
  pinMode(D, OUTPUT);
  pinMode(E, OUTPUT);
  pinMode(F, OUTPUT);
  pinMode(G, OUTPUT);
  pinMode(BTN1, INPUT);
  pinMode(BTN2, INPUT);
  pinMode(CA1, OUTPUT);
  pinMode(CA2, OUTPUT);
}

void loop() {

  // check button1
  int val1 = digitalRead(BTN1);
  if (val1 == HIGH) {
    digit1++;
    digit1 %= 10;
    delay(10);
  }

  // check button2
  int val2 = digitalRead(BTN2);
  if (val2 == HIGH) {
    digit2++;
    digit2 %= 10;
    delay(10);
  }

  // display number
  unsigned long startTime = millis();
  for (unsigned long elapsed=0; elapsed &lt; 600; elapsed = millis() - startTime) {
    lightDigit1(numbers[digit1]);
    delay(5);
    lightDigit2(numbers[digit2]);
    delay(5);
  }
}

void lightDigit1(byte number) {
  digitalWrite(CA1, LOW);
  digitalWrite(CA2, HIGH);
  lightSegments(number);
}

void lightDigit2(byte number) {
  digitalWrite(CA1, HIGH);
  digitalWrite(CA2, LOW);
  lightSegments(number);
}

void lightSegments(byte number) {
  for (int i = 0; i &lt; 7; i++) {
    int bit = bitRead(number, i);
    digitalWrite(segs[i], bit);
  }
}</pre>
<p>Here&#8217;s a video of the <a href="http://www.theelectronicshobbyist.com/blog/goto/uno" style="" target="_blank" rel="nofollow" onmouseover="self.status='http://www.theelectronicshobbyist.com/blog/goto/uno';return true;" onmouseout="self.status=''">Arduino</a> <a href="http://www.youtube.com/watch?v=NgssVuNDYgw" target="_blank">2-digit 7-segment display counter with buttons</a> in action.</p>
<p>You might also enjoy:<ol>
<li><a href='http://www.theelectronicshobbyist.com/blog/controlling-a-seven-segment-display-using-arduino-part-3-of-4/' rel='bookmark' title='Controlling a Seven-Segment Display Using Arduino Part 3'>Controlling a Seven-Segment Display Using Arduino Part 3</a></li>
<li><a href='http://www.theelectronicshobbyist.com/blog/arduino-2-digit-7-segment-display-counter/' rel='bookmark' title='Arduino 2-Digit 7-Segment Display Counter | Part 1'>Arduino 2-Digit 7-Segment Display Counter | Part 1</a></li>
<li><a href='http://www.theelectronicshobbyist.com/blog/controlling-a-seven-segment-display-using-arduino-part-4-of-4/' rel='bookmark' title='Controlling a Seven-Segment Display Using Arduino Part 4'>Controlling a Seven-Segment Display Using Arduino Part 4</a></li>
<li><a href='http://www.theelectronicshobbyist.com/blog/arduino-2-digit-7-segment-display-with-buttons-sketch/' rel='bookmark' title='Arduino 2-Digit 7-Segment Display with Buttons: Sketch | Part 5'>Arduino 2-Digit 7-Segment Display with Buttons: Sketch | Part 5</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.theelectronicshobbyist.com/blog/arduino-2-digit-7-segment-display-with-buttons/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Controlling a Seven-Segment Display Using Arduino Part 3</title>
		<link>http://www.theelectronicshobbyist.com/blog/controlling-a-seven-segment-display-using-arduino-part-3-of-4/</link>
		<comments>http://www.theelectronicshobbyist.com/blog/controlling-a-seven-segment-display-using-arduino-part-3-of-4/#comments</comments>
		<pubDate>Mon, 15 Feb 2010 07:00:01 +0000</pubDate>
		<dc:creator>Natalia</dc:creator>
				<category><![CDATA[Arduino]]></category>
		<category><![CDATA[Project]]></category>
		<category><![CDATA[7-segment display]]></category>
		<category><![CDATA[button]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[LED]]></category>
		<category><![CDATA[pins]]></category>
		<category><![CDATA[sketch]]></category>

		<guid isPermaLink="false">http://www.theelectronicshobbyist.com/blog/?p=19</guid>
		<description><![CDATA[The second sketch cycles through the numbers from 0 to 9, but only increments the display counter each time a button is pressed. Note that this code includes simple debouncing by introducing a short delay when the Arduino detects that the button has been pressed. Sketch #2: // www.TheElectronicsHobbyist.com/blog // Natalia Fargasch Norman // Seven-segment [...]]]></description>
			<content:encoded><![CDATA[<p></p><div class="separator" style="clear: both; text-align: center;"><a style="clear: right; float: right; margin-bottom: 1em; margin-left: 1em;" href="http://www.theelectronicshobbyist.com/images/Seven-Segment.jpg"><img src="http://www.theelectronicshobbyist.com/images/Seven-Segment.jpg" border="0" alt="" width="162" height="200" /></a></div>
<p>The second sketch cycles through the numbers from 0 to 9, but only increments the display counter each time a button is pressed. Note that this code includes simple debouncing by introducing a short delay when the <a href="http://www.cutedigi.com/product_info.php?ref=3&amp;products_id=4221&amp;affiliate_banner_id=1" target="_blank">Arduino</a> detects that the button has been pressed.</p>
<p><span id="more-19"></span></p>
<p>Sketch #2:</p>
<pre>// www.TheElectronicsHobbyist.com/blog
// Natalia Fargasch Norman
// Seven-segment LED Display
// Common Anode pins 3 and 8

//   G F + A B
//   | | | | |   -&gt; pins and segments they control
//   ---------
//  F|   A   |B
//   |---G---|   -&gt; segments
//  E|   D   |C
//   ---------
//   | | | | |   -&gt; pins and segments they control
//   E D + C DP

// Segments that make each number when lit:
// 0 =&gt; ABCDEF
// 1 =&gt; BC
// 2 =&gt; ABDEG
// 3 =&gt; ABCDG
// 4 =&gt; BCFG
// 5 =&gt; ACDFG
// 6 =&gt; ACDEFG
// 7 =&gt; ABC
// 8 =&gt; ABCDEFG
// 9 =&gt; ABCDFG

// <a href="http://www.theelectronicshobbyist.com/blog/goto/uno" style="" target="_blank" rel="nofollow" onmouseover="self.status='http://www.theelectronicshobbyist.com/blog/goto/uno';return true;" onmouseout="self.status=''">Arduino</a> digital pins used to light up
// corresponding segments on the LED display
#define A 2
#define B 3
#define C 4
#define D 5
#define E 6
#define F 7
#define G 8

// Pushbutton connected to pin 9
#define BUTTON 9

// Common anode;
// on when pin is low
// and off when pin is high
#define ON LOW
#define OFF HIGH

int count = 0; // current display count
int val = 0;   // digital input from button

void setup() {
  pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(C, OUTPUT);
  pinMode(D, OUTPUT);
  pinMode(E, OUTPUT);
  pinMode(F, OUTPUT);
  pinMode(G, OUTPUT);
  pinMode(BUTTON, INPUT);
  zero();
}

void loop() {
  val = digitalRead(BUTTON);
  if (val == HIGH) {
    count++;
    delay(200);
    switch (count) {
      case 0:
        zero();
        break;
      case 1:
        one();
        break;
      case 2:
        two();
        break;
      case 3:
        three();
        break;
      case 4:
        four();
        break;
      case 5:
        five();
        break;
      case 6:
        six();
        break;
      case 7:
        seven();
        break;
      case 8:
        eight();
        break;
      case 9: {
        nine();
        count = -1;
        break;
      }
    }
  }
}

// 0 =&gt; ABCDEF
void zero() {
  digitalWrite(A, ON);
  digitalWrite(B, ON);
  digitalWrite(C, ON);
  digitalWrite(D, ON);
  digitalWrite(E, ON);
  digitalWrite(F, ON);
  digitalWrite(G, OFF);
}

// 1 =&gt; BC
void one() {
  digitalWrite(A, OFF);
  digitalWrite(B, ON);
  digitalWrite(C, ON);
  digitalWrite(D, OFF);
  digitalWrite(E, OFF);
  digitalWrite(F, OFF);
  digitalWrite(G, OFF);
}

// 2 =&gt; ABDEG
void two() {
  digitalWrite(A, ON);
  digitalWrite(B, ON);
  digitalWrite(C, OFF);
  digitalWrite(D, ON);
  digitalWrite(E, ON);
  digitalWrite(F, OFF);
  digitalWrite(G, ON);
}

// 3 =&gt; ABCDG
void three() {
  digitalWrite(A, ON);
  digitalWrite(B, ON);
  digitalWrite(C, ON);
  digitalWrite(D, ON);
  digitalWrite(E, OFF);
  digitalWrite(F, OFF);
  digitalWrite(G, ON);
}

// 4 =&gt; BCFG
void four() {
  digitalWrite(A, OFF);
  digitalWrite(B, ON);
  digitalWrite(C, ON);
  digitalWrite(D, OFF);
  digitalWrite(E, OFF);
  digitalWrite(F, ON);
  digitalWrite(G, ON);
}

// 5 =&gt; ACDFG
void five() {
  digitalWrite(A, ON);
  digitalWrite(B, OFF);
  digitalWrite(C, ON);
  digitalWrite(D, ON);
  digitalWrite(E, OFF);
  digitalWrite(F, ON);
  digitalWrite(G, ON);
}

// 6 =&gt; ACDEFG
void six() {
  digitalWrite(A, ON);
  digitalWrite(B, OFF);
  digitalWrite(C, ON);
  digitalWrite(D, ON);
  digitalWrite(E, ON);
  digitalWrite(F, ON);
  digitalWrite(G, ON);
}

// 7 =&gt; ABC
void seven() {
  digitalWrite(A, ON);
  digitalWrite(B, ON);
  digitalWrite(C, ON);
  digitalWrite(D, OFF);
  digitalWrite(E, OFF);
  digitalWrite(F, OFF);
  digitalWrite(G, OFF);
}

// 8 =&gt; ABCDEFG
void eight() {
  digitalWrite(A, ON);
  digitalWrite(B, ON);
  digitalWrite(C, ON);
  digitalWrite(D, ON);
  digitalWrite(E, ON);
  digitalWrite(F, ON);
  digitalWrite(G, ON);
}

// 9 =&gt; ABCDFG
void nine() {
  digitalWrite(A, ON);
  digitalWrite(B, ON);
  digitalWrite(C, ON);
  digitalWrite(D, ON);
  digitalWrite(E, OFF);
  digitalWrite(F, ON);
  digitalWrite(G, ON);
}</pre>
<p>You might also enjoy:<ol>
<li><a href='http://www.theelectronicshobbyist.com/blog/controlling-a-seven-segment-display-using-arduino-part-4-of-4/' rel='bookmark' title='Controlling a Seven-Segment Display Using Arduino Part 4'>Controlling a Seven-Segment Display Using Arduino Part 4</a></li>
<li><a href='http://www.theelectronicshobbyist.com/blog/controlling-a-seven-segment-display-using-arduino-part-2-of-4/' rel='bookmark' title='Controlling a Seven-Segment Display Using Arduino Part 2'>Controlling a Seven-Segment Display Using Arduino Part 2</a></li>
<li><a href='http://www.theelectronicshobbyist.com/blog/arduino-2-digit-7-segment-display-with-buttons/' rel='bookmark' title='Arduino 2-Digit 7-Segment Display with Buttons | Part 4'>Arduino 2-Digit 7-Segment Display with Buttons | Part 4</a></li>
<li><a href='http://www.theelectronicshobbyist.com/blog/arduino-2-digit-7-segment-display-counter/' rel='bookmark' title='Arduino 2-Digit 7-Segment Display Counter | Part 1'>Arduino 2-Digit 7-Segment Display Counter | Part 1</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.theelectronicshobbyist.com/blog/controlling-a-seven-segment-display-using-arduino-part-3-of-4/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Served from: www.theelectronicshobbyist.com @ 2012-02-06 22:29:43 -->
