Crash course: Embedded programming with Arduino

Posted by Unknown Minggu, 25 Desember 2011 0 komentar

Try your hand at embedded programming with this project

Demand for embedded programming is growing like crazy, so anyone looking for some job security might consider becoming an ace embedded programmer. It helps to have some programming experience, but even raw beginners can get started inexpensively and learn on their own. One of the friendliest introductions to embedded coding is Arduino. Arduino is a popular open embedded hardware and software platform with hundreds of howtos and projects to try out. Today we review the basics of both electronics and Arduino coding in a fun holiday project, Singing Holiday Snowman.

Singing snowman

I'm going to take my old plastic snowman (figure 1) and outfit him to sing holiday songs whenever anyone approaches within a few feet, and to blink some festive red and green LEDs. (Half the fun of Arduino is blinky lights.) You, of course, may use any object you want. If you are new to electronics and embedded coding, give yourself a few days to put this together because it covers a lot of ground. This project gives hands-on experience in these essential fundamentals:

  • Soldering
  • Reading simple schematics
  • Loading and modifying Arduino programs


Figure 1: Plastic snowman before his operation, mute and unlit
Source: Carla Schroder

Prerequisites

The Singing Snowman is modeled on the Halloween Pumpkin project. Beginners should work through the first three of these excellent Arduino tutorials first. These will teach you how to install and use the Arduino software, and get acquainted with tools and Arduino parts. You'll be doing a fair bit of soldering; if you're not a good hand with a soldering iron, find some nothing-to-lose circuit boards and practice. The soldering page links to some good howtos.
Here is a handy hardware list. The first two items are nice kits that supply everything you need for Singing Snowman, plus leftovers for more projects.

  • Ladyada's Electronics Toolkit, $100. Includes a good adjustable temperature soldering iron, multimeter, snips, strippers, solder, solder sucker and braid, multimeter, vise, and other useful tools.
  • Adafruit ARDX - v1.3 Experimentation Kit. This includes red and green LEDs and matching resistors, printed howtos, breadboard, Arduino Uno, USB cable, wires, and other goodies for multiple projects. $85.
  • Adafruit Wave Shield for Arduino. This is the part that plays sounds. $22. You'll also need a SD/MMC card for storing audio files.
  • A small 8 ohm speaker, like an old PC speaker.
  • MaxBotix EZ1 sonar sensor. This will detect when someone is within a few feet of the snowman, and then it will play songs and animate the blinky lights. I use the MB1010 because it has a narrow sensitivity range to the front. There are three other versions with different sensitivity patterns.
  • Insulated 22-gauge solid-core wire in red, green, white, and black.
  • A magnifier and good light. I like magnifier lamps, and magnifier headlamps are nice too.
  • Wrist grounding strap. Arduino boards and accessories are pretty resilient to electrostatic discharge (ESD), but a strap is cheap insurance in a static-prone environment.
  • Drill and bits.
If you already have a well-equipped electronics workbench and don't need the two Adafruit kits, this is what you'll need for Singing Snowman:

  • Seven red and green LEDs with matching resistors
  • Arduino Uno
  • 9- or 12-volt power supply

Plus the other non-kit parts: Adafruit Wave Shield, MaxBotix EZ1, and wiring. Figure 2 shows my workbench with some of the tools and parts for this project.

Figure 2: Workbench for the singing holiday snowman project
Source: Carla Schroder

LEDs and resistors

You always need resistors with LEDs because resistors limit the current flowing to the LED to a safe level. Resistors are everywhere in electronics, and come in a huge variety of sizes and shapes. We are going to use through-hole carbon film resistors. A common beginner question is how do you know which resistors go with your LEDs? You need three pieces of information: The voltage of your power supply (Vs), and the voltage forward (Vf) and drive current (If) of the LED. Then calculate the resistor value (R), in ohms, like this: R = (Vs - Vf)/If.
For example, I have some of these 5mm red LEDs and a 9-volt power supply. So my equation is (9 - 1.85) / 0.02 = 357.5 ohms. The next highest standard resistor is 360 ohms at 1/2 watt, though most electronics gurus will go one higher and choose a 390 ohm resistor for a larger safety margin. A higher ohm value means more resistance and less current. You can damage LEDs with too much current. What does the watts value signify? That's how much power the resistor can handle. For low-power uses like our little blinky LEDS you'll see 1/8, 1/4, and 1/2 watt resistors. Try one of the many online LED calculators until you feel comfortable with your math, like this one.

Figure 3: The color-coding tells us this is a 390 ohm resistor
Source: Carla Schroder

