// include the library code:
#include <LiquidCrystal.h>
#include <IRremote.h>
#include <LedControl.h>
float JoyStickX = 0;
float JoyStickY = 0;
float JoyStickXold = 0;
float JoyStickYold = 0;
const int JOYSTICK_X_PIN = A0;
const int JOYSTICK_Y_PIN = A1;
const int JOYSTICK_BUTTON_PIN = 34;
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
// signal pin connected to pin 13
const int RECV_PIN = 31;
// Dot matrix pins
const int dOut = 22;
const int CLK = 24;
const int CS = 25;
const int dIn = 23;
// dot matrix control class
LedControl lc = LedControl(23, 24, 25, 1);
// mode of movement (true is joystick)
bool mode = true;
// postion array {row, column}
int pos[]= {3, 4};
// Remote Button array {button value, direction}
unsigned long button[][2] = {{0, 0}, {0, 1}, {0, 2}, {0, 3}, {0, 4}};
unsigned long currentBut = 0;
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
pinMode(JOYSTICK_BUTTON_PIN, INPUT);
digitalWrite(JOYSTICK_BUTTON_PIN, HIGH);
IrReceiver.begin(RECV_PIN); // setup and assign receive pin
// Startup Sequence
lcd.println("Starting ");
for(int i = 0; i < 5; i++){
lcd.setCursor(0, 1);
switch (i){
case 0:
lcd.println("Power Button");
break;
case 1:
lcd.println("Up ");
break;
case 2:
lcd.println("Down ");
break;
case 3:
lcd.println("Right ");
break;
case 4:
lcd.println("Left ");
break;
}
while(IrReceiver.decode() != 1){
lcd.setCursor(0,0);
lcd.println("Starting ");
delay(50);
lcd.setCursor(0,0);
lcd.println("Starting. ");
delay(50);
lcd.setCursor(0,0);
lcd.println("Starting.. ");
delay(50);
lcd.setCursor(0,0);
lcd.println("Starting...");
delay(50);
}
button[i][0] = IrReceiver.decodedIRData.decodedRawData; // IrReceiver.decodedIRData.decodedRawData returns the raw decoded value, 32 bits
Serial.println(IrReceiver.decodedIRData.decodedRawData);
IrReceiver.resume();
}
lcd.clear();
lcd.setCursor(0,0);
lcd.println("Joystick ");
// Dot Matrix startup
lc.shutdown(0,false);
lc.setIntensity(0,0);
lc.clearDisplay(0);
}
void loop() {
// powerbutton mode switch
if (IrReceiver.decode()){
delay(50);
currentBut = IrReceiver.decodedIRData.decodedRawData;
Serial.print("Received IR code: ");
Serial.println(currentBut);
if(currentBut == button[0][0]){
mode = !mode;
// reset square pos
pos[0] = 3;
pos[1] = 4;
lc.clearDisplay(0);
lc.setLed(0, pos[0], pos[1], true); lc.setLed(0, pos[0], pos[1] - 1, true);
lc.setLed(0, pos[0] + 1, pos[1], true); lc.setLed(0, pos[0] + 1, pos[1] - 1, true);
lcd.setCursor(0,0);
switch(mode){
case true:
//lcd.print("Starting ");
lcd.println("Joystick ");
break;
case false:
//lcd.print("Starting ");
lcd.println("Remote ");
break;
}
}
IrReceiver.resume();
}
// joystick click mode switch
if (digitalRead(JOYSTICK_BUTTON_PIN) == 0){
delay(50);
mode = !mode;
// reset square pos
pos[0] = 3;
pos[1] = 4;
lc.clearDisplay(0);
lc.setLed(0, pos[0], pos[1], true); lc.setLed(0, pos[0], pos[1] - 1, true);
lc.setLed(0, pos[0] + 1, pos[1], true); lc.setLed(0, pos[0] + 1, pos[1] - 1, true);
lcd.setCursor(0,0);
switch(mode){
case true:
//lcd.print("Starting ");
lcd.println("Joystick ");
break;
case false:
//lcd.print("Starting ");
lcd.println("Remote ");
break;
}
}
// joystick movement
if(mode == true){
// joystick movement
JoyStickX = map(analogRead(JOYSTICK_X_PIN), 0, 1023, 0,8);
JoyStickY = map(analogRead(JOYSTICK_Y_PIN), 0, 1023, 8,0); // 8 to 0 to fix inverted y (since top of screen is 0 y)
// Check for move
if(JoyStickX != JoyStickXold || JoyStickY != JoyStickYold){
drawJoy(JoyStickX, JoyStickY, pos);
JoyStickXold = JoyStickX;
JoyStickYold = JoyStickY;
}
}
// remote movement
if (mode == false){
if (IrReceiver.decode()){ // Returns 0 if no data ready, 1 if data ready.
currentBut = IrReceiver.decodedIRData.decodedRawData;
for(int i = 1; i < 5; i++){
if(currentBut == button[i][0]){
drawRemote(button[i][1], pos);
}
}
IrReceiver.resume(); // ready to receive next value
}
}
}
void drawJoy(int joyX, int joyY, int (&pos)[2]){
lc.clearDisplay(0);
pos[1] = joyX;
pos[0] = joyY - 1; // minus one cause top row of screen is 0
// constrain
pos[0] = constrain(pos[0], -1, 7); // y (row)
pos[1] = constrain(pos[1], 0, 8); // x (col)
//* Testing for position
//Serial.print(pos[1]);
//Serial.print(" , ");
//Serial.println(pos[0]);
lc.setLed(0, pos[0], pos[1], true); lc.setLed(0, pos[0], pos[1] - 1, true);
lc.setLed(0, pos[0] + 1, pos[1], true); lc.setLed(0, pos[0] + 1, pos[1] - 1, true);
}
void drawRemote(int direction, int (&pos)[2]){
lc.clearDisplay(0);
// increment main point
switch(direction){ // 1 = up, 2 = down, 3 = right, 4 = left
case 1:
pos[0] -= 1; // Modified to decrement the value
break;
case 2:
pos[0] += 1; // Modified to increment the value
break;
case 3:
pos[1] -= 1; // Modified to decrement the value
break;
case 4:
pos[1] += 1; // Modified to increment the value
break;
}
// constrain
pos[0] = constrain(pos[0], -1, 7); // y (row)
pos[1] = constrain(pos[1], 0, 8); // x (col)
lc.setLed(0, pos[0], pos[1], true); lc.setLed(0, pos[0], pos[1] - 1, true);
lc.setLed(0, pos[0] + 1, pos[1], true); lc.setLed(0, pos[0] + 1, pos[1] - 1, true);
}