﻿ Arduino Microcontrollers

Language
English

# Chapter 2: Arduino Demonstration

This is a simple board that demonstrates some of the capabilities of the Arduino. The video demonstrates controlling several green LEDs, an RGB (multi-color) LED, and a servo controlled by the input of a pressure sensor.

## 2.1 Lights

The green LEDs are hooked up to the Arduino like the following:

The above image shows how one LED is hooked up using pin 13. The rest of the LEDs are hooked up in a similar manner, from pins 12 down to 4.

The multi-color RGB LED is a bit more complex, but not by much. It is managed just like three regular LEDs, but we use a common positive power supply, and then control the electricity flowing to ground.

## 2.2 Sensors and Servos

The pressure sensor hooks between +5V and ground along with another resister to limit the current. There is another wire that feeds into the Arduino to measure the voltage drop.

The Arduino program reads that as a number between 0 and 1023, depending on how much pressure has been applied. It then converts that to a number between 0 and 254 by dividing by 4. That value is fed to the servo, and controls how far the servo arm moves.

## 2.3 Source Code

// Import servo library
#include <Servo.h>

// Global Variables
long currentPin=4;
Servo myservo;

// Setup function
void setup() {
// Attach the servo object to pin 3
myservo.attach(3);
// Set pins 1 to 13 for output.
for( int i=1; i <= 13; i++ )
pinMode(i, OUTPUT);
}

// Main program loop
void loop()
{
// Loop from pins 4 to 13
for( int i=4; i <= 13; i++ ) {
// Figure out if the LED should be lit
if( currentPin == i ) {
digitalWrite(i, HIGH); // On
} else {
digitalWrite(i, LOW);  // Off
}
}
// Set the next LED to light up next time
currentPin += 1;
// Reset back to the beginning
if( currentPin > 13 ) {
currentPin = 4;
}

// Get random color values
int r=random(0,256);
int g=random(0,256);
int b=random(0,256);

// Set red, green, and blue parts of LED
analogWrite(0, r);
analogWrite(1, g);
analogWrite(2, b);

// Read force sensor on pin A5 (possible range 0-1023)