Below is the code that will run the LCD and the Servo motor. The LCD is a required part of the project and we didn't have a specific need so we wanted to make it cool. When the hovercraft the LCD will read "System Charging". While driving the LCD will then read what direction it is driving. For example "slight left, slight right, left, and right". The servo code will turn the servo in increments of 30 degrees.
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
#include <LiquidCrystal.h> | |
#include <IRremote.h> | |
#include <Servo.h> | |
Servo myservo; | |
LiquidCrystal lcd(12,11,5,4,3,2); | |
int RECV_PIN = 7; | |
IRrecv irrecv(RECV_PIN); | |
decode_results results; | |
void setup() | |
{ | |
Serial.begin(9600); | |
irrecv.enableIRIn(); | |
lcd.begin(16, 2); | |
lcd.setCursor(0,0); | |
lcd.print("System Charging"); | |
myservo.attach(9); | |
} | |
void loop() | |
{ | |
if (irrecv.decode(&results)) { | |
Serial.println(results.value); | |
irrecv.resume(); | |
if(results.value == 1 || results.value == 2049) | |
{ | |
myservo.write(150); | |
lcd.setCursor(0,0); | |
lcd.print("Full Left "); | |
} | |
if(results.value == 4 || results.value == 2052) | |
{ | |
myservo.write(120); | |
lcd.setCursor(0,0); | |
lcd.print("Slight Left "); | |
} | |
if (results.value == 5 || results.value == 2053) | |
{ | |
myservo.write(90); | |
lcd.setCursor(0,0); | |
lcd.print("Straight Ahead "); | |
} | |
if (results.value == 6 || results.value == 2054) | |
{ | |
myservo.write(60); | |
lcd.setCursor(0,0); | |
lcd.print("Slight Right "); | |
} | |
if (results.value == 3 || results.value == 2051) | |
{ | |
myservo.write(30); | |
lcd.setCursor(0,0); | |
lcd.print("Full Right " ); | |
} | |
} | |
} |
Another required element to the project was the use of LED's. Again, we wanted to use this feature to make our hovercraft cooler. We decided to use red and blue LED lights. The code below is used to control the LED lights. When the Hovercraft turns a certain way, lights on that side will blink.
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
#include <IRremote.h> | |
#include <Servo.h> | |
int RECV_PIN = 7; | |
IRrecv irrecv(RECV_PIN); | |
int button = 0; | |
const int frontBlue = 11; | |
const int backBlue = 10; | |
const int leftRed = 12; | |
const int rightRed = 13; | |
const int topWhite = 9; | |
int motorPin = 8; | |
Servo servo1; | |
int pos = 0; | |
decode_results results; | |
void setup() | |
{ | |
Serial.begin(9600); | |
irrecv.enableIRIn(); | |
servo1.attach(3); | |
pinMode(frontBlue, OUTPUT); | |
pinMode(backBlue, OUTPUT); | |
pinMode(leftRed, OUTPUT); | |
pinMode(rightRed, OUTPUT); | |
pinMode(topWhite, OUTPUT); | |
} | |
void loop() | |
{ | |
digitalWrite(leftRed, HIGH); | |
digitalWrite(rightRed, HIGH); | |
digitalWrite(frontBlue, HIGH); | |
digitalWrite(backBlue, HIGH); | |
if(irrecv.decode(&results)) | |
{ | |
Serial.println(results.value); | |
irrecv.resume(); | |
if (results.value == 1 || results.value == 2049){ | |
digitalWrite(leftRed, HIGH); | |
delay(500); | |
digitalWrite(leftRed, LOW); | |
delay(500); | |
} | |
if (results.value == 4 || results.value == 2052){ | |
digitalWrite(leftRed, HIGH); | |
delay(500); | |
digitalWrite(leftRed, LOW); | |
delay(500); | |
} | |
if (results.value == 7 || results.value == 2055){ | |
digitalWrite(leftRed, HIGH); | |
delay(500); | |
digitalWrite(leftRed, LOW); | |
delay(500); | |
} | |
if (results.value == 34 || results.value == 2082){ | |
digitalWrite(leftRed, HIGH); | |
delay(500); | |
digitalWrite(leftRed, LOW); | |
delay(500); | |
} | |
if (results.value == 3674273534 || results.value == 96281711){ | |
digitalWrite(rightRed, HIGH); | |
delay(500); | |
digitalWrite(rightRed, LOW); | |
delay(500); | |
} | |
if (results.value == 2057 || results.value == 9){ | |
digitalWrite(rightRed, HIGH); | |
delay(500); | |
digitalWrite(rightRed, LOW); | |
delay(500); | |
} | |
if (results.value == 2054 || results.value == 6){ | |
digitalWrite(rightRed, HIGH); | |
delay(500); | |
digitalWrite(rightRed, LOW); | |
delay(500); | |
} | |
if (results.value == 2051 || results.value == 3){ | |
digitalWrite(rightRed, HIGH); | |
delay(500); | |
digitalWrite(rightRed, LOW); | |
delay(500); | |
} | |
if (results.value == 2891014758 || results.value == 3843267751){ | |
digitalWrite(frontBlue, HIGH); | |
delay(100); | |
digitalWrite(frontBlue, LOW); | |
delay(100); | |
} | |
if (results.value == 3826490130 || results.value == 2907792379){ | |
digitalWrite(backBlue, HIGH); | |
delay(100); | |
digitalWrite(backBlue, LOW); | |
delay(100); | |
} | |
} | |
} |
This code is used to control the big fan on the back of the hovercraft.
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
#include <IRremote.h> | |
int RECV_PIN = 7; | |
int num = 0; | |
const int motorPin = 9; | |
IRrecv irrecv(RECV_PIN); | |
int button = 0; | |
int Speed1 = 50; | |
int Speed2 = 100; | |
int Speed3 = 150; | |
int Speed4 = 200; | |
decode_results results; | |
void setup() | |
{ | |
Serial.begin(9600); | |
irrecv.enableIRIn(); | |
pinMode(motorPin, OUTPUT); | |
} | |
void loop() | |
{ | |
if(irrecv.decode(&results)) | |
{ | |
Serial.println(results.value); | |
irrecv.resume(); | |
if (results.value == 2891014758 || results.value == 3843267751) | |
{ | |
button++; | |
if (button > 5) | |
{ | |
button = 5; | |
} | |
Serial.println(button); | |
} | |
delay(50); | |
if (results.value == 2907792379 || results.value == 3826490130) | |
{ | |
button--; | |
if (button < 0) | |
{ | |
button = 0; | |
} | |
Serial.println(button); | |
} | |
} | |
delay(50); | |
if(button == 0){ | |
digitalWrite(motorPin, LOW); | |
} | |
if(button == 1){ | |
analogWrite(motorPin, Speed1); | |
} | |
if(button == 2){ | |
analogWrite(motorPin, Speed2); | |
} | |
if(button == 3){ | |
analogWrite(motorPin, Speed3); | |
} | |
if(button == 4){ | |
analogWrite(motorPin, Speed4); | |
} | |
if(button == 5){ | |
digitalWrite(motorPin, HIGH); | |
} | |
} | |
No comments:
Post a Comment