Skip to content

Instantly share code, notes, and snippets.

@GroverChouT
Created April 15, 2017 08:27
Show Gist options
  • Save GroverChouT/634700c114ab3a807892c4d129e1a0e7 to your computer and use it in GitHub Desktop.
Save GroverChouT/634700c114ab3a807892c4d129e1a0e7 to your computer and use it in GitHub Desktop.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <IRremote.h>
const int en[] = {10, 5};
const int in[] = {9, 8, 7, 6};
const int ir = 11;
const int beep = 4;
const int cannon = 12;
LiquidCrystal_I2C lcd(0x27, 16, 2);
IRrecv irrecv(ir);
decode_results results;
int mspeed = 100;
int result = 0;
void motor_set(int in1, int in2, int in3, int in4) {
digitalWrite(in[0], in1);
digitalWrite(in[1], in2);
digitalWrite(in[2], in3);
digitalWrite(in[3], in4);
}
void go_forward() {
motor_set(HIGH, LOW, LOW, HIGH);
}
void go_back() {
motor_set(LOW, HIGH, HIGH, LOW);
}
void turn_left() {
motor_set(LOW, HIGH, LOW, HIGH);
}
void turn_right() {
motor_set(HIGH, LOW, HIGH, LOW);
}
void reset() {
motor_set(LOW, LOW, LOW, LOW);
digitalWrite(beep, LOW);
lcd.setCursor(11, 1);
lcd.print(" ");
}
void setup() {
Serial.begin(9600);
for (int i : en) {
pinMode(i, OUTPUT);
analogWrite(i, mspeed);
}
for (int i : in) {
pinMode(i, OUTPUT);
}
pinMode(beep, OUTPUT);
pinMode(cannon, OUTPUT);
lcd.init();
lcd.init();
lcd.backlight();
lcd.print(">> THE ARK <<");
irrecv.enableIRIn();
}
void loop() {
lcd.setCursor(0, 1);
lcd.print("SPEED: ");
lcd.setCursor(7, 1);
lcd.print(mspeed);
if (irrecv.decode(&results)) {
if (results.value != 0xFFFFFFFF) {
result = results.value;
}
switch (result) {
case 0xFF18E7:
go_forward();
break;
case 0xFF4AB5:
go_back();
break;
case 0xFF10EF:
turn_left();
break;
case 0xFF5AA5:
turn_right();
break;
case 0xFFE01F:
if (mspeed >= 125) {
mspeed -= 25;
for (int i : en) {
analogWrite(i, mspeed);
}
}
break;
case 0xFFA857:
if (mspeed <= 225) {
mspeed += 25;
for (int i : en) {
analogWrite(i, mspeed);
}
}
break;
case 0xFF906F:
digitalWrite(beep, HIGH);
lcd.setCursor(11, 1);
lcd.print("BEEP!");
break;
case 0xFF38C7:
digitalWrite(cannon, HIGH);
lcd.setCursor(11, 1);
lcd.print("FIRE!");
delay(150);
digitalWrite(cannon, LOW);
default:
break;
}
irrecv.resume();
} else {
reset();
}
delay(150);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment