วันศุกร์ที่ 19 ธันวาคม พ.ศ. 2557

ทำรีโมทใข้เอง

โปรเจ็คนี้ผมยังไม่ทำนะครับถ้าทำเสร็จแล้วจะเอาคริปมาให้ดูอีก













 unsigned long pulse_start,pulse_high,pulse_low;
int x=0,i,chk,flip=0,freq=12;
unsigned int  code_on[60][6]={0};
unsigned int  code_off[60][6]={0};

void setup() {
  pinMode(2,INPUT);   //  IR sensor-TSOP
  pinMode(12,OUTPUT); // IR LED driver pin
  pinMode(13,OUTPUT); // Indicator LED
  pinMode(4,INPUT);   // Vol+
  pinMode(5,INPUT);   // Vol-
  pinMode(6,INPUT);   // Prog+
  pinMode(7,INPUT);   // prog-
  pinMode(3,INPUT);   // Power
  pinMode(8,INPUT);   // Jammer
  pinMode(9,INPUT);   //programming cancel button
  digitalWrite(12,LOW);
  //delay(1000);
//Total buttons= 7,POWER,VOLUME+-,Channel+-,Jammer,programming cancel...
 
// Serial.begin(9600);

while(1){                            //freq callibration
  PORTB |=_BV(PORTB4);
  delayMicroseconds(freq);
  PORTB &= ~_BV(PORTB4);
  delayMicroseconds(freq);
 

  if( PIND & _BV(PORTD4)) {
   freq=freq+1;
   delay(500);
  }
 
  PORTB |=_BV(PORTB4);
  delayMicroseconds(freq);
  PORTB &= ~_BV(PORTB4);
  delayMicroseconds(freq);
 
  if(PIND & _BV(PORTD5)) {
  freq=freq-1;
  delay(500);
  }
 
  PORTB |=_BV(PORTB4);
  delayMicroseconds(freq);
  PORTB &= ~_BV(PORTB4);
  delayMicroseconds(freq);
 
  if(PIND & _BV(PORTD6))
  break;
 
  PORTB |=_BV(PORTB4);
  delayMicroseconds(freq);
  PORTB &= ~_BV(PORTB4);
  delayMicroseconds(freq);

}

}

