Arduino Cookbook (part 2) – What was left out and other information from the preface

Let’s continue going through the Arduino Cookbook by Michael Margolis.  (see the first article about the book.)

The book doesn’t cover much electronics theory and practice, but provides some basic guidance.  If you want some more background in this area, then these books are recommended:

• Make: Electronics by Charles Platt (O’Reilly)
• Getting Started in Electronics by Forrest Mims (Master Publishing)
• Physical Computing by Dan O’Sullivan and Tom Igoe (Cengage)
• Practical Electronics for Inventors by Paul Scherz (McGraw-Hill)

These all look like good books to go through, and we might have future series going through these books.  I recognized the Getting Started in Electronics by Forrest Mims.  I had bought it nearly 25 years ago!!  And I still have that copy.  I was going through it with my children (ages 8 and 12) this summer.  It has some wonderful illustrations in it.  Here are some pictures to give you a sense:

IMG_9283 IMG_9282

The Arduino Cookbook explains code to do very specific things.  The author recommends the following books to give you more information on programming in general and C in particular:

• Practical C Programming by Steve Oualline (O’Reilly)
• A Book on C by Al Kelley and Ira Pohl (Addison-Wesley)

The C Programming Language by Brian W. Kernighan and Dennis M. Ritchie(Prentice Hall)

The code in the Arduino Cookbook has been tested with release versions 0018 through 0020.  It was written before v1.0 was finalized.  There might be small changes required to make the code work, and we are directed to this website:

www.oreilly.com/catalog/9780596802479/

So, if you have doubled checked, and tripled checked something from the book, and it does not work as expected, then check the link above to see if there are some changes required.  There is also an Appendix on troubleshooting, and more help can always be found from: www.arduino.cc

In the section “Using Code Examples” it says:

This book is here to help you make things with Arduino. 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 this book does require permission. Answering a question by citing this book and quoting example code does not require permission. 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: “Arduino Cookbook by Michael Margolis
with Nick Weldin (O’Reilly). Copyright 2011 Michael Margolis and Nicholas Weldin, 9780596802479.”

If you feel your use of code examples falls outside fair use or the permission given here, feel free to contact us at permissions@oreilly.com.

In this set of blog posts, I will be going through the book, page by page, but we will not be putting the information in the book on this page.  Instead I will be providing some feedback, additional information, and my own personal experiments related to the information in the book.  Therefore, you will want to get the book in order to make best use of these blog posts.  Please use the link below so that we can get a small commission from Amazon to help support this site and the buying of all the Arduino related materials that we need to keep experimenting:

Arduino Cookbook

If you like online versions of things, then you should check out Safari Books Online:

my.safaribooksonline.com.

This is actually how I am reading the book, free through a service provided by my local library (www.hcpl.net/).  It is a great way to get technical books, that are searchable, without having to find a place to store them.    It can be great when doing some of the examples having the ability to copy and paste from the electronic book to the code editor without having to type it in, or try to find an alternate resource to get it from.

The Acknowledgments section can sometimes be very useful to read.  There are several people that Michael thanks:

Nick Weldin

Simon St. Laurent – editor at O’Reilley

Brian Jepson (twitter)– helped to provide the Xbee content in chapter 14

Audrey Doyle – fixed typos and grammatical errors

Philip Lindsay – chapter 15 help – how to make Ethernet more understandable

Mikal Hart – GPS recipes and software serial

Core Arduino development team: Massimo Banzi, David Cuartielles (twitter), Tom Igoe, Gianluca Martino, and David Mellis

Alexandra Deschamps-Sonsino, CEO of Tinker London (has workshops for the Arduino)

Peter Knight

SparkFun

Maker Store

Gravitech

NKC Electronics

Modern Device

Liquidware

Adafruit

Makerbot Industries

Mindkits

Oomlout

SK Pang

Brock Craft

Daneil Soltis

(families of the writers)

Joshua Nobel for his book Programming Interactivity:  A Designer’s Guide to Processing, Arduino, and Openframeworks (published July 28, 2009)  (This looks like a good book for us to go through too)

In the next post, we will get into Chapter 1 – Getting started…

Arduino Cookbook by Michael Margolis

 

In order to bring some structure to my experiments with the Arduino, and to help me get through the learning curve quicker, I thought it would be good to have a book to guide me.  The book I have selected is the Arduino Cookbook by Michael Margolis.  It is a recent book, published in March 2011.  The first words of the preface say just what I am looking for.  “This book was written to help you explore the amazing things you can do with Arduino.” Yay