That's for a single LED. We're using 7, so that changes our requirements. Again, there are handy online calculators to help us. The LED series/parallel array wizard calculates four different wiring layouts for you to choose from.
Resistors are color-coded. The color bars on the resistor in Figure 3 tell us this is rated at 390 ohms with 5% tolerance: Orange-White-Brown means 3-9- and one zero, or 390. The gold band = 5%. The tolerance rating is electronics-speak for plus/minus variance from the stated ohms.
Resistor color code tables are all over the place, on the Web and in books.

Assembly

The Wave Shield requires assembly and there is a lot of soldering, so do that first. When it's all together it will plug in nicely on top of your Arduino board. Then you'll solder your speaker and sonar sensor to the Wave board; follow the Halloween Pumpkin howto to see where the wires go. Make their wires long enough to allow you to mount the speaker and sensor where you want them inside your own project.
Use the LED series/parallel array wizard to figure out your LED wiring, and then solder your wiring harness together. Make sure your positive and negative leads are in correct order. The Halloween Pumpkin page says to use a glue gun to fasten the LEDs in place, but I prefer to drill holes that hold them snugly because it's less messy and easy to make changes. Once they're in place, solder the leads to the appropriate analog inputs and ground on your Wave board, and again leave enough slack in your wiring so you can move and easily access the Arduino and Wave Shield.
The clever part about the Halloween Pumpkin project is it saves a lot of code-writing by using an analog input to capture the audio output, which creates a variable-strength signal, and using that to animate the LEDs. Solder analog input #1 to the R7 1.5k resistor on the Wave board.
Plug your SD card into a PC in the usual way, with a card reader or built-in slot, and transfer your audio files to it. They must be in 22KHz 16-bit mono or lesser quality WAV. Then plug the card into the Wave Shield. Now you're ready to connect your PC to the Arduino with a USB cable, fire up the Arduino software, edit your sketch, and load it into the Arduino. One of Arduino's advantages is the speed of iteration -- you can quickly make and test changes in your code. Figure 4 shows my Singing Holiday Snowman in action.

Figure 4: Singing holiday snowman lit up and singing
Source: Carla Schroder

Arduino code

I'm using the Halloween Pumpkin sketch (an Arduino program is called a sketch) with some minor changes. The Arduino programming language is pretty much a slimmed-down C/C++. You will have to make some changes to this sketch, so look for my comments in the sketch which are enclosed in the standard C multi-line comment symbols, /* */. Single-line comments, which start with //, are the original comments from the original sketch author.
We covered a lot of ground in this Crash Course, and hopefully your project blinks and sings just the way you want. If something doesn't work right, and you can't figure it out, go back to the Arduino tutorials and review the basics, and the Arduino forums are a good source of help.

Additional references


Singing snowman sketch


#include 
#include
#include "util.h"
#include "wave.h"

AF_Wave card;
File f;
Wavefile wave;

// save RAM by using program memory strings
#define playcomplete(x) ROM_playcomplete(PSTR(x))

#define servo 7
#define redled 9
#define eyeleds 18
#define mouthleds 17
#define midmouthleds 16
#define outermouthleds 19

// set up Serial library at 9600 bps
void setup() {
Serial.begin(9600);
Serial.println("Wave test!");

/* Setup the LEDs */

pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(redled, OUTPUT);
pinMode(servo, OUTPUT);
pinMode(eyeleds, OUTPUT);
pinMode(outermouthleds, OUTPUT);
pinMode(midmouthleds, OUTPUT);
pinMode(mouthleds, OUTPUT);

randomSeed(analogRead(0));

/* Useful feedback if the SD card is missing
* or cannot be read */

if (!card.init_card()) {
putstring_nl("Card init. failed!"); return;
}
if (!card.open_partition()) {
putstring_nl("No partition!"); return;
}
if (!card.open_filesys()) {
putstring_nl("Couldn't open filesys"); return;
}

if (!card.open_rootdir()) {
putstring_nl("Couldn't open dir"); return;
}

putstring_nl("Files found:");
ls();
}

void ls() {
char name[13];
int ret;

card.reset_dir();
putstring_nl("Files found:");
while (1) {
ret = card.get_next_name_in_dir(name);
if (!ret) {
card.reset_dir();
return;
}
Serial.println(name);
}
}


void pulseServo(uint8_t servopin, uint16_t p) {

digitalWrite(servopin, HIGH);
delayMicroseconds(600);
while (p--) {
delayMicroseconds(4);
}
digitalWrite(servopin, LOW);
delay(18);
}

/* typedef unsigned char "snowmanstate"
* You can change this name to something
* suitable for your own project
* Measures how close people are and plays
* audio files according to distance */

