// Group 12
//1.Sam Moses [email protected] 30128306
//2.Marc Gauvin [email protected] 30132685
//3.Xavier Champagne [email protected] 30113963
//4.Yahye Bashey [email protected] 30108680
//5.Michael Pullishy [email protected]
#include <IRremote.h> //including infrared remote header file
#include <LiquidCrystal.h>
#include <LedControl.h>
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2; //defining LCD pins
int RECV_PIN = 7; // the pin where you connect the output pin of IR sensor
IRrecv receiver(RECV_PIN );
//decode_results results;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int DIN = 33; //defining dot matrix pins
int CS = 31;
int CLK = 29;
int x = 3; //initial coords of one corner of the rectangle
int y = 3;
int xPosition = 0;
int yPosition = 0;
int SW_state = 0;
int mapX = 0;
int mapY = 0;
int VRx = A15; //Defining Joystick pins
int VRy = A13;
int SW = 22;
LedControl lc=LedControl(DIN, CLK, CS,0); //Starting the dot matrix
boolean remoteActive = false; //Setting use of the joystick as default
unsigned long key_value = 0;
void setup(){
Serial.begin(9600);
lcd.begin(16, 2);
receiver.enableIRIn(); // Start the IR receiver
//lcd.print("Ready");
lcd.setCursor(0, 1);
pinMode(VRx, INPUT); //Making sure that the pins connected to the joystick are considered as input pins by the microprocessor
pinMode(VRy, INPUT);
pinMode(SW, INPUT_PULLUP); //Making pin for joystick pushdown and input pin, and setting initial signal to HIGH (joystick is on)
lc.shutdown(0,false);
lc.setIntensity(0,0);
lc.clearDisplay(0);
lc.setLed(0,x,y,true); //Forming the initial rectangle
delay(100); //juicy startup animation
lc.setLed(0,x+1,y,true);
delay(100);
lc.setLed(0,x+1,y+1,true);
delay(100);
lc.setLed(0,x,y+1,true);
delay(150);
lc.clearDisplay(0);
delay(250);
printBox();
}
void loop(){
if(remoteActive){ //this will happen after one iteration of joystick, if the user presses center button
remote();
}
if(!remoteActive){ //This is called initially, because joystick is set to default
joystick();
}
}
void printBox(){ //This keeps the rectangle within the dot-matrix display
if(x < 0){x = -1;}
if(x > 7){x = 7;}
if(y < 0){y = -1;}
if(y > 7){y = 7;}
lc.clearDisplay(0);
lc.setLed(0,x,y,true);
lc.setLed(0,x+1,y,true);
lc.setLed(0,x+1,y+1,true);
lc.setLed(0,x,y+1,true);
}
void remote(){
lcd.print("Mode: Remote");
if (receiver.decode()) { //check if IR has been decoded properly
//Cases correspond to pulse codes from Volume and forward/back buttons
switch (receiver.decodedIRData.command){
case 162:
remoteActive = false;
lcd.print("Power");
break;
case 224:
y = y + 1; // move the box upword
printBox();
delay(100);
//lcd.print("Volume +");
break;
case 2:
x = x - 1; // move the box to left
printBox();
delay(100);
//lcd.print("|<<");
break;
case 152:
x = x + 1; // move the box to the right
printBox();
delay(100);
//lcd.print(">>|");
break ;
case 144:
y = y - 1; // move the box downword
//lcd.print("Volume -");
printBox();
delay(100);
break ;
}
receiver.resume();
}
delay(300);
lcd.clear();
}
void joystick(){
lcd.print("Mode: Joystick");
xPosition = analogRead(VRx); //Get Left or Right from joystick
yPosition = analogRead(VRy); //Get Up or Down from joystick
SW_state = digitalRead(SW);
mapX = map(xPosition, 0, 1023, -512, 512); //map 0 or 1 from the joystick to -512 or 512, respectively
mapY = map(yPosition, 0, 1023, -512, 512);
Serial.print("X: ");
Serial.print(mapX);
Serial.print(" | Y: ");
Serial.print(mapY);
Serial.print(" | Button: ");
Serial.println(SW_state);
//move according to joystick
if(mapX > 500){
x = x - 1; // move the box to left
printBox();
}
if(mapX < -500){
x = x + 1; // move the box to the righ
printBox();
}
if(mapY > 500){
y = y +1; // move the box up
printBox();
}
if(mapY < -500){
y = y - 1; // move the box down
printBox();
}
if(SW_state == 0){
remoteActive = true; //if middle button is pressed, activate remote
}
delay(100);
lcd.clear();
}