#include <LedControl.h>
#include <LiquidCrystal.h>
#include <IRremote.hpp>
int squareX = 3;
int squareY = 3;
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
const int irReceiverPin = 7;
int DIN = 9;
int CS = 10;
int CLK = 8;
LedControl lc = LedControl(DIN, CLK, CS, 1);
const int joyX = A1;
const int joyY = A2;
const int joyButton = 13;
bool irpower = false;
int mode=0;
int jb=0;
void setup() {
lcd.begin(16, 2);
IrReceiver.begin(irReceiverPin, ENABLE_LED_FEEDBACK);
pinMode(joyButton, INPUT_PULLUP);
lc.shutdown(0,false);
lc.setIntensity(0,8);
lc.clearDisplay(0);
updateLCDMode();
Serial.begin(9600);
}
void loop() {
// Check for IR commands first
if (IrReceiver.decode()) {
if (IrReceiver.decodedIRData.command == 162) {
mode = (mode + 1) % 2; // Toggle between 0 (Joystick mode) and 1 (Remote mode)
irpower = true; // Update 'irpower' based on the mode
updateLCDMode(); // Update LCD to show the current mode
}
else if (irpower) {
// If in Remote mode, process the IR command to move the square
moveSquareWithIR(IrReceiver.decodedIRData.command);
}
IrReceiver.resume(); // Prepare for the next IR command
}
// Process joystick input if in Joystick mode and the joystick button is not pressed
if (digitalRead(joyButton) == 0)
{
mode = 0;
int xValue = analogRead(joyX);
int yValue = analogRead(joyY);
const int threshold = 100; // Joystick movement threshold
// Adjust the square position based on the joystick input
if (xValue < (512 - threshold)) {
if (squareX > 0) squareX--;
}
else if (xValue > (512 + threshold)) {
if (squareX < 5) squareX++;
}
if (yValue < (512 - threshold)) {
if (squareY > 0) squareY--;
} else if (yValue > (512 + threshold)) {
if (squareY < 5) squareY++;
}
updateDotMatrix(); // Update the Dot Matrix display with the new position
}
delay(100); // Short delay to debounce and throttle the loop execution
}
void updateLCDMode() {
lcd.clear();
if (mode==0) {
lcd.print("Mode: JOYSTICK");
}
else if (mode==1)
{
lcd.print("Mode: REMOTE");
}
}
void moveSquareWithIR(unsigned long but) {
switch (but) {
case 144:
if (squareY > 0) squareY--;
break;
case 224:
if (squareY < 5) squareY++;
break;
case 2:
if (squareX > 0) squareX--;
break;
case 152:
if (squareX < 5) squareX++;
break;
default:
break;
}
// Update the Dot Matrix to reflect the new position
updateDotMatrix();
}
void updateDotMatrix() {
lc.clearDisplay(0);
for (int x = 0; x < 2; x++) {
for (int y = 0; y < 2; y++) {
lc.setLed(0, squareY + y, squareX + x, true);
}
}
}