Learning to Program the Arduino
Listing 4. Fade an LED On/Off Using a Flex Sensor
/* Flex sensor + LED
/* Analog Input
* Demonstrates analog input by reading an analog sensor
* on analog pin 0 and turning on and off a light emitting
* diode (LED) connected to digital pin 13.
* The amount of time the LED will be on and off depends
* on the value obtained by analogREAD().
* Created by David Cuartielles
* Modified 16 Jun 2009
* By Tom Igoe
* http://arduino.cc/en/Tutorial/AnalogInput
* Modified 16 July, 2011
* By Amit Saha
* Current code was tested with a Flex sensor
*/
int sensorPin = 0; /*Flex sensor pin */
int ledPin = 13; /* LED pin*/
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
int loop=1;
float sensorValues=0.0,delaytime;
for(loop=1;loop <=10 ;loop++)
{
sensorValues = sensorValues + analogRead(sensorPin);
}
//Use the average as a delay value
delaytime = sensorValues/10;
digitalWrite(ledPin, HIGH);
delay(delaytime);
digitalWrite(ledPin, LOW);
delay(delaytime);
}
The flex sensor has two pins: one end should be connected to the +5V input, and the other pin should be connected to the analog pin 0 and farther on to the ground via a 10 kOhm resistor.
Serial Communication
All the sketches so far have made use of the Arduino library calls to read and write to the Arduino pins. You haven't directly made use of the serial communication between the host computer and the Arduino board. First, I'll describe how to write a basic client/server style program.
The "server" program is a sketch that lives on the Arduino board waiting for serial data to be available (an integer in this case) and sends back the number by adding 1 to it to the "client".
Listing 5. Example of a Server Sketch
/* Serial communication demo: +1*/
/* Server program
Amit Saha*/
int number;
void setup()
{
// Open the serial connection, 9600 baud
Serial.begin(9600);
}
void loop()
{
// Get the data "packet"
// Wait for some data to arrive
if (Serial.available()>1) {
//operation=Serial.read();
number=Serial.read();
Serial.println(number + 1);
}
}
The client side of the program is written in Processing using its serial library (Listing 6).
Listing 6. Example of a Serial Client Sketch
/*Client for Serial communication*/
/* Amit Saha */
import processing.serial.*;
Serial myPort; // The serial port
// initial variables:
int i = 1; // counter
char inData;
void setup () {
size(400, 350); // window size
// List all the available serial ports
println(Serial.list());
// Pick up the first port, since I usually have
// just the Arduino connected.
// Make sure the correct port is selected here.
myPort = new Serial(this, Serial.list()[0], 9600);
myPort.clear();
// set initial background:
background(255); }
void draw () {
myPort.write(4);
//myPort.write(5);
if (myPort.available() > 0) {
inData = (char)myPort.read(); // Typecast it to the corresponding
// character for the
// ASCII value
serialEvent();
}
}
void serialEvent () {
System.out.println(inData);
}
To run the client, download the processing IDE and create a new sketch with the code as shown in Listing 5. Run this code after you have uploaded the server sketch (Listing 5) to the Arduino.
Controlling a Servo
Now, let's make things move with your Arduino code. For this example, let's control the movement of a servo—that is, start and stop the servo and control its speed and rotation. Three wires protrude out of the servo: control (white/yellow), power (red) and ground (black/brown). First, set up the circuit such that the the control wire is connected to the Arduino's digital pin 2, the red wire is connected to the Arduino's 5V input and the black wire to the ground. Now, upload the sketch (Listing 7) to your Arduino. You also optionally may connect an LED to the digital pin 13 (in the same way you connected a single LED earlier), which will turn on or off depending on whether the servo is rotating.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Designing Electronics with Linux | May 22, 2013 |
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
- New Products
- Linux Systems Administrator
- Senior Perl Developer
- Technical Support Rep
- UX Designer
- Web & UI Developer (JavaScript & j Query)
- Designing Electronics with Linux
- Dynamic DNS—an Object Lesson in Problem Solving
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Using Salt Stack and Vagrant for Drupal Development
- Nice article, thanks for the
7 hours 58 min ago - I once had a better way I
13 hours 43 min ago - Not only you I too assumed
14 hours 1 min ago - another very interesting
15 hours 54 min ago - Reply to comment | Linux Journal
17 hours 47 min ago - Reply to comment | Linux Journal
1 day 41 min ago - Reply to comment | Linux Journal
1 day 58 min ago - Favorite (and easily brute-forced) pw's
1 day 2 hours ago - Have you tried Boxen? It's a
1 day 8 hours ago - seo services in india
1 day 13 hours ago



