#include <LiquidCrystal.h>
#include <IRremote.hpp>
#include <LedControl.h>
const int rs = 7, en = 8, d4 = 4, d5 = 5, d6 = 6, d7 = 3;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
#define joyX A4
#define joyY A5
#define buttonPin 2 //to switch modes
int xMap, yMap, xValue, yValue;
LedControl lc = LedControl(13, 11, 12, 1); //13=DIN, 12=CS, 11=CLK
#define IR_receiver_pin 1
bool JoystickMode = true; //indicates current mode
//int dotX = 3;
//int dotY = 4;
void setup() {
Serial.begin(115200);
IrReceiver.begin(IR_receiver_pin, DISABLE_LED_FEEDBACK); // Enable receiving IR signals
pinMode(buttonPin, INPUT_PULLUP); //button is set to input with internal pull-up resistor
lc.shutdown(0, false);
lc.setIntensity(0,0); //sets brightness to medium values
lc.clearDisplay(0);
}
void loop() {
if (digitalRead(buttonPin) == LOW ) {
JoystickMode = !JoystickMode; //goes into toggle mode when pressed
delay(200);
lcd.clear();
if (JoystickMode) {
lcd.print("Mode: JOYSTICK");
} else {
lcd.print("Mode: IR REMOTE");
}
delay(200);
}
if (!JoystickMode){
if (IrReceiver.decode()) { // Returns true if data ready.
if (IrReceiver.decodedIRData.decodedRawData == 1570963200 ) {
JoystickMode = !JoystickMode; // Toggle mode when power button is pressed on the IR remote
delay(200);
lcd.clear();
if (JoystickMode) {
lcd.print("Mode: JOYSTICK");
} else {
lcd.print("Mode: IR REMOTE");
}
delay(200);
} }
}
if (JoystickMode) { //joystick mode
xValue = analogRead(joyX);
Serial.println("xVal =");
Serial.println(xValue, DEC);
yValue = analogRead(joyY);
Serial.println("yVal =");
Serial.println(yValue, DEC);
delay(100);
xMap = map(xValue, 0, 1023, 7, 0);
yMap = map(yValue, 0, 1023, 0, 7);
lc.setLed(0, xMap, yMap, true);
lc.setLed(0, xMap-1, yMap, true);
lc.setLed(0, xMap, yMap+1, true);
lc.setLed(0, xMap-1, yMap+1, true);
delay(1000);
lc.clearDisplay(0);
}
else
{ //IR REMOTE mode
if (IrReceiver.decode()) { // Returns true if data ready.
Serial.print("Code: ");
Serial.println(IrReceiver.decodedIRData.decodedRawData);
//xMap = map(xValue, 0, 1023, 7, 0);
//yMap = map(yValue, 0, 1023, 0, 7);
switch(IrReceiver.decodedIRData.decodedRawData) {
case 4244832000: //move up
xMap = max(0, xMap-2);
break;
case 1738080000: //move down
xMap = min(8, xMap+2);
break;
case 1871773440: //move right
yMap = max(-1, yMap-2); //changed the range so that it does not fall of the screen
break;
case 534839040: //move left
yMap = min(7, yMap+2);
break;
xMap = map(xValue, 0, 1023, 7, 0);
yMap = map(yValue, 0, 1023, 0, 7);
}
lc.clearDisplay(0);
lc.setLed(0, xMap, yMap, true);
lc.setLed(0, xMap-1, yMap, true);
lc.setLed(0, xMap, yMap+1, true);
lc.setLed(0, xMap-1, yMap+1, true);
delay(1000);
//lc.clearDisplay(0);
IrReceiver.resume(); // ready to receive next value
//lc.clearDisplay(0);
}
}
}