void loop() {
 
  for(int count=0 ;count<=10 ;count++) {
  digitalWrite(13,HIGH);
  delay(50);
  digitalWrite(13,LOW);
  delay(50);
  }
  delay(1000);
  //code_on[60][6]={0};
  //code_off[60][6]={0};
  //Serial.print("Ahoy,I'm ready !");
 
  for(x=0 ; x<=5 ; x=x+1) {
  
  
  i=0,chk=0;
 
 
 // float freq=0;   frequency can't be measured caused i'm using an IR sensor that "filters out" the 38kHz signal....basile has provided a link to use an IR photocell....then the freq part might work..
 
 
  while(1) {
  if(!(PIND & _BV(PORTD2))) {                  //First Low signal
  pulse_start=micros();
  pulse_low=pulse_start;
  break;
  }
  if(PINB & _BV(PORTB1)) {                     // programming cancelled
  goto loop_out;
  }
 
  }

  pulse:
 
  while(1) {
  if( (PIND & _BV(PORTD2)) ) {                //High signal
  chk=1;
  pulse_high=micros();
 
  if( (pulse_high-pulse_low) > 20  && (pulse_high-pulse_low) < 65000 ) {
  code_on[i][x] =(pulse_high-pulse_start);
  i++;
  if(i==59)
  goto pulse_end;
  pulse_start=pulse_high;
  break;
  }

 
  //else if( fabs(pulse_high-pulse_low) < 20) {   //can't measure freq right now,as TSOP filters the 38Khz signals
  //if(f==0) {
  //int time=fabs(pulse_high-pulse_low);
  //freq=(1000/(2*time));
 //}
  //}
 
  else if(micros()-pulse_low > 65000)            //65 Chosen as variable to store time is unsigned int
  goto pulse_end;                                //High for too long time,so taken as pulse end
  }
 
  else if ( (!(PIND & _BV(PORTD2))) && chk==1) {              //low signal
  chk=0;
  pulse_low=micros();
  /*if(f==0) {
  unsigned int time=(pulse_low-pulse_high);
  Serial.print(time);
  freq=(1000/(2*time));
  f=f+1;
  }*/
 
  }
 
 
  else if( (!(PIND & _BV(PORTD2))) && (micros()-pulse_low) > 100000)    //low for too long,aborted
  return;
  }
 
 
  while(1){
  if((!(PIND & _BV(PORTD2))) && (micros()-pulse_start >36 )) {   //check for Low signal
  pulse_low=micros();
  code_off[i-1][x]= pulse_low-pulse_start;
  pulse_start=pulse_low;
  goto pulse;
  }
  else if (micros()-pulse_start>100000)   //assumed to be end of pulse
  goto pulse_end;
 
 
  }
  pulse_end:
 
  for(int count=0 ;count<=4 ;count++) {
  digitalWrite(13,HIGH);
  delay(150);
  digitalWrite(13,LOW);
  delay(150);
  }
 

  }
 //**************************************************************************************//
 //*****************************Data input complete**************************************//
 //**************************************************************************************//
 //**************************************************************************************//

  loop_out:
  
  digitalWrite(13,HIGH);    //Indicating data input complete
 
 
  while(1) {
  
  if(digitalRead(3)==1) {    //power
  flip=!flip;
  digitalWrite(13,LOW);
 // Serial.print("POWER");
  IR_Signal(flip);
  }
 
  else if(digitalRead(4)==1) {  //Vol+
  digitalWrite(13,LOW);
  //Serial.print("Vol+");
  IR_Signal(2);
 
 
  }
 
  else if(digitalRead(5)==1) {   //Vol-
  digitalWrite(13,LOW);
  //Serial.print("Vol-");
  IR_Signal(3);
 
  }
 
  else if(digitalRead(6)==1) {   //prog+
  digitalWrite(13,LOW);
  //Serial.print("Prog+");
  IR_Signal(4);
  }

  else if(digitalRead(7)==1) {   //Prog-
  digitalWrite(13,LOW);
  //Serial.print("Prog-");
  IR_Signal(5);
  }
 
  else if(digitalRead(8)==1) {
    digitalWrite(13,LOW);
  while(digitalRead(8)==1) {     //jammer
  PORTB |=_BV(PORTB4);
  delayMicroseconds(freq);
  PORTB &= ~_BV(PORTB4);
  delayMicroseconds(freq);
  }
  digitalWrite(13,HIGH);
  }
 
  else
  digitalWrite(13,HIGH);
  }
 
 
 

  /*Serial.print("\nOn times\n");
  for(int j=0; j<=i; j++){
  Serial.println(code_on[j]);
  //Serial.print("\n");
  }
  Serial.print("\nOff times\n");
  for(int j=0; j<=i; j++){
  Serial.println(code_off[j]);
  //Serial.print("\n");
  }
  Serial.print("Frequency = ");
  Serial.println(freq);
  delay(500);*/
}

void IR_Signal(int a) {
  int col=a;
  for(int k=0; k<=60 ;k++){
  unsigned long time=micros();
  while(micros()-time<=code_on[k][col]) {
  PORTB |=_BV(PORTB4);
  delayMicroseconds(freq);
  PORTB &= ~_BV(PORTB4);
  delayMicroseconds(freq);
  }
  time=micros();
  while(micros()-time<=code_off[k][col]) {
  //PORTB &= ~_BV(PORTB4);
  //delayMicroseconds(freq);
  }
  }
 
}


http://www.instructables.com/id/Clone-1/step6/Youre-done/


ไม่มีความคิดเห็น:

แสดงความคิดเห็น