Preface

Arduino is a family of microcontrollers (tiny computers).  It can be used for “physical computing”.  Actually responding to and interacting with the “real world”.

This book is aimed at a broad range of people.  There is no assumption about a particular hardware or software background.  But even if you have a microcontroller background or a background in C/C++, you will still find this book very useful.

What does this book cover?

Chapter 1 – Getting Started – (everything you need to get that first program written, that first circuit created, and possibly even that first LED blinking?)

Chapter 2 – Making the Sketch Do Your Bidding.  (“sketches” are what we call the Arduino program)  This chapter covers the key software concepts and task.

Chapter 3 – Using Mathematical Operators

Chapter 4 – Serial Communications – how to connect to the computer and other devices using a serial connection.

Chapter 5 – Simple Digital and Analog Input – how to actually get the data into the arduino

Chapter 6 – Getting Input from Sensors – enable touch, sound, position, heat, and light

Chapter 7 – Visual Output – controlling LEDS (ok, looks like they wait until this chapter to get that first LED blinking?)  Switch on one or many LEDs, controlling brightness and color.  How to create bar graphs, numeric LED displays, create patterns, animations with LED arrays.

Chapter 8 – Physical output .. how to make things move with solenoids (I’ve never quite understood what a solenoid is…)  servo motors (got some of them floating around… will be good to put them to some good use… found some for sale on eBay for just a few cents too.  but possibly will not win those)  Physical output is where the rubber meets the road.  This is where we make things move!  Can’t wait till we get to chapter 8!!!

Chapter 9 – Audio Output – generate sounds by playing wav files and MIDI output.

Chapter 10 – Remotely Controlling External Devices – interact with the TV, cameras, garage doors, appliances, toys. 

Chapter 11 – Using Displays – interfacing with text and graphical LCD displays

Chapter 12 – Using Time and Dates – measuring time; handling time delays (here we will learn what we need to know to develop a clock, timer, and I could create a speech timer for my toastmaster club…)

Chapter 13 – Communicating using I2C and SPI  (I have did one project in the Parallax Basic stamp book using I2C.  I understand it is a protocol to connect things together.  I am completely unfamiliar with SPI…)

Chapter 14 – Wireless Communication – using XBee

Chapter 15 – Ethernet and Networking – hook up things to the Internet.  (I have been playing around with the EtherShield, and got a web page setup to control a little LED.  But I got the cheaper enc28j60 based EtherShield.  Not the newer, expensive one based on the Wiznet chip.  The API calls for the Enc28j60 are a lot more difficult to understand than the newer ones.  The difference in cost though is about 100%.  You can get the Enc28j60 etherShield for about $20.  The Wiznet one will be around $45.  It will be interesting to see which one the book uses… I think we can both already guess 🙂 )

Chapter 16 – Using, Modifying and Creating Libraries (maybe this is where I can learn how to create a easier to use library wrapper for the EtherShield that I have. )

Chapter 17 – Advanced Coding and Memory Handling

Chapter 18 – Using the Controller Chip Hardware – How to access and use hardware functions that are not “fully” exposed through the documented Arduino language… (Sounds interesting and advanced)

I have no idea how long it will take to get through the book.  And I imagine we will be taking a lot of detours on the way to create projects using what we have learned.  But I would recommend that if you are starting with the Arduino that you get the book and come along this journey with me.  I intend to keep you updated on my progress in the book, and I intend to publish the code and projects that I create from the things that I learn from the book, but to fully understand everything, you will need the book.  Please use this link:

Also, if you live in Harris county, then you can access it through the Harris Public County Library who has an arrangement with SafariBooksOnline to make it available.  Your library might have a similar setup.

Follow Me Game – Arduino Based Game

Remember playing MySimon (Simon Says) back in the 1980s?  It was a simple game that could entertain you for hours.  It had 4 buttons, each of a different color.  Turn on the game, and one of the buttons would glow.  Then you had to press that button.  Another button was added to the pattern, and the goal was to get to the longest pattern without messing up.
Here I show a simple Arduino based version of this game.  It is simpler because there are only 3 buttons, but it is completely open for you to build on and learn from.

 

image

See the circuit and files on Fritzing.org

 

Update:  I received an email about a more sophisticated version of Simon.


