// Kelsey & Raj
// SUSE 303 - Lab 3
// Monday, Feburary 26, 2023 & Monday, March 4, 2023
// Library imports:
# include <LiquidCrystal.h>
# include <LedControl.h>
# include <IRremote.h>
// Pin connections for LCD and LED Dot Matrix:
LiquidCrystal lcd(53, 51, 49, 47, 45, 43, 41);
LedControl lc(9, 10, 8, 1);
// Variable declartions:
bool JOYSTICK = true, REMOTE, TOGGLE = false; // Boolean operators for mode toggling
int X, Y = 3; // Coordinates of box position (starts at centre)
int current, previous = 0; // Required for joystick toggle check
// Remote codes [SUBJECT TO CHANGE]:
const unsigned long int UP = 4244832000; // Vol+ button
const unsigned long int DOWN = 1738080000; // Vol- button
const unsigned long int FORWARD = 1871773440; // Forward button
const unsigned long int BACKWARD = 534839040; // Reverse button
const unsigned long int POWER = 1570963200; // Power button
// Custom function to reposition box given input:
void position_box(int x, int y) {
lc.clearDisplay(0);
lc.setLed(0, x, y, true);
lc.setLed(0, x, y+1, true);
lc.setLed(0, x+1, y, true);
lc.setLed(0, x+1, y+1, true);
}
void setup() {
// put your setup code here, to run once:
// Pin connections for joystick and IR remote receiver:
pinMode(A2, INPUT); // VRx connection (horizontal)
pinMode(A1, INPUT); // VRy connection (vertical)
pinMode(52, INPUT_PULLUP); // SW connection (button)
IrReceiver.begin(12);
// LCD and serial monitor setup, splashscreen, and constant display:
Serial.begin(9600);
lcd.begin(16,2);
lcd.print("Hello World!");
delay(2000);
lcd.clear();
lcd.print("Mode: ");
// LED Dot Matrix setup and initialization:
lc.shutdown(0,false);
lc.setIntensity(0,0);
position_box(3, 3);
}
void loop() {
// put your main code here, to run repeatedly:
// Checks for input toggle from joystick button:
//current = digitalRead(52);
//if (current != previous) TOGGLE = true;
//previous = current;
//Serial.print(digitalRead(52));
//attachInterrupt(digitalPinToInterrupt(52), Serial.println("Working"), CHANGE);
//delay(1000);
// Code to toggle input sources:
if (TOGGLE == true) {
JOYSTICK = !JOYSTICK;
REMOTE = !REMOTE;
TOGGLE = false;
}
// Resets display to reprint mode as nessecary:
lcd.setCursor(6, 0);
// Joystick controls:
if (JOYSTICK == true) {
lcd.print("JOYSTICK");
Y = map(analogRead(A2), 0, 1023, -1, 7);
X = -map(analogRead(A1), 0, 1023, -7, 1);
position_box(X, Y);
} else lcd.print("REMOTE ");
// Remote controls:
if(IrReceiver.decode()) {
long int Remote_Input = IrReceiver.decodedIRData.decodedRawData;
//Serial.print("Code: ");
//Serial.println(Remote_Input);
switch (Remote_Input) {
case UP:
if (X > -1) X -= 1;
break;
case DOWN:
if (X < 7) X += 1;
break;
case FORWARD:
if (Y > -1) Y -= 1;
break;
case BACKWARD:
if (Y < 7) Y += 1;
break;
case POWER:
TOGGLE = true;
break;
}
if (REMOTE == true) position_box(X, Y);
IrReceiver.resume();
}
}