For the first part of this exercise, we were to build a DIP Controlled LED Array. The setup used 5 digital pins on the Arduino board that were connected to ports 4 through 8 of the DIP switch. Afterwards, 8 LED lights were connected to the digital pins of the Arduino board via resistors. The result looked like this:
The code below was compiled and uploaded to the board. Now, whenever a switch was thrown from the DIP, a corresponding LED lit up as shown below:
The purpose of this exercise was to give us the knowledge on how to control the LEDs via the state of a pin. The code used is displayed below:
// DIP to LED
// Joe Pardue September 29, 2009
void setup()
{
// Init pins for input
pinMode(8, INPUT); // DIP 0
digitalWrite(8,HIGH); // Turn on pullup
pinMode(9, INPUT); // DIP 1
digitalWrite(9,HIGH); // Turn on pullup
pinMode(10, INPUT); // DIP 2
digitalWrite(10,HIGH); // Turn on pullup
pinMode(11, INPUT); // DIP 3
digitalWrite(11,HIGH); // Turn on pullup
pinMode(12, INPUT); // DIP 4
digitalWrite(12,HIGH); // Turn on pullup
//pinMode(13, INPUT); // DIP 5
//digitalWrite(13,HIGH); // Turn on pullup
// Init pins for output
pinMode(0, OUTPUT); // LED 0
pinMode(1, OUTPUT); // LED 1
pinMode(2, OUTPUT); // LED 2
pinMode(3, OUTPUT); // LED 3
pinMode(4, OUTPUT); // LED 4
pinMode(5, OUTPUT); // LED 5
pinMode(6, OUTPUT); // LED 5
pinMode(7, OUTPUT); // LED 6
// preset to turn LEDs off
digitalWrite(0,HIGH);
digitalWrite(1,HIGH);
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
digitalWrite(6,HIGH);
digitalWrite(7,HIGH);
}
void loop()
{
digitalWrite(0,digitalRead(8)); // show DIP 0 state on LED 0
digitalWrite(1,digitalRead(9)); // show DIP 1 state on LED 1
digitalWrite(2,digitalRead(10)); // show DIP 2 state on LED 2
digitalWrite(3,digitalRead(11)); // show DIP 3 state on LED 3
digitalWrite(4,digitalRead(12)); // show DIP 4 state on LED 4
}
———————————————————————————————————————–
The Arduino can also be used to play music! Using the width of a pulse, a tone can be played for all to hear. By knowing the frequency at which a musical key is related with, the duration at which a pulse should last is known. It is simply 1 / frequency = period. All is described in the Arduino Melody tutorial page: http://www.arduino.cc/en/Tutorial/Melody
A simple melody is played below.
Sound familiar? It should. It’s a childhood melody Twinkle Twinkle Little Star.
The code for this project is displayed below:
int speakerPin = 9;
int length = 15; // the number of notes
char notes[] = "ccggaagffeeddc "; // a space represents a rest
int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
int tempo = 300;
void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
}
}
void playNote(char note, int duration) {
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
// play the tone corresponding to the note name
for (int i = 0; i < 8; i++) {
if (names[i] == note) {
playTone(tones[i], duration);
}
}
}
void setup() {
pinMode(speakerPin, OUTPUT);
}
void loop() {
for (int i = 0; i < length; i++) {
if (notes[i] == ' ') {
delay(beats[i] * tempo); // rest
} else {
playNote(notes[i], beats[i] * tempo);
}
// pause between notes
delay(tempo / 2);
}
}
———————————————————————————————————————–
The IR sensors are devices that measure distance via the speed of light. The time it take to go from the sensor to the object and back is converted to distance and then divided by two. The following code can be used to retrieve the distance in centimeters:
#define IR 0
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.println(readIR(IR));
delay(500);
}
float readIR(byte pin)
{
int tmp =0;
for(int i=0; i<5 ; i++)
{
tmp = tmp + analogRead(pin);
}
tmp = tmp / 5;
if (tmp < 3) return -1; // invalid value
return (6787.0 /((float)tmp – 3.0)) – 4.0;
}
The setup of the IR sensor is simply 5V, Ground, and a Data pin. That’s all that was used in the setup below.
The sensor (on the right) is reading the distance of the breadboard (on the left). I have placed a ruler starting from the IR sensor and placing the bread board 15cm away. Using the serial monitor and reading values every half a second, the results varied only a little.
———————————————————————————————————————–
H-Bridges are Integrated Chips (IC) that enable us to control the speed and direction of a motor. This diagram shows our robot’s implementation of the IC’s. We decided to use one H-Bridge (which controls two motors) to control one side of the robot. The Not-Gates helped us reduce the number of pins for a more efficient outlook. The capacitors are used as to filter out any noise or voltage spikes that would cause undesired operation.
To make the robots move forward and backwards, the following code was used:
// Most Arduino boards provide six 8-bit PWM (Pulse Width Modulated) signal outputs: pins 3, 5, 6, 9, 10, and 11
#define motor_left_speed 9 // Pin for the Left Motor Speed connected to PWM
#define motor_left_dir 6 // Pin for the Left Motor Direction connected in conjunction w/ NOT-Gate
#define motor_right_speed 10 // Pin for the Right Motor Speed connected to PWM
#define motor_right_dir 4 // Pin for the Right Motor Direction connected in conjunction w/ NOT-Gate
// PWM Signal – Range: 0 – 255
int velocity = 200;
void setup()
{
}
void loop()
{
// Forwards
analogWrite(motor_left_speed, velocity);
analogWrite(motor_right_speed, velocity);
digitalWrite(motor_left_dir, HIGH);
digitalWrite(motor_right_dir, HIGH);
delay(2000);
// Stop
analogWrite(motor_left_speed, 0);
analogWrite(motor_right_speed, 0);
delay(2000);
// Backwards
analogWrite(motor_left_speed, velocity);
analogWrite(motor_right_speed, velocity);
digitalWrite(motor_left_dir, LOW);
digitalWrite(motor_right_dir, LOW);
delay(2000);
// Stop
analogWrite(motor_left_speed, 0);
analogWrite(motor_right_speed, 0);
delay(2000);
}
I did not implement exact degree rotation because this involves the optical encoders. This task still poses a challenge to complete accurately.