Arduino SimonGame

More infos at jadiema.blogspot.com/
Arduino Simon Game: Version 3
Initial Release from David M. Auld (19th October 2009): Version 1 & 2
www.dave-auld.net/index.php?option=com_content&view=article&id=96:arduino-project-simon-game&catid=54:arduino-projects&Itemid=107
Added some features by jadiema: Version 3:
NEW: Added EEPROM storage for high score: level and name
NEW: Multilangage selection
You’ll find the code at: code.google.com/p/my-arduino-projects/downloads/detail?name=Arduino_SimonGame_EEPROM_LANGUE_V3_1.pde&can=2&q=#makechanges

Simple Scoreboard using the Arduino processor

The Arduino microcontroller board has got to be one of the coolest things to come along.  If you ever wanted to create simple gadgets and devices without that much hassle, then this the board to get.  You can hook it up to your computer using the USB port, program in a simple C language, download the code, and away you go.  You can use it to create controllers for your games, lights for your Christomas tree, loggers for your garden, monitors for your rooms, and controllers for your robots.  All you need is a little patience and imagination (access to Google helps too 🙂 ).

Today, I want to share with you a little project that we created.  It is a simple scoreboard.  It took us about 20 minutes to put together, but that is because we were just learning and didn’t know much. 

We started out by finding this example on how the LCD panel works: www.arduino.cc/en/Tutorial/LiquidCrystal 

Then we hooked up some buttons, added some variables, created several iterations between our code.

Start off displaying the “Hello World” message in the LCD example.  Then had added a “homeScore” variable and displayed that.  Then added the Home Button.  And tested to see if it was “high”, i.e. “on” in our code.  Our first version of the code did not have the “delay” commands, and pressing the button one time caused the score to increment by 50-120 points.  This was because the loop would have that many cycles before the button was brought back “low”, i.e. “off”.  Adding the “delay” kept this from happening.  Then did the same thing for the visitor button.  Required some experimentation with the lcd.SetCursor command to get the visitor scores to show up in the right place.  Then added the reset button and set the scores to 0 when it was pressed.  Ran into the problem that if the score had become two digit, then the 0 was only over writing part of the digits.  Decided to “clear” the display completely on reset, which resolved the problem.  Then cleaned up the code a little, and this was the results:

 

IMG_8725

image

Code (based on LCD example: www.arduino.cc/en/Tutorial/LiquidCrystal )

/*
Simple Scoreboard

Shows a simple Home/Visitor scoreboard

*/

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int homeScore;
int visitorScore;
const int buttonHomePin = 6;
const int buttonVisitorPin = 7;
const int buttonResetPin = 8;
int buttonState = 0;

void setup() {
  // set up the LCD’s number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  setupScreen(); 
}

void setupScreen()
{
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("home     visitor");
  homeScore = 0;
  visitorScore = 0;
}

void loop() {
  buttonState = digitalRead(buttonHomePin);
  if(buttonState == HIGH)
  {
    homeScore++;
    delay(500);
  }
    buttonState = digitalRead(buttonVisitorPin);
  if(buttonState == HIGH)
  {
    visitorScore++;
    delay(500);
  }

   buttonState = digitalRead(buttonResetPin);
  if(buttonState == HIGH)
  {
    setupScreen();
  }
   lcd.print(homeScore);
   lcd.setCursor(12, 1);
   lcd.print(visitorScore);
}

First Two Weeks of Summer

“Can’t talk now… I’m on a schedule.”  “No, I have no time to meet this week.”  “Really focused, can’t think about that right now.”  If you have talked to me in the past two weeks, you have probably heard something like that.  Also, you may have noticed that I have been a little slow on emails, not attending events that I normally go to (like the cypress coffee club networking meeting and the CyFair Chamber meetings or the Friday night chess meets, toastmasters, etc.)  You may have noticed that the daily learn Hindi show on ISpeakHindi.com has been a little less than daily.  And you might be tempted to ask “Are you OK?”  or perhaps you think I am working on some super secret project.  Well, it is time that I set the record straight.

These past two weeks I have been engaged in something that is best described as “continuous contact parenting”.  In past summers we have sent the kinds off to various camps.  But this summer I am at home and available to spend with them 24/7.  I want to make the most of this time.   Because I know that in less than a decade both of them will out of the house, and I will probably have time for them at that point, but they will probably be too busy with their lives for me.