uint8_t snowmanstate = 0;

void loop() {
int distsensor, i;
long time;
/*
for (i=0; i<50; i++) {
pulseServo(servo,0);
}
for (i=0; i<50; i++) {
pulseServo(servo,400);
}
return;
*/
distsensor = 0;
for (i=0; i<8; i++) {
distsensor += analogRead(0);
delay(50);
}
distsensor /= 8;

// putstring("Sensor = ");
Serial.println(distsensor);

/* A reading of 500 = more than 6 meters away, so once
* every 200 times play one of of three scary sounds
* when no one is present */

if (distsensor<= 500) {
digitalWrite(eyeleds, HIGH);
}
if (distsensor > 500) {
digitalWrite(eyeleds, LOW);

// nobody there. one out of 200 times play one of
// the scary sounds (once every few minutes)

/* Substitute your own audio file names */

snowmanstate = 1;
i = random(200);
//Serial.println(i);
if (i == 0) {
i = random(3);
if (i == 0) {
playcomplete("merryxmas.wav");
} else if (i == 1) {
playcomplete("hollyjolly.wav");
} else {
playcomplete("jinglebellsbatmansmells.wav");
}
}

/* When someone comes closer play these files
* Substitute your own audio file names */

} else if ((distsensor > 300) && (distsensor < 400)) {
if (snowmanstate <= 1) {
playcomplete("rudolph.wav");
} else {
i = random(60);
//Serial.println(i);
if (i == 0) {
i = random(3);
if (i == 0) {
playcomplete("frosty.wav");
} else if (i == 1) {
playcomplete("kissingsanta.wav");
} else {
playcomplete("deckhalls.wav");
}
}
}
snowmanstate = 2;
} else if ((distsensor > 100) && (distsensor < 200)) {
if (snowmanstate <= 2) {
playcomplete("frosty.wav");
} else {
i = random(50); // more often
//Serial.println(i);
if (i == 0) {
i = random(3);
if (i == 0) {
playcomplete("gotell.wav");
} else if (i == 1) {
playcomplete("joyworld.wav");
} else {
playcomplete("letitsnow.wav");
}
}
}
snowmanstate = 3;
} else if (distsensor < 50) {
if (snowmanstate <= 3) {
playcomplete("linussong.wav");
} else {
i = random(30); // more often
//Serial.println(i);
if (i == 0) {
i = random(2);
if (i == 0) {
playcomplete("mrgrinch.wav");
} else if (i == 1) {
playcomplete("sleighride.wav");
}
}

}
snowmanstate = 4;
}
}

/* This part lights the LEDs according to
* the strength of the audio signal */

void ROM_playcomplete(const char *romname) {
char name[13], i;
uint8_t volume;
int v2;

for (i=0; i <13; i++) {
name[i] = pgm_read_byte(&romname[i]);
}
name[12] = 0;
Serial.println(name);
playfile(name);
while (wave.isplaying) {
volume = 0;
for (i=0; i<8; i++) {
v2 = analogRead(1) - 512;
if (v2< 0)
v2 *= -1;
if (v2 > volume)
volume = v2;
delay(5);
}
if (volume > 200) {
digitalWrite(outermouthleds, HIGH);
} else {
digitalWrite(outermouthleds, LOW);
}
if (volume > 150) {
digitalWrite(midmouthleds, HIGH);
} else {
digitalWrite(midmouthleds, LOW);
}
if (volume > 100) {
digitalWrite(mouthleds, HIGH);
} else {
digitalWrite(mouthleds, LOW);
}
//putstring("vol = "); Serial.println(volume, DEC);
}
card.close_file(f);
}

void playfile(char *name) {
f = card.open_file(name);
if (!f) {
putstring_nl(" Couldn't open file"); return;
}
if (!wave.create(f)) {
putstring_nl(" Not a valid WAV"); return;
}
// ok time to play!
wave.play();
}
TERIMA KASIH ATAS KUNJUNGAN SAUDARA
Judul: Crash course: Embedded programming with Arduino
Ditulis oleh Unknown
Rating Blog 5 dari 5
Semoga artikel ini bermanfaat bagi saudara. Jika ingin mengutip, baik itu sebagian atau keseluruhan dari isi artikel ini harap menyertakan link dofollow ke https://androidblackberries.blogspot.com/2011/12/crash-course-embedded-programming-with.html. Terima kasih sudah singgah membaca artikel ini.

0 komentar:

Posting Komentar

Trik SEO Terbaru support Online Shop Baju Wanita - Original design by Bamz | Copyright of android blackberry.