I also worked on code for the IR remote since we still don't know if using Bluetooth is viable. However I do think the IR remote is the best way to go. With this code (assuming it works perfect), all that needs to be added are the hexadecimal codes for each button [a quick test solves this] and the light code we already have working.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Mike Igoe | |
//IR Test Code | |
//Preparing for using the IR remote | |
#include <IRremote.h> //IR | |
#include <Servo.h> // servo library | |
//Assigning IR to pin 11 | |
int RECV_PIN = 11; | |
IRrecv irrecv(RECV_PIN); | |
//Store IR results | |
decode_results results; | |
/* | |
Holding place for when we can test what each button does! | |
//These are the speed buttons (0-9) | |
long button1 = 0xFF728D; | |
long button2 = 0xFF52AD; | |
long button3 = 0xFF929D; | |
long button4 = 0xFFB24D; | |
long button5 = ; | |
long button6 = ; | |
long button7 = ; | |
long button8 = ; | |
long button9 = ; | |
long button0 = ; | |
//These are the direction buttons (left/right) | |
long buttonLeft = ; | |
long buttonRight = ; | |
*/ | |
const int motorPin = 9; //motor | |
int speed; | |
Servo servo1; // servo control object | |
int angle = 90; // initial angle that will then be changed for direction | |
void setup() | |
{ | |
Serial.begin(9600); | |
irrecv.enableIRIn(); // Start the receiver | |
pinMode(motorPin, OUTPUT); //setup motor | |
servo1.attach(9); //servo to pin 9 | |
servo1.write(angle); //Start servo at 90 degrees | |
} | |
void loop() | |
{ | |
if (irrecv.decode(&results)) | |
{ | |
if (results.value == button1) //Speed One | |
{ | |
speed = 255/9; | |
analogWrite(motorPin, speed); | |
} | |
if (results.value == button2) //Speed Two | |
{ | |
speed = 255/8; | |
analogWrite(motorPin, speed); | |
} | |
if (results.value == button3) //Speed Three | |
{ | |
speed = 255/7; | |
analogWrite(motorPin, speed); | |
} | |
if (results.value == button4) //Speed Four | |
{ | |
speed = 255/6; | |
analogWrite(motorPin, speed); | |
} | |
if (results.value == button5) //Speed Five | |
{ | |
speed = 255/5; | |
analogWrite(motorPin, speed); | |
} | |
if (results.value == button6) //Speed Six | |
{ | |
speed = 255/4; | |
analogWrite(motorPin, speed); | |
} | |
if (results.value == button7) //Speed Seven | |
{ | |
speed = 255/3; | |
analogWrite(motorPin, speed); | |
} | |
if (results.value == button8) //Speed Eight | |
{ | |
speed = 255/2; | |
analogWrite(motorPin, speed); | |
} | |
if (results.value == button9) //Speed Nine | |
{ | |
speed = 255; | |
analogWrite(motorPin, speed); | |
} | |
if (results.value == button0) //Stop | |
{ | |
speed = 0; | |
analogWrite(motorPin, speed); | |
} | |
if (results.value == buttonLeft) //Rudder left, Boat right | |
{ | |
angle = angle - 10; | |
servo1.write(angle); | |
} | |
if (results.value == buttonRight) //Rudder right, Boat left | |
{ | |
angle = angle + 10; | |
servo1.write(angle); | |
} | |
} | |
} | |
The basic premis of the code is that buttons 0-9 on the remote will represent different speeds and the left/right arrows will indicate direction. All of our other ideas can be implemented (boat path patterns, adjusting light brightness, etc.) once we get this to work properly and get it in water!
- Mike Igoe
No comments:
Post a Comment