MintDuino James Floyd Kelly and Marc de Vinck docx

58 598 0
MintDuino James Floyd Kelly and Marc de Vinck docx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

MintDuino Building an Arduino-Compatible Breadboard Microcontroller James Floyd Kelly & Marc de Vinck CREATE YOUR OWN 2-PLAYER LED GAME Project Book Project Book n Wire up and program the MintDuino to light an LED n Build a game that tests the reflexes of two players n Learn how to make your “game light” glow at random intervals n Construct a pushbutton circuit to control individual players’ LEDs n Put all of the components together, including simple code to control your game Get started with MintDuino, the Maker Shed kit that’s perfect for learning or teaching the fundamentals of how microcontrollers work. This hands-on book shows you how to build a complete MintDuino project from start to finish. Learn how to assemble the microcontroller on a breadboard (no soldering required), and immediately begin programming it. You’ll build the MintDuino Reflex Game in five separate stages or sub-tasks. Breaking down a large project into manageable tasks helps you track down errors quickly, and lets you see how each part of the circuit works. Once you complete the game, you’ll have the resources and experience to tackle more MintDuino projects. If you’re interested in the fascinating world of microcontrollers, you’ll enjoy this book. MintDuino Building an Arduino-Compatible Breadboard Microcontroller Twitter: @oreillymedia facebook.com/oreilly oreilly.com ISBN: 978-1-449-30766-0 US $4.99 CAN $5.99 MintDuino James Floyd Kelly and Marc de Vinck Beijing • Cambridge • Farnham • Köln • Sebastopol • Tokyo MintDuino by James Floyd Kelly and Marc de Vinck Copyright © 2011 Blue Rocket Writing Services, Inc. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://my.safaribooksonline.com). For more informa- tion, contact our corporate/institutional sales department: (800) 998-9938 or corporate@oreilly.com. Editor: Brian Jepson Production Editor: Kristen Borg Proofreader: O’Reilly Production Services Cover Designer: Mark Paglietti Interior Designer: David Futato Illustrator: Robert Romano September 2011: First Edition. Revision History for the First Edition: September 12, 2011 First release December 16, 2011 Second release See http://oreilly.com/catalog/errata.csp?isbn=9781449307660 for release details. The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. The Maker Press series desig- nations, MintDuino, and related trade dress are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. ISBN: 978-1-449-30766-0 [LSI] 1325889248 Contents Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . v 1/Build a Mintronics: MintDuino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Build the Power Supply . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 Power the Microcontroller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4 Getting Ready to Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 2/The MintDuino Reflex Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 3/Subtask 1: Light an LED . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Start Building . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Upload Your First Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 4/Subtask 2: Randomly Light an LED . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 The Game Takes Shape . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .22 5/Subtask 3: Light an LED with a Pushbutton . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Light the LED . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .30 6/Subtask 4: Add Buttons and LEDs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Light LEDs for Each Player . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 7/Subtask 5: Program the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 The Final Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .45 Contents iii Preface The MintDuino is deceiving—this little tin of electronics is capable of provid- ing the brains for an unlimited number of devices limited only by your imag- ination…and, of course, your bank account. While the MintDuino is certainly capable of being used during a moment of inspiration, you’ll find that the real power of the device comes when you sit down, figure out what you want to make, and then start tackling your project a bit at a time. Anyone who has done circuit building or programming (or both) knows that it rarely pays to just start inserting components and writing complex sketches (programs in Arduino-speak). When trouble arises in a circuit or a program, it can be a time-consuming process to track down the error (or more likely, errors) when you have too much complexity. That’s why it’s always a good idea to break a project idea down into manageable components—let’s call them subtasks. If your big project has five major subtasks, tackling the first subtask and building a slightly less complicated circuit with a slightly less complicated sketch will save time and reduce stress. Once you’ve got the first subtask figured out, you move on to the second. By the time you’ve got all five subtasks built and their individual sketches tested, you’re likely to have an increased chance of success when you pull them all together and build The Big Project. And that’s exactly what you’re going to do in this MintDuino Notebook. You’re going to be shown The Big Project…but you’re not going to be allowed to build it just yet. The Big project has subtasks, and first you’re going to learn how to get the subtasks to work. Once you’ve done that, you’ll be ready for the finale where you bring together all you’ve learned and tackle…The Big Project. So, what is The Big Project? Keep reading. What You Need The complete list of materials for this project is below, but you’ll find a partial list of components with each subtask that contains only those items used in that particular section: • 1 MintDuino—assembled (see http://makeprojects.com/Project/Build -a-Mintronics-MintDuino/608/1 for assembly instructions) • 1 9V battery • 1 FTDI adapter, such as the FTDI Friend (see http://www.makershed .com/ProductDetails.asp?ProductCode=MKAD22) • 1 USB cable (A to mini-B type) • A jumper wire kit, such as http://www.makershed.com/ProductDetails .asp?ProductCode=MKSEEED3 • 2 LEDs (you can use any color you have handy) Plus, you’ll need the following components, all of which are available in the Mintronics: Survival Pack: • 2 LEDs (one red and one green LED come with the Survival Pack, but you can use any color you have handy) • 1 mini breadboard • 1 9V battery connector • 3 resistors (100 ohm minimum) • Jumper wires (you’ll find enough wire in the Survival Pack to get you started, but you’ll need to dip into the jumper wire kit soon) • 2 pushbuttons • Enough jumper wire to get you through Subtask 3. Conventions Used in This Book The following typographical conventions are used in this book: Italic Indicates new terms, URLs, email addresses, filenames, and file exten- sions. Constant width Used for program listings, as well as within paragraphs to refer to pro- gram elements such as variable or function names, databases, data types, environment variables, statements, and keywords. vi Preface Constant width bold Shows commands or other text that should be typed literally by the user. Constant width italic Shows text that should be replaced with user-supplied values or by val- ues determined by context. TIP: This icon signifies a tip, suggestion, or general note. CAUTION: This icon indicates a warning or caution. Using Code Examples This book is here to help you get your job done. In general, you may use the code in this book in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require per- mission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission. We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “MintDuino by James Floyd Kelly (O’Reilly). Copyright 2011 Blue Rocket Writing Services, Inc., 978-1-4493-0766-0.” If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at permissions@oreilly.com. Safari® Books Online Safari Books Online is an on-demand digital library that lets you easily search over 7,500 technology and creative reference books and videos to find the answers you need quickly. With a subscription, you can read any page and watch any video from our library online. Read books on your cell phone and mobile devices. Access Preface vii new titles before they are available for print, and get exclusive access to manuscripts in development and post feedback for the authors. Copy and paste code samples, organize your favorites, download chapters, bookmark key sections, create notes, print out pages, and benefit from tons of other time-saving features. O’Reilly Media has uploaded this book to the Safari Books Online service. To have full digital access to this book and others on similar topics from O’Reilly and other publishers, sign up for free at http://my.safaribooksonline.com. How to Contact Us Please address comments and questions concerning this book to the pub- lisher: O’Reilly Media, Inc. 1005 Gravenstein Highway North Sebastopol, CA 95472 800-998-9938 (in the United States or Canada) 707-829-0515 (international or local) 707-829-0104 (fax) We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at: http://www.oreilly.com/catalog/0636920020882 To comment or ask technical questions about this book, send email to: bookquestions@oreilly.com For more information about our books, courses, conferences, and news, see our website at http://www.oreilly.com. Find us on Facebook: http://facebook.com/oreilly Follow us on Twitter: http://twitter.com/oreillymedia Watch us on YouTube: http://www.youtube.com/oreillymedia Content Updates December 16, 2011 • Modified the book’s style and trim size. • Added a new chapter that explains how to build the Mintduino. viii Preface [...]... out how to detect which of two pushbuttons has been pressed Then we add some code to start the game, check for a premature button push, and reset the game and we’re in business 11 NOTE: This MintDuino Notebook will provide you with the sample code required to test various circuits and electronics components, but it will not be able to provide a comprehensive tutorial on programming the MintDuino (or... paired pushbutton is pressed Once we understand what’s required in order to light LEDs, wait a random amount of time, and test for pushbuttons to be pressed, we can pull it all together and build the MintDuino Reflex Game The fact that we’ll have multiple LEDS shouldn’t be a problem—if we know how to light one, well, we can light two…or three And if we know how to determine if a pushbutton has been pressed,... a better understanding of the programming language used to create MintDuino sketches, you’ll want to turn to a variety of online and printed sources designed to teach beginning Arduino programming Make: Arduino, at http://makezine.com/ar duino/, is a great starting point You’ll find videos, projects, books, and more there 12 MintDuino 3/Subtask 1: Light an LED We’ll start with Subtask 1 and assemble... the code below into the Arduino IDE: // MintDuino NoteBook 1 – Subtask 2 int ledPin = 7; // Digital Pin 7 for LED anode connection int ledWaitMin = 2000; // Set minimum wait time to 2000 milliseconds void setup() { // use noise on pin 1 to generate a random number randomSeed(analogRead(1)); pinMode(ledPin, OUTPUT); } void loop() { // add random time of 0-5 seconds int ledWait = ledWaitMin + random(5000);... count++) { digitalWrite(ledPin, HIGH); delay(250); digitalWrite(ledPin, LOW); delay(250); } delay(ledWait); // random amount of time passes digitalWrite(ledPin, HIGH); delay(2000); // wait 2 seconds after random lighting digitalWrite(ledPin, LOW); 21 delay(5000); // wait 5 seconds before resetting } After you upload this sketch and each time you reboot or power up the MintDuino, the following will occur... the MintDuino so that you can upload the sketch Remember that you’ll need to provide power to the MintDuino using the 9V battery! 18 MintDuino Figure 3-5 Add the FTDI Friend (Adapter) to the MintDuino and upload the sketch After uploading the sketch to the MintDuino, leave the USB cable plugged into the FTDI /MintDuino and you should see a quickly flashing LED on the mini breadboard, as shown in Figure... button first after the Game Light LED lights up wins, and a green LED will light up on the side of the winning player That’s The Big Project—but let’s break it down into four subtasks that will help us understand the proper wiring of the MintDuino, as well as the electronics components and programming elements required: 1 Wire up and program the MintDuino to light an LED—this will help us to not only... delay(250); digitalWrite(ledPin, LOW); delay(250); } delay(ledWait); // random amount of time passes digitalWrite(ledPin, HIGH); delay(2000); // wait 2 seconds after random lighting digitalWrite(ledPin, LOW); delay(5000); // wait 5 seconds before resetting } Connect the FTDI Friend (or other FTDI adapter) to your MintDuino as seen in Figure 3-5 This connects your computer to the MintDuino so that you can upload... connecting the mini breadboard to the MintDuino Figure 3-4 MintDuino and mini breadboard circuit completed with jumper wires Subtask 1: Light an LED 17 Upload Your First Sketch Now it’s time to upload the Subtask 1 program (sketch) You can download this sketch from http://examples.oreilly.com/0636920020882, or simply open your Arduino IDE and enter the following sketch/code: // MintDuino NoteBook 1 – Subtask... for LED anode connection int ledWaitMin = 2000; // Set minimum wait time to 2000 milliseconds void setup() { // use noise on pin 1 to generate a random number randomSeed(analogRead(1)); pinMode(ledPin, OUTPUT); } void loop() { // add random time of 0-5 seconds int ledWait = ledWaitMin + random(5000); // three fast blinks for (int count = 0; count < 3; count++) { digitalWrite(ledPin, HIGH); delay(250); . $5.99 MintDuino James Floyd Kelly and Marc de Vinck Beijing • Cambridge • Farnham • Köln • Sebastopol • Tokyo MintDuino by James Floyd Kelly and Marc de Vinck Copyright. MintDuino Building an Arduino-Compatible Breadboard Microcontroller James Floyd Kelly & Marc de Vinck CREATE YOUR OWN 2-PLAYER

Ngày đăng: 15/03/2014, 02:20

Mục lục

  • Conventions Used in This Book

  • How to Contact Us

  • Chapter 1. Build a Mintronics: MintDuino

    • Build the Power Supply

    • Getting Ready to Program

    • Chapter 2. The MintDuino Reflex Game

    • Chapter 3. Subtask 1: Light an LED

      • Start Building

      • Upload Your First Sketch

      • Chapter 4. Subtask 2: Randomly Light an LED

        • The Game Takes Shape

        • Chapter 5. Subtask 3: Light an LED with a Pushbutton

          • Light the LED

          • Chapter 6. Subtask 4: Add Buttons and LEDs

            • Light LEDs for Each Player

            • Chapter 7. Subtask 5: Program the Game

              • The Final Sketch

Tài liệu cùng người dùng

Tài liệu liên quan