Book Image

C Programming for Arduino

By : Julien Bayle
Book Image

C Programming for Arduino

By: Julien Bayle

Overview of this book

Physical computing allows us to build interactive physical systems by using software & hardware in order to sense and respond to the real world. C Programming for Arduino will show you how to harness powerful capabilities like sensing, feedbacks, programming and even wiring and developing your own autonomous systems. C Programming for Arduino contains everything you need to directly start wiring and coding your own electronic project. You'll learn C and how to code several types of firmware for your Arduino, and then move on to design small typical systems to understand how handling buttons, leds, LCD, network modules and much more. After running through C/C++ for the Arduino, you'll learn how to control your software by using real buttons and distance sensors and even discover how you can use your Arduino with the Processing framework so that they work in unison. Advanced coverage includes using Wi-Fi networks and batteries to make your Arduino-based hardware more mobile and flexible without wires. If you want to learn how to build your own electronic devices with powerful open-source technology, then this book is for you.
Table of Contents (21 chapters)
C Programming for Arduino
Credits
About the Author
Acknowledgement
About the Reviewers
www.PacktPub.com
Preface
Index

Creating your own LED-array library


We are going to create a very small library and test it with a basic circuit including six LEDs that are not multiplexed.

Wiring six LEDs to the board

Here is the circuit. It basically contains six LEDs wired to Arduino:

Six LEDs wired to the board

The circuit diagram is shown as follows:

Another diagram of the six LEDs wired directly to Arduino

I won't discuss the circuit itself, except to mention that I put in a 1 kΩ resistor. I took the worst case where all LEDs would be switched on at the same time. This would drive a lot of current, and so this acts as security for our Arduino. Some authors wouldn't use it. I'd prefer to have some LEDs dimming a bit in order to protect my Arduino.

Creating some nice light patterns

Here is code for lighting up the LEDs according to some patterns, all hardcoded. A pause is made between each pattern display:

void setup() {

  for (int i = 2 ; i <= 7 ; i++)
  {
    pinMode(i, OUTPUT);
  }
}

void loop(){

  // switch on everything...