#include <IRremote.h>
#include <LedControl.h>
#include <LiquidCrystal.h>
//Val = map(analog_val, 0, 1023, -10, 10);
int joy_x;
int x = 4; //assigned to 3 to set up square
int y = 4;
int joy_y;
int DIN = 8;
int CS = 9;
int CLK = 10;
int SEL = 2; //button of joystick
//IR receiver
const int RECV_PIN=17; //signal pin on pin 17
unsigned long up = 4244832000;
unsigned long down = 1738080000;
unsigned long left = 534839040;
unsigned long right = 1871773440;
unsigned long power = 1570963200;
LedControl lc = LedControl(DIN, CLK, CS, 0);
//LCD setup -- pins
const int rs=12, en=11, d4=5, d5=4, d6=6, d7=7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
//mode (joystick or RS)
int mode=0;
//mode 1 = joystick
//mode 2 = IR remote
void setup() {
lc.shutdown(0,false);
lc.setIntensity(0,0);
lc.clearDisplay(0);
Serial.begin(9600);
pinMode(SEL, INPUT);
IrReceiver.begin(RECV_PIN);
lcd.begin(16,2);
}
void loop() {
lcd.clear();
lcd.setCursor(0,0);
Serial.println(mode);
lcd.print("Mode: ");
if (mode==1){
lcd.print("JOYSTICK");
}
else if (mode==2){
lcd.print("REMOTE");
}
else{
lcd.print(" ");
}
//check mode
if (digitalRead(2)==HIGH){
mode = 1; //joystick
}
else if (IrReceiver.decodedIRData.decodedRawData == power){
mode = 2; //IR remote
}
lc.setLed(0,y,x,true);
lc.setLed(0, y-1, x-1, true);
lc.setLed(0, y, x-1, true);
lc.setLed(0, y-1, x, true);
if (mode==1){
joy_x = analogRead(A0);
joy_y = analogRead(A1);
x = map(joy_x, 0, 1023, 0, 8);
y = map(joy_y, 0, 1023, 8, 0);
lc.clearDisplay(0);
disp_dots;
}
//IR reciever -- reading inputs (Original code)
if (IrReceiver.decode()){ //0 if no data ready, 1 if data ready
//Serial.print("Code: ");
//Serial.println(IrReceiver.decodedIRData.decodedRawData);
//returns raw decoded value, 32 bits
IrReceiver.resume(); //ready to recieve next value
}
if (mode==2){
//remote
if(IrReceiver.decodedIRData.decodedRawData == up){
//clear the original square then decrement the y
//and regenerate square (similar for other directions)
lc.clearDisplay(0);
//bounding square from the top
if(y>0){
y--;
disp_dots(x,y);
}
}
if(IrReceiver.decodedIRData.decodedRawData == down){
lc.clearDisplay(0);
//bounding square from bottom
if(y<8){
y++;
disp_dots(x,y);
}
}
if(IrReceiver.decodedIRData.decodedRawData == left){
lc.clearDisplay(0);
//bounding square from the left
if(x<8){
x++;
//disp_dots(x,y);
}
}
if(IrReceiver.decodedIRData.decodedRawData == right){
lc.clearDisplay(0);
//bounding square from the right
if(x>0){
x--;
//disp_dots(x,y);
}
}
}
//default original square
disp_dots(x,y);
delay(200);
}
//Define a function to display the dots on the screen
void disp_dots(int x, int y){
lc.setLed(0,y,x,true);
lc.setLed(0, y-1, x-1, true);
lc.setLed(0, y, x-1, true);
lc.setLed(0, y-1, x, true);
}