Comments
Really interesting
Arduino is a really intersting project because its way to use are limitless. It can be adapted to many things and it's quite easy to program.
I love the Arduino! Thanks
I love the Arduino! Thanks for the interesting article.
Processing, C/C++ and Arduino
Thank you for the comment.
Thank you for the comment. Perhaps, "based on Processing" is not very true.
However to me, the language looks "very" C. And yes as you pointed out, its really gcc-avr down below as also pointed out in the later parts of the article. My experience with Processing is not beyond a basic level, so I came to Arduino "from" C, rather than processing.
Cheers,
Amit
Great article. Just a brief
Great article.
Just a brief observation.
Arduino doesn't stand for "good friend" in Italian (I'm Italian). As far as I know Arduino is the name of the cafè Arduino inventors where used to meet.
But Wikipedia says so?
Hello: Thanks for your comment. I am sorry if I got it wrong, But Wikipedia has this: "Arduino" is an Italian masculine first name, meaning "strong friend". The English version of the name is "Hardwin".(From: http://en.wikipedia.org/wiki/Arduino#History)
Cheers,
Amit
You're mistaken.
Amit is a Hebrew name which has the meaning "Friend" but it's not correct to say that Amit is the Hebrew word for friend. Just because the name's origin is Italian doesn't mean it's a current Italian word for something.
Name of Cafe, Name of King
The name Arduino comes from the cafe the founder(s) frequented where they thought of the project . Cafe Arduino takes it's name from medievel king that briefly ruled the area . I can't find my source right now but it's something like King of Ardo , therefore he was an Arduino , hence Cafe Arduino , hence the Arduino Project . Much of this comes from the IEEE article a few months ago .
Unfortunately, the lack of research in the first sentence makes me doubt the entire 5 page article . I'll glance at it later anyway in case there's something useful . - MC
I'm from Italy, therefore I
I'm from Italy, therefore I can give a definitive answer. Arduino is the name of a bar in Ivrea (Piedmont, Italy) frequented by the (Italian) creators of the open source framework. The bar was named after Arduin of Ivrea, first King of Italy from 1002 to 1017. By the way, Arduino is not a common name nowadays in Italy
actuators
The IO pins can't drive much: 5v and 40ma each, not to exceed 120ma total. You need a tiny relay to do anything more than power an LED, and even there you need to limit the current to prevent burnout. I have had luck with tiny reed relays connected directly to the IO pins. See www.elexp.com p/n 22RD5, with 5v/500ohm coils. These draw only 10ma. Buy more than you need and test them all. Two of the 12 I ordered had open coils. And put a diode across any relay coils. The Arduino already has pull-up/pull-down diodes on all the pins, which is why the AD pins read around 2.5v if not connected. Remember, when you stop applying any power to a coil, the magnetic field collapses which produces a brief voltage spike. Also, any of the above reed relays can also be used as magnetic sensors to detect an open window, for instance. Just glue a small magnet nearby.
Figures 2 and 3
The LEDs shown in figures 2 and 3 are shorted as illustrated. The edge rows on most (if not all) breadboards are buses.
Ground wires and tty
Right. Also the ground wire of the rightmost LED does not connect anywhere. It should be in the same column as the LED it is supposed to ground.
If you are using a recent version of Arduino (and a newer original board) you are more likely to find /dev/ttyUSB0 that /dev/ttyACM0. (Just a typo, but /dev/tttyACM0 won't do any good at all).