But how to make the most of the summer?  Should I just sit around and do whatever comes to mind.  Should we just play board game after board game.  Read books.  Watch movies.  What should we do?

I had some very specific ideas on what an ideal summer day might look like.  It should have exercise.  Time for eating.  Piano practice.  Math worksheets.  Piano writing.  But there is one area that I thought we could focus on and have a lot of fun:  Electronics and robotics.  After putting all that together, we came up with a daily schedule that we would try for two weeks:

image

The electronics time was spent learning about voltage, current, resistance, using the multi-meter, understanding the relationship between voltage, current, resistance, soldering, striping wires, reading circuit diagrams, making connections between components on a breadboard, using a Parallax Basic stamp.

Here are some pictures to give you some idea:

IMG_8710

We also put together some kits:

IMG_8708

In fact, we have a “wall of completed projects” where we put a postcard or something to indicate the projects that we have finished:

IMG_8709

We have also programmed Parallax’s scribbler robot. 

 

IMG_8706

During our free time we did end up playing a board game, Settlers of Catan.  My friend, Daniel Parker, in Austin introduced it to us a couple of weeks ago.  Since then we have played dozens of games.

IMG_8671

This is just a small glimpse of the things we have been up to. 

First Real Electronics project

I’ve been wanting to get into electronics. I have so many kits, parts, books, and ideas. But nothing really put together. I had thought about creating a device to count the number of times that a ping pong ball hits the backboard, but I’m not really sure how to measure each hit. I was thinking of using a microphone and counting pulses, but I’m not quite there yet.

Then at the “Executive” meeting at the HP Houston Toastmasters club (I’m the newly elected VP of Education), there was an agenda item to consider buying a speech timer. You would program the minimum and maximum time. Then click start. And it would turn on a green light once you got to the minimum time. It would show the yellow when you were halfway between the minimum and maximum time. And then it would show red when you get to the maximum time.

So, this all seems simple. I know how to program a basic stamp from parallax to turn on lights, respond to buttons, and display things on an LCD screen. I just need to put them all together. So, stay tuned with the blog to see how I go about doing that.

I have created a wiki page for the project to so you can look at the latest design: Speech Timer Wiki Page

Yesterday, I successfully hooked up my Basic Stamp board to my laptop, wired up an LCD display, and completed the “Experiment #11: A Basic LCD Demonstration”. This showed me how to actually get something to show up on the display.

Today, I need to do “Experiment #30: Using a Real-time clock”. This will ensure that I can count the seconds and minutes accurately. And I want to combine what I learned from #30 and #11 to show the time on the LCD screen. So, lets see how that goes.

Experiment #30: Using a Real-time clock

We use a DS1302 real-time clock (RTC) and a 32.768 kHz crystal hooked up to the stamp.

The crystal has no external markings, but I found this picture on digikey:

The total length is about 1/2 inch. The component part is only 1/4 inch long.

Need a 1k resister.. What are the color bands? Time to google… ahhh Here we go:

www.micro-ohm.com/colorcode/rescolor.html

Brown-black– red

double check with VOM

Got everything hooked up… sample program loaded.. but the time does not increment…

I wonder if the DS1302 or the crystal has gone bad?

Well, that’s all for today.. Hopefully I will get a chance to look at it more tomorrow.

StampWorks day 1

I really would like to get into electronics, robotics, and tele-presence in a big way. But I have little skills in this regard. Lots of ideas, but little skills. No problem.. Just develop the skills.

Over the years, I have bought and collected many electronics/robotics learning kits. My plan is to go through them, develop some basic skills, and then start attempting to create a tele-presence device that will allow for me to remotely do gardening, build things, household chores, etc.

To get started, I will be going through the StampWorks manual. In the course of this, I will learn basic electronics, microprocessor programming, interacting with LCD panel, motors, and other devices.

So, let’s go over the setup:

1) StampWorks Kit from Parallax

2) USB to Serial connector from Radio Shack –ooops, no VISTA drivers… I need to find a different device…

3) HP laptop computer

I checked the Parallax site for a more recent version of the Basic Stamp IDE. There is a newer one (v.2.3.9), that can be found on the Basic Software download page.

Now to test out our setup. First program

‘ {$STAMP BS2}

DEBUG “The StampWorks lab is ready!”

— could not connect to the stamp because there is no VISTA driver for the USB-Serial device I have…. Stay tuned…

hpim5722.jpg