Friday, November 29, 2013

Friday, November 29
Tuesday, we wrote the code for our car that integrates an IR remote, two motors, and the RGB led light. The code will allow a user to drive the car using the IR remote, and with each different direction that the car goes, a different color light is lit. A copy of the code is given below.
#include <IRremote.h> // sets up the IR Remote
// The following integers are for the two wheel motors
int RECV_PIN = 8; // receiver pin
int leftForward = 9; // pin for the left wheel to go forward
int leftBackward = 10; // pin for left wheel to go backward
int rightForward = 6; // pin for right wheel to go forward
int rightBackward = 5; // pin for right wheel to go backward
// The following integers are for the RGB LED
const int Red = 2; // Red led
const int Green = 3; // Green led
const int Blue = 4; // Blue led
// The following values are the codes for the IR Remote buttons
long forward = 0x10EFA05F; // up arrow
long backward = 0x10EF00FF; // down arrow
long left = 0x10EF10EF; // left arrow
long right = 0x10EF807F; // right arrow
long center = 0x10EF20DF; // center circle
long A = 0x10EFF807; // "A" button
long B = 0x10EF7887; // "B" button
long C = 0x10EF58A7; //"C" button
long power = 0x10EFD827; // Power button
IRrecv irrecv(RECV_PIN);
decode_results results; // decodes the values of the remote codes
void setup()
{
Serial.begin(9600); // begins serial port
// The following "pinMode"'s set the wheel pins as outputs
pinMode(leftForward, OUTPUT);
pinMode(rightForward, OUTPUT);
pinMode(leftBackward, OUTPUT);
pinMode(rightBackward, OUTPUT);
// The following "pinMode"'s set the RGB pins as outputs
pinMode(Red, OUTPUT);
pinMode(Green, OUTPUT);
pinMode(Blue, OUTPUT);
irrecv.enableIRIn(); // Start the receiver
irrecv.blink13(true); // blink LED on P13 when IR signal is present
}
void loop()
{
if (irrecv.decode(&results))
{
Serial.println(results.value, HEX); // the serial monitor will read the code values when a button is pressed
if(results.value == power) // if the "Power" button is pressed:
{
delay(100);
// Both wheels stop spinning:
digitalWrite(rightBackward, LOW);
digitalWrite(leftBackward, LOW);
digitalWrite(leftForward, LOW);
digitalWrite(rightForward, LOW);
// LED shuts off:
digitalWrite(Red, LOW);
digitalWrite(Green, LOW);
digitalWrite(Blue, LOW);
}
if (results.value == forward) // if the up arrow button is pressed:
{
delay(100);
// Both wheels spin forward:
digitalWrite(leftForward, HIGH);
digitalWrite(rightForward, HIGH);
digitalWrite(rightBackward, LOW);
digitalWrite(leftBackward, LOW);
// LED turns GREEN:
digitalWrite(Red, LOW);
digitalWrite(Green, HIGH);
digitalWrite(Blue, LOW);
}
if (results.value == backward) // if the down arrow is pressed:
{
delay(100);
// Both wheels spin backward:
digitalWrite(rightBackward, HIGH);
digitalWrite(leftBackward, HIGH);
digitalWrite(leftForward, LOW);
digitalWrite(rightForward, LOW);
// LED turns Red:
digitalWrite(Red, HIGH);
digitalWrite(Green, LOW);
digitalWrite(Blue, LOW);
}
if (results.value == left) // if the left arrow is pressed:
{
delay(100);
// Left wheel spins forward:
digitalWrite(leftForward, HIGH);
digitalWrite(leftBackward, LOW);
digitalWrite(rightForward, LOW);
digitalWrite(rightBackward, LOW);
// LED turns Blue:
digitalWrite(Red, LOW);
digitalWrite(Green, LOW);
digitalWrite(Blue, HIGH);
}
if (results.value == right) // if the right arrow is pressed:
{
delay(100);
// Right wheel spins forward:
digitalWrite(rightForward, HIGH);
digitalWrite(leftBackward, LOW);
digitalWrite(leftForward, LOW);
digitalWrite(rightBackward, LOW);
// LED turns Purple:
digitalWrite(Red, HIGH);
digitalWrite(Green, LOW);
digitalWrite(Blue, HIGH);
}
if (results.value == center) // if the center circle button is pressed:
{
delay(100);
// Both wheels stop spinning:
digitalWrite(rightBackward, LOW);
digitalWrite(leftBackward, LOW);
digitalWrite(leftForward, LOW);
digitalWrite(rightForward, LOW);
// LED turns White:
digitalWrite(Red, HIGH);
digitalWrite(Green, HIGH);
digitalWrite(Blue, HIGH);
}
irrecv.resume(); // Receive the next value
}
}
view raw gistfile1.txt hosted with ❤ by GitHub
The motors that we are using are gear motors with two wires that can connect directly into digital PWM pins of the arduino board.  One of the wires allows the motor to spin clockwise, and the other wire allows for counter clockwise rotation.  Because of this, coding our car was alot easier to do than we originally thought.  An H-bridge was also not necessary because of the simplicity of the motor pins.  We used the IR library that Prof. Sullivan posted to the Design Lab blog, which made setting up and using the IR remote very easy.  The RGB led was integrated into the code to help tell what is going on when a button is pressed. With the way the code is written now, the led glows green when going forward, red when going backward, blue when it turns left, purple when it turns right, and white when it is not moving.
A video of the code uploaded to the arduino board with the motors and led attached is below.



This final code might be slightly altered if we decide to change how the car is going to turn. The way it is now, to turn either left or right, the corresponding wheel will spin while the other is stopped. We also are thinking of ways to integrate the "A", "B", and "C" buttons into the car.  Right now we are thinking of doing pre-set paths that the car will follow.

We also sent in our parts for 3D printing and laser cutting.  We needed to print 4 parts, two of our L-brackets and two of the motor brackets.  Images of these two final models submitted are below.



The code took about an hour to write and add comments. We should have our new motors and our printed and laser cut parts altogether by early next week so that we can begin building the working model.

No comments:

Post a Comment