Last active
October 2, 2015 22:48
-
-
Save liamgriffiths/2339413 to your computer and use it in GitHub Desktop.
blimp code
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
/* | |
borrowed: | |
- IR code/library from http://www.arcfn.com/2009/08/multi-protocol-infrared-remote-library.html | |
- ultrasonic sensor code from http://arduino.cc/en/Tutorial/Ping | |
*/ | |
#include <IRremote.h> | |
#include <Servo.h> | |
// pins pins pins | |
const int IR_SENSOR = 2; // bow && stern? | |
const int PORT_MOTOR = 3; | |
const int STARBOARD_MOTOR = 5; | |
const int THRUSTER_SERVO = 6; | |
const int ULTRASONIC_SENSOR = 9; | |
// apple remote buttons | |
const int PLAY = 2011275421; | |
const int VOLUMEUP = 2011254941; | |
const int VOLUMEDOWN = 2011246749; | |
const int PREVIOUS = 2011271325; | |
const int NEXT = 2011259037; | |
const int MENU = 2011283613; | |
// some othe variables the blimp needs | |
const float e = 2.7182818284590452354; | |
IRrecv ir_reciever(IR_SENSOR); | |
decode_results ir_results; | |
Servo servo; | |
long altitude = 0; | |
long ideal_altitude = 120; // in cm | |
int altitude_change_threshold = 100; | |
int thruster_position = 90; // 180 total degrees | |
int port_motor_speed = 0; // 0-255, though it seems the motors don't really start until around ~50 | |
int starboard_motor_speed = 0; // 0-255 | |
void setup(){ | |
Serial.begin(9600); | |
ir_reciever.enableIRIn(); | |
servo.attach(THRUSTER_SERVO); | |
servo.write(90); | |
pinMode(PORT_MOTOR, OUTPUT); | |
pinMode(STARBOARD_MOTOR, OUTPUT); | |
} | |
void loop(){ | |
//handle_ir_input(); | |
update_thruster(); | |
update_motors_speed(); | |
delay(100); | |
} | |
void handle_ir_input(){ | |
if (ir_reciever.decode(&ir_results)) { | |
switch(ir_results.value){ | |
case PLAY: break; | |
case MENU: break; | |
case VOLUMEUP: break; | |
case VOLUMEDOWN: break; | |
case NEXT: break; | |
case PREVIOUS: break; | |
default: break; | |
} | |
ir_reciever.resume(); | |
} | |
} | |
long current_altitude(){ | |
pinMode(ULTRASONIC_SENSOR, OUTPUT); | |
digitalWrite(ULTRASONIC_SENSOR, LOW); | |
delayMicroseconds(2); | |
digitalWrite(ULTRASONIC_SENSOR, HIGH); | |
delayMicroseconds(5); | |
digitalWrite(ULTRASONIC_SENSOR, LOW); | |
pinMode(ULTRASONIC_SENSOR, INPUT); | |
long duration = pulseIn(ULTRASONIC_SENSOR, HIGH); | |
long new_altitude = microsecondsToCentimeters(duration); | |
// sometimes it seems the sensor gives a bogus reading, i think this should cancel it out | |
//return abs(altitude - new_altitude) > altitude_change_threshold || new_altitude == 0 ? altitude : new_altitude; | |
return new_altitude; | |
} | |
long microsecondsToCentimeters(long microseconds){ | |
// The speed of sound is 340 m/s or 29 microseconds per centimeter. | |
// The ping travels out and back, so to find the distance of the | |
// object we take half of the distance travelled. | |
return microseconds / 29 / 2; | |
} | |
void update_motors_speed(){ | |
analogWrite(STARBOARD_MOTOR, starboard_motor_speed); | |
analogWrite(PORT_MOTOR, port_motor_speed); | |
} | |
void update_thruster(){ | |
long altitude = current_altitude(); | |
if(altitude == 0) return; | |
signed long diff = ideal_altitude - altitude; | |
if(diff > 0 && diff > 90) diff = 90; | |
if(diff < 0 && diff < -90) diff = -90; | |
int angle = (int) (90 + diff); | |
servo.write(angle); | |
Serial.print("altitude is "); | |
Serial.print(altitude); | |
Serial.print(" and angle is "); | |
Serial.print(angle); | |
Serial.println(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment