Skip to content

Instantly share code, notes, and snippets.

@alfongj
Created April 20, 2013 08:14
Show Gist options
  • Save alfongj/5425213 to your computer and use it in GitHub Desktop.
Save alfongj/5425213 to your computer and use it in GitHub Desktop.
S107 IR-RC Helicopter controller via Processing and Arduino Mega. Adapted from http://www.diyphonegadgets.com/2012/04/tutorial-how-to-control-ir-helicopter.html It would be great if someone rewrote the Arduino part in a better way, using timers or one of the IR libraries out there :D
//Arduino code to control a helicotper.
int IRledPin = 12; // Connect IR LED to pin 12
int incomingByte = 0;
String incomingString;
int pulseValues[33];
int pulseLength = 0;
void setup() {
// initialize the IR digital pin as an output:
pinMode(IRledPin, OUTPUT);
Serial.begin(9600);
for (int i=0; i < 33; i++)
pulseValues[i] = 0;
}
void loop()
{
SendCode();
}
void pulseIR(long microsecs) {
cli(); // this turns off any background interrupts
while (microsecs > 0) {
// 38 kHz is about 13 microseconds high and 13 microseconds low
digitalWrite(IRledPin, HIGH); // this takes about 3 microseconds to happen
delayMicroseconds(10); // hang out for 10 microseconds
digitalWrite(IRledPin, LOW); // this also takes about 3 microseconds
delayMicroseconds(10); // hang out for 10 microseconds
// so 26 microseconds altogether
microsecs -= 26;
}
sei(); // this turns them back on
}
void Zero()
{
pulseIR(300);
delayMicroseconds(300);
pulseLength += 600;
}
void One()
{
pulseIR(300);
delayMicroseconds(600);
pulseLength += 900;
}
void sendPulseValue(int pulseValue)
{
if (pulseValue == 1)
One();
else
Zero();
}
void checkPulseChanges()
{
if (Serial.available() > 0)
{
incomingByte = Serial.read();
//Pulse 1
if (incomingByte == 'a')
pulseValues[0] = 0;
if (incomingByte == 'A')
pulseValues[0] = 1;
//Pulse 2
if (incomingByte == 'b')
pulseValues[1] = 0;
if (incomingByte =='B')
pulseValues[1] = 1;
//Pulse 3
if (incomingByte == 'c')
pulseValues[2] = 0;
if (incomingByte == 'C')
pulseValues[2] = 1;
//Pulse 4
if (incomingByte == 'd')
pulseValues[3] = 0;
if (incomingByte == 'D')
pulseValues[3] = 1;
//Pulse 5
if (incomingByte == 'e')
pulseValues[4] = 0;
if (incomingByte == 'E')
pulseValues[4] = 1;
//Pulse 6
if (incomingByte == 'f')
pulseValues[5] = 0;
if (incomingByte == 'F')
pulseValues[5] = 1;
//Pulse 7
if (incomingByte == 'g')
pulseValues[6] = 0;
if (incomingByte == 'G')
pulseValues[6] = 1;
//Pulse 8
if (incomingByte == 'h')
pulseValues[7] = 0;
if (incomingByte == 'H')
pulseValues[7] = 1;
//Pulse 9
if (incomingByte == 'i')
pulseValues[8] = 0;
if (incomingByte == 'I')
pulseValues[8] = 1;
//Pulse 10
if (incomingByte == 'j')
pulseValues[9] = 0;
if (incomingByte == 'J')
pulseValues[9] = 1;
//Pulse 11
if (incomingByte == 'k')
pulseValues[10] = 0;
if (incomingByte == 'K')
pulseValues[10] = 1;
//Pulse 12
if (incomingByte == 'l')
pulseValues[11] = 0;
if (incomingByte == 'L')
pulseValues[11] = 1;
//Pulse 13
if (incomingByte == 'm')
pulseValues[12] = 0;
if (incomingByte == 'M')
pulseValues[12] = 1;
//Pulse 14
if (incomingByte == 'o')
pulseValues[13] = 0;
if (incomingByte == 'O')
pulseValues[13] = 1;
//Pulse 15
if (incomingByte == 'p')
pulseValues[14] = 0;
if (incomingByte == 'P')
pulseValues[14] = 1;
//Pulse 16
if (incomingByte == 'q')
pulseValues[15] = 0;
if (incomingByte == 'Q')
pulseValues[15] = 1;
//Pulse 17
if (incomingByte == 'r')
pulseValues[16] = 0;
if (incomingByte == 'R')
pulseValues[16] = 1;
//Pulse 18
if (incomingByte == 's')
pulseValues[17] = 0;
if (incomingByte == 'S')
pulseValues[17] = 1;
//Pulse 19
if (incomingByte == 't')
pulseValues[18] = 0;
if (incomingByte == 'T')
pulseValues[18] = 1;
//Pulse 20
if (incomingByte == 'u')
pulseValues[19] = 0;
if (incomingByte == 'U')
pulseValues[19] = 1;
//Pulse 21
if (incomingByte == 'v')
pulseValues[20] = 0;
if (incomingByte == 'V')
pulseValues[20] = 1;
//Pulse 22
if (incomingByte == 'w')
pulseValues[21] = 0;
if (incomingByte == 'W')
pulseValues[21] = 1;
//Pulse 23
if (incomingByte == 'x')
pulseValues[22] = 0;
if (incomingByte == 'X')
pulseValues[22] = 1;
//Pulse 24
if (incomingByte == 'y')
pulseValues[23] = 0;
if (incomingByte == 'Y')
pulseValues[23] = 1;
//Pulse 25
if (incomingByte == 'z')
pulseValues[24] = 0;
if (incomingByte == 'Z')
pulseValues[24] = 1;
//Pulse 26
if (incomingByte == '1')
pulseValues[25] = 0;
if (incomingByte == '2')
pulseValues[25] = 1;
//Pulse 27
if (incomingByte == '3')
pulseValues[26] = 0;
if (incomingByte == '4')
pulseValues[26] = 1;
//Pulse 28
if (incomingByte == '5')
pulseValues[27] = 0;
if (incomingByte == '6')
pulseValues[27] = 1;
//Pulse 29
if (incomingByte == '7')
pulseValues[28] = 0;
if (incomingByte == '8')
pulseValues[28] = 1;
//Pulse 30
if (incomingByte == '9')
pulseValues[29] = 0;
if (incomingByte == '!')
pulseValues[29] = 1;
//Pulse 31
if (incomingByte == '@')
pulseValues[30] = 0;
if (incomingByte == '#')
pulseValues[30] = 1;
//Pulse 32
if (incomingByte == '$')
pulseValues[31] = 0;
if (incomingByte == '%')
pulseValues[31] = 1;
//Pulse 33
if (incomingByte == '^')
pulseValues[32] = 0;
if (incomingByte == '&')
pulseValues[32] = 1;
}
}
void SendCode() {
while (true)
{
checkPulseChanges();
pulseIR(4000);
delayMicroseconds(2000);
pulseLength=6000;
sendPulseValue(pulseValues[0]);
sendPulseValue(pulseValues[1]);
sendPulseValue(pulseValues[2]);
sendPulseValue(pulseValues[3]);
sendPulseValue(pulseValues[4]);
sendPulseValue(pulseValues[5]);
sendPulseValue(pulseValues[6]);
sendPulseValue(pulseValues[7]);
sendPulseValue(pulseValues[8]);
sendPulseValue(pulseValues[9]);
sendPulseValue(pulseValues[10]);
sendPulseValue(pulseValues[11]);
sendPulseValue(pulseValues[12]);
sendPulseValue(pulseValues[13]);
sendPulseValue(pulseValues[14]);
sendPulseValue(pulseValues[15]);
sendPulseValue(pulseValues[16]);
sendPulseValue(pulseValues[17]);
sendPulseValue(pulseValues[18]);
sendPulseValue(pulseValues[19]);
sendPulseValue(pulseValues[20]);
sendPulseValue(pulseValues[21]);
sendPulseValue(pulseValues[22]);
sendPulseValue(pulseValues[23]);
sendPulseValue(pulseValues[24]);
sendPulseValue(pulseValues[25]);
sendPulseValue(pulseValues[26]);
sendPulseValue(pulseValues[27]);
sendPulseValue(pulseValues[28]);
sendPulseValue(pulseValues[29]);
sendPulseValue(pulseValues[30]);
sendPulseValue(pulseValues[31]);
//Footer
pulseIR(360);
delayMicroseconds( (28600 - pulseLength) );
delay(20);
}
}
import controlP5.*;
import processing.serial.*;
import controlP5.*;
ControlP5 controlP5;
CheckBox checkbox;
Button b;
float boxX;
float boxY;
int boxSize = 20;
boolean mouseOverBox = false;
byte[] previousFlags = new byte[32];
byte[] flagsToSend = new byte[32];
Serial port;
String outString;
int helicopterUpSpeed = 0;
int helicopterPitch = 63;
int helicopterYaw = 68;
void setup() {
size(640, 480);
controlP5 = new ControlP5(this);
checkbox = controlP5.addCheckBox("checkBox", 20, 20);
// make adjustments to the layout of a checkbox.
checkbox.setColorForeground(color(120));
checkbox.setColorActive(color(255));
checkbox.setColorLabel(color(128));
checkbox.setItemsPerRow(8);
checkbox.setSpacingColumn(30);
checkbox.setSpacingRow(10);
// add items to a checkbox.
checkbox.addItem("1", 0);
checkbox.addItem("2", 0);
checkbox.addItem("3", 0);
checkbox.addItem("4", 0);
checkbox.addItem("5", 0);
checkbox.addItem("6", 0);
checkbox.addItem("7", 0);
checkbox.addItem("8", 0);
checkbox.addItem("9", 0);
checkbox.addItem("10", 0);
checkbox.addItem("11", 0);
checkbox.addItem("12", 0);
checkbox.addItem("13", 0);
checkbox.addItem("14", 0);
checkbox.addItem("15", 0);
checkbox.addItem("16", 0);
checkbox.addItem("17", 0);
checkbox.addItem("18", 0);
checkbox.addItem("19", 0);
checkbox.addItem("20", 0);
checkbox.addItem("21", 0);
checkbox.addItem("22", 0);
checkbox.addItem("23", 0);
checkbox.addItem("24", 0);
checkbox.addItem("25", 0);
checkbox.addItem("26", 0);
checkbox.addItem("27", 0);
checkbox.addItem("28", 0);
checkbox.addItem("29", 0);
checkbox.addItem("30", 0);
checkbox.addItem("31", 0);
checkbox.addItem("32", 0);
checkbox.deactivateAll();
controlP5.addButton("Up", 0, 120, 120, 35, 20);
controlP5.addButton("Down", 0, 120, 160, 35, 20);
controlP5.addButton("Forward", 0, 180, 120, 45, 20);
controlP5.addButton("Backward", 0, 180, 160, 45, 20);
controlP5.addButton("TurnLeft", 0, 60, 120, 40, 20);
controlP5.addButton("TurnRight", 0, 60, 160, 40, 20);
port = new Serial(this, Serial.list()[0], 9600);
for (int i=0;i<32;i++)
{
flagsToSend[i] = 0;
previousFlags[i] = 0;
}
addMouseWheelListener(new java.awt.event.MouseWheelListener() {
public void mouseWheelMoved(java.awt.event.MouseWheelEvent evt) {
mouseWheel(evt.getWheelRotation());
}
}
);
startSetUp();
}
//Incremental like bits
//0000, 0001, 0010, 0011, 0100, etc
void Up()
{
String currentSpeed = binary(helicopterUpSpeed, 7);
if(helicopterUpSpeed <= 125)
helicopterUpSpeed += 1;
String newSpeed = binary(helicopterUpSpeed, 7);
setNewSpeed(currentSpeed, newSpeed);
}
void Down()
{
String currentSpeed = binary(helicopterUpSpeed, 7);
if (helicopterUpSpeed > 0)
helicopterUpSpeed -= 1;
String newSpeed = binary(helicopterUpSpeed, 7);
setNewSpeed(currentSpeed, newSpeed);
}
void Backward()
{
String currentSpeed = binary(helicopterPitch, 7);
helicopterPitch += 1;
String newSpeed = binary(helicopterPitch, 7);
setNewPitch(currentSpeed, newSpeed);
}
void Forward()
{
String currentSpeed = binary(helicopterPitch, 7);
helicopterPitch -= 1;
String newSpeed = binary(helicopterPitch, 7);
setNewPitch(currentSpeed, newSpeed);
}
void TurnLeft()
{
String currentSpeed = binary(helicopterYaw, 7);
helicopterYaw -= 1;
String newSpeed = binary(helicopterYaw, 7);
setNewYaw(currentSpeed, newSpeed);
}
void TurnRight()
{
String currentSpeed = binary(helicopterYaw, 7);
helicopterYaw += 1;
String newSpeed = binary(helicopterYaw, 7);
setNewYaw(currentSpeed, newSpeed);
}
void setNewSpeed(String currentSpeed, String newSpeed)
{
//Compare each bit and see if it needs changing.
if (newSpeed.charAt(6) != currentSpeed.charAt(6) )
checkbox.toggle(23);
if (newSpeed.charAt(5) != currentSpeed.charAt(5) )
checkbox.toggle(22);
if (newSpeed.charAt(4) != currentSpeed.charAt(4) )
checkbox.toggle(21);
if (newSpeed.charAt(3) != currentSpeed.charAt(3) )
checkbox.toggle(20);
if (newSpeed.charAt(2) != currentSpeed.charAt(2) )
checkbox.toggle(19);
if (newSpeed.charAt(1) != currentSpeed.charAt(1) )
checkbox.toggle(18);
if (newSpeed.charAt(0) != currentSpeed.charAt(0) )
checkbox.toggle(17);
}
void setNewPitch(String currentSpeed, String newSpeed)
{
if (newSpeed.charAt(6) != currentSpeed.charAt(6) )
checkbox.toggle(15);
if (newSpeed.charAt(5) != currentSpeed.charAt(5) )
checkbox.toggle(14);
if (newSpeed.charAt(4) != currentSpeed.charAt(4) )
checkbox.toggle(13);
if (newSpeed.charAt(3) != currentSpeed.charAt(3) )
checkbox.toggle(12);
if (newSpeed.charAt(2) != currentSpeed.charAt(2) )
checkbox.toggle(11);
if (newSpeed.charAt(1) != currentSpeed.charAt(1) )
checkbox.toggle(10);
if (newSpeed.charAt(0) != currentSpeed.charAt(0) )
checkbox.toggle(9);
}
void setNewYaw(String currentSpeed, String newSpeed)
{
if (newSpeed.charAt(6) != currentSpeed.charAt(6) )
checkbox.toggle(7);
if (newSpeed.charAt(5) != currentSpeed.charAt(5) )
checkbox.toggle(6);
if (newSpeed.charAt(4) != currentSpeed.charAt(4) )
checkbox.toggle(5);
if (newSpeed.charAt(3) != currentSpeed.charAt(3) )
checkbox.toggle(4);
if (newSpeed.charAt(2) != currentSpeed.charAt(2) )
checkbox.toggle(3);
if (newSpeed.charAt(1) != currentSpeed.charAt(1) )
checkbox.toggle(2);
if (newSpeed.charAt(0) != currentSpeed.charAt(0) )
checkbox.toggle(1);
}
void startSetUp()
{
//First clear the arduino.
port.write('a');
port.write('b');
port.write('c');
port.write('d');
port.write('e');
port.write('f');
port.write('g');
port.write('h');
port.write('i');
port.write('j');
port.write('k');
port.write('l');
port.write('m');
port.write('o');
port.write('p');
port.write('q');
port.write('r');
port.write('s');
port.write('t');
port.write('u');
port.write('v');
port.write('w');
port.write('x');
port.write('y');
port.write('z');
port.write('1');
port.write('3');
port.write('5');
port.write('7');
port.write('9');
port.write('@');
port.write('$');
port.write('^');
//Set the pulse to the basic configuration.
checkbox.toggle(1);
checkbox.toggle(6);
checkbox.toggle(10);
checkbox.toggle(11);
checkbox.toggle(12);
checkbox.toggle(13);
checkbox.toggle(14);
checkbox.toggle(15);
checkbox.toggle(16);
checkbox.toggle(25);
checkbox.toggle(28);
checkbox.toggle(29);
checkbox.toggle(30);
}
void draw()
{
background(200);
text(" Current Speed: " + helicopterUpSpeed, 230, 135);
text(" Pitch: " + helicopterPitch, 230, 165);
text(" Yaw: " + helicopterYaw, 230, 195);
}
void controlEvent(ControlEvent theEvent) {
if (theEvent.isGroup()) {
for (int i=0;i<theEvent.group().arrayValue().length;i++)
{
byte n = (byte)theEvent.group().arrayValue()[i];
flagsToSend[i] = n;
//there was a change in the flags, send the update.
if (previousFlags[i] != flagsToSend[i])
{
println(i+":"+n);
if (i==0) {
if (n == 0) {
port.write('a');
}
else {
port.write('A');
}
}
if (i==1) {
if (n == 0) {
port.write('b');
}
else {
port.write('B');
}
}
if (i==2) {
if (n == 0) {
port.write('c');
}
else {
port.write('C');
}
}
if (i==3) {
if (n == 0) {
port.write('d');
}
else {
port.write('D');
}
}
if (i==4) {
if (n == 0) {
port.write('e');
}
else {
port.write('E');
}
}
if (i==5) {
if (n == 0) {
port.write('f');
}
else {
port.write('F');
}
}
if (i==6) {
if (n == 0) {
port.write('g');
}
else {
port.write('G');
}
}
if (i==7) {
if (n == 0) {
port.write('h');
}
else {
port.write('H');
}
}
if (i==8) {
if (n == 0) {
port.write('i');
}
else {
port.write('I');
}
}
if (i==9) {
if (n == 0) {
port.write('j');
}
else {
port.write('J');
}
}
if (i==10) {
if (n == 0) {
port.write('k');
}
else {
port.write('K');
}
}
if (i==11) {
if (n == 0) {
port.write('l');
}
else {
port.write('L');
}
}
if (i==12) {
if (n == 0) {
port.write('m');
}
else {
port.write('M');
}
}
if (i==13) {
if (n == 0) {
port.write('o');
}
else {
port.write('O');
}
}
if (i==14) {
if (n == 0) {
port.write('p');
}
else {
port.write('P');
}
}
if (i==15) {
if (n == 0) {
port.write('q');
}
else {
port.write('Q');
}
}
if (i==16) {
if (n == 0) {
port.write('r');
}
else {
port.write('R');
}
}
if (i==17) {
if (n == 0) {
port.write('s');
}
else {
port.write('S');
}
}
if (i==18) {
if (n == 0) {
port.write('t');
}
else {
port.write('T');
}
}
if (i==19) {
if (n == 0) {
port.write('u');
}
else {
port.write('U');
}
}
if (i==20) {
if (n == 0) {
port.write('v');
}
else {
port.write('V');
}
}
if (i==21) {
if (n == 0) {
port.write('w');
}
else {
port.write('W');
}
}
if (i==22) {
if (n == 0) {
port.write('x');
}
else {
port.write('X');
}
}
if (i==23) {
if (n == 0) {
port.write('y');
}
else {
port.write('Y');
}
}
if (i==24) {
if (n == 0) {
port.write('z');
}
else {
port.write('Z');
}
}
if (i==25) {
if (n == 0) {
port.write('1');
}
else {
port.write('2');
}
}
if (i==26) {
if (n == 0) {
port.write('3');
}
else {
port.write('4');
}
}
if (i==27) {
if (n == 0) {
port.write('5');
}
else {
port.write('6');
}
}
if (i==28) {
if (n == 0) {
port.write('7');
}
else {
port.write('8');
}
}
if (i==29) {
if (n == 0) {
port.write('9');
}
else {
port.write('!');
}
}
if (i==30) {
if (n == 0) {
port.write('@');
}
else {
port.write('#');
}
}
if (i==31) {
if (n == 0) {
port.write('$');
}
else {
port.write('%');
}
}
if (i==32) {
if (n == 0) {
port.write('^');
}
else {
port.write('&');
}
}
}
previousFlags[i]=n;
}
}
}
void mouseWheel(int delta) {
if (delta == 1)
Down();
else
Up();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment