Go Down

Topic: Ps2 KeyBoard  TV Remote (Read 1 time) previous topic - next topic

sodanum

Apr 17, 2010, 12:25 pm Last Edit: Apr 17, 2010, 12:44 pm by sodanum Reason: 1
[ch3657][ch3657]Hello  ;D

" Sa Wat Dee "

This is my project

Arduino Ps2 KeyBoard  TV Remote
[media]http://www.youtube.com/watch?v=HWORWJsqAjE&feature=player_embedded[/media]










Code: [Select]
#include <IRremote.h>
#include <ps2.h>
unsigned char code;

int RECV_PIN = 7;  // [ch3586][ch3634] data [ch3586][ch3629][ch3591][ch3605][ch3633][ch3623][ch3619][ch3633][ch3610][ch3619][ch3637][ch3650][ch3617][ch3607] [ch3605][ch3656][ch3629][ch3585][ch3633][ch3610] digitital pin 7
int RECV_POWER = 2; // [ch3586][ch3634][ch3652][ch3615][ch3610][ch3623][ch3585] [ch3586][ch3629][ch3591][ch3605][ch3633][ch3623][ch3619][ch3633][ch3610][ch3619][ch3637][ch3650][ch3617][ch3607] [ch3605][ch3656][ch3629][ch3585][ch3633][ch3610] digital pin 2

int IR_GND = 1; // [ch3586][ch3634][ch3652][ch3615][ch3621][ch3610][ch3586][ch3629][ch3591][ch3627][ch3621][ch3629][ch3604] ir digital pin 1

int LED_1 = 11;
int LED_2 = 12;
int LED_3 = 13;

IRrecv irrecv(RECV_PIN);
IRsend irsend;

decode_results results;

PS2 kbd(5, 4); // Ps2 keyborad  data [ch3605][ch3656][ch3629][ch3585][ch3633][ch3610] digital pin 4
                       // clock [ch3605][ch3656][ch3629][ch3585][ch3633][ch3610] digital pin 5

void kbd_init()
{
  char ack;

  kbd.write(0xff);  // send reset code
  ack = kbd.read();  // byte, kbd does self test
  ack = kbd.read();  // another ack when self test is done
}

void setup()
{
  
   kbd_init();

  irrecv.enableIRIn(); // Start the receiver

  pinMode(RECV_POWER,OUTPUT);
  digitalWrite(RECV_POWER,LOW);
  
  pinMode(LED_1, OUTPUT);
  digitalWrite(LED_1,HIGH);
  
  pinMode(LED_2,OUTPUT);
  digitalWrite(LED_2,HIGH);
  
  pinMode(LED_3,OUTPUT);
  digitalWrite(LED_3,HIGH);
  
  pinMode(IR_GND,OUTPUT);
  digitalWrite(IR_GND,LOW);
  
  keyBoardCode();
  
}

// Storage for the recorded code
int codeType = -1; // The type of code
unsigned long codeValue; // The code value if not raw
unsigned int rawCodes[RAWBUF]; // The durations if raw
int codeLen; // The length of the code
int toggle = 0; // The RC5/6 toggle state

//[ch3619][ch3633][ch3610]
unsigned long myValue[80];
int myLen[80];

//[ch3626][ch3656][ch3591]
unsigned long sendIrValue;
int sendIrLen;

unsigned char keyboard[80];

// Stores the code for later playback
// Most of this code is just logging

void storeCode(decode_results *results) {
  codeType = results->decode_type;
  int count = results->rawlen;

  if (codeType == UNKNOWN)
  {
    codeLen = results->rawlen - 1;
   
     for (int i = 1; i <= codeLen; i++) {
      if (i % 2) {
        rawCodes[i - 1] = results->rawbuf[i]*USECPERTICK - MARK_EXCESS;
        delay(1);}
        else {
        rawCodes[i - 1] = results->rawbuf[i]*USECPERTICK + MARK_EXCESS;
        delay(1);}}
   }else {
    if (codeType == NEC) {
    if (results->value == REPEAT) {
        delay(1);
        return;}}  
    else if (codeType == SONY){delay(1);}
    else if (codeType == RC5) {delay(1);}
    else if (codeType == RC6) {delay(1);}
    else {delay(1);}
   
    codeValue = results->value;
    codeLen = results->bits;
  }
}

void sendCode(int repeat) {
  
   // code [ch3627][ch3621][ch3633][ch3585][ch3616][ch3634][ch3588][ch3626][ch3656][ch3591]
  if (codeType == NEC)
  {
    if (repeat) {irsend.sendNEC(REPEAT, sendIrLen);}
    else { irsend.sendNEC(sendIrValue, sendIrLen);}
  }

  else if (codeType == SONY){irsend.sendSony(sendIrValue, sendIrLen);}
  
  else if (codeType == RC5 || codeType == RC6)
  {
       if (!repeat) { toggle = 1 - toggle;}
       sendIrValue = sendIrValue & ~(1 << (sendIrLen - 1));
       sendIrValue = sendIrValue | (toggle << (sendIrLen - 1));
       if (codeType == RC5) { irsend.sendRC5(sendIrValue, sendIrLen);}
       else { irsend.sendRC6(sendIrValue, sendIrLen);}
  }
  
  else if (codeType == UNKNOWN ) {irsend.sendRaw(rawCodes, sendIrLen, 38);}
  

}

int lastButtonState;
int buttonState = HIGH;
int recvButton = 1;
int recvState;

int irButton ;
int irState  ;

int test;

void loop() {
  
  
  code = kbd.read();
  
//keyBoard F1 setup IR
  if(code == 0x05){recvButton = 1-recvButton;
     irrecv.enableIRIn(); // Re-enable receiver  
     if(recvButton==0)recvState = 1-recvState;}

///////// [ch3648][ch3611][ch3636][ch3604][ch3611][ch3636][ch3604][ch3585][ch3634][ch3619][ch3607][ch3635][ch3591][ch3634][ch3609] ir recv
  if(recvState == 1){
    digitalWrite(RECV_POWER,HIGH) ;
    digitalWrite(LED_1,LOW);
    digitalWrite(IR_GND,HIGH);
    GotoSetKeyBoard();}//[ch3605][ch3633][ch3657][ch3591][ch3588][ch3656][ch3634][ch3611][ch3640][ch3656][ch3617]
   
  if(recvState == 0){
         digitalWrite(RECV_POWER,LOW) ;
         digitalWrite(LED_1,HIGH);
         digitalWrite(IR_GND,LOW);
         GotoSendIr();} // [ch3626][ch3656][ch3591] code remote
         
         
}



//////////////[ch3648][ch3585][ch3655][ch3610] Code Remote [ch3605][ch3634][ch3617][ch3611][ch3640][ch3656][ch3617]////////////////////
void GotoSetKeyBoard()
{

for(int i = 0; i < 80;i++){
if(code == keyboard[i]   ){  
       myValue[i] = codeValue;
       myLen[i] = codeLen;  
      
  }}     
  // [ch3629][ch3656][ch3634][ch3609][ch3588][ch3656][ch3634] Remote
  if (irrecv.decode(&results)) {
    digitalWrite(LED_1, LOW);
    storeCode(&results);
    delay(1);
    irrecv.resume(); // resume receiver
    digitalWrite(LED_1, HIGH);}   
  
}




////////////////////// [ch3626][ch3656][ch3591] code remote ////////////////////////////
void GotoSendIr()
{
    /// [ch3585][ch3604][ch3611][ch3640][ch3656][ch3617][ch3626][ch3656][ch3591] Remote
    for(int i = 0; i < 80;i++){
    if(code == keyboard[i]  ){
    irButton = 1-irButton;         
    if(irButton == 0)irState = 1;
   
    sendIrValue = myValue[i];
    sendIrLen   = myLen[i];

   }}
   
   /// [ch3626][ch3656][ch3591][ch3588][ch3656][ch3634] Remote
   if (irState == 1 ) {
    digitalWrite(LED_1, LOW);
    sendCode(LOW);
    digitalWrite(LED_1, HIGH);
    delay(100); // Wait a bit between retransmissions
    irState = 0;}
}

////////////////// keyBoardCode//////////////////
void keyBoardCode()
{
  keyboard[0] = 0x075; // up
  keyboard[1] = 0x072; // down
  keyboard[2] = 0x06B; // left
  keyboard[3] = 0x074; // right
  
  keyboard[4] = 0x070; // lns
  keyboard[5] = 0x071; // Del
  keyboard[6] = 0x06C; // HOME
  keyboard[7] = 0x069; // End
  keyboard[8]  = 0x07D; // P up
  keyboard[9]  = 0x07A; // P down
  
  
  keyboard[10] = 0x16; // 1
  keyboard[11] = 0x1E; // 2
  keyboard[12] = 0x26; // 3
  keyboard[13] = 0x25; // 4
  keyboard[14] = 0x2E; // 5
  keyboard[15] = 0x36; // 6
  keyboard[16] = 0x3D; // 7
  keyboard[17] = 0x3E; // 8
  keyboard[18] = 0x46; // 9
  keyboard[19] = 0x45; // 0
  keyboard[20] = 0x4E; // -
  keyboard[21] = 0x55; // +
  keyboard[22] = 0x66; // back
  keyboard[23] = 0x0E; // ~
  
}




Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy