Robofun 機器人論壇

 找回密碼
 申請會員
搜索
熱搜: 活動 交友 discuz
查看: 8994|回復: 11
打印 上一主題 下一主題

arduino接紅外線接收器

[複製鏈接]
跳轉到指定樓層
1#
發表於 2011-8-17 18:16:23 | 顯示全部樓層 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式
各位大大,我想請教一下,因為我現在想要用網路去控制紅外線接收器,想請問,我如果照我下面code的想法做的話,因為會來不及接收,而跳出去,想請問,各位大大,有什麼比較好的想法?



#include <IRremote.h>                    // 引用 IRRemote 函式庫
#include <SPI.h>
#include <Client.h>
#include <Ethernet.h>
#include <Server.h>
#include <Udp.h>

byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x3A, 0xB3 }; //physical mac address
byte ip[] = { 192, 168, 111, 50 };            // ip in lan
byte gateway[] = { 192, 168, 111, 254 };            // internet access via router
byte subnet[] = { 255, 255, 255, 0 };                   //subnet mask
Server server(80);                                      //server port
byte sampledata=50;            //some sample data - outputs 2 (ascii = 50 DEC)
String readString = String(30); //string for fetching data from address

IRsend irsend;                           // 定義 IRsend 物件來發射紅外線訊號

const int irReceiverPin = 2;             // 紅外線接收器 OUTPUT 訊號接在 pin 2
IRrecv irrecv(irReceiverPin);            // 定義 IRrecv 物件來接收紅外線訊號
decode_results results;                  // 解碼結果將放在 decode_results 結構的 result 變數裏

int input_nu;
String string_temp;
char to_int_temp[1];

// 顯示紅外線協定種類
void showIRProtocol(decode_results *results)
{
  Serial.print("Protocol: ");
  
  // 判斷紅外線協定種類
  switch(results->decode_type) {
   case NEC:
     Serial.print("NEC");
     break;
   case SONY:
     Serial.print("SONY");
     break;
   case RC5:
     Serial.print("RC5");
     break;
   case RC6:
     Serial.print("RC6");
     break;
   case DISH:
     Serial.print("DISH");
     break;
   case SHARP:
     Serial.print("SHARP");
     break;
   default:
     Serial.print("Unknown encoding");  
  }  

  // 把紅外線編碼印到 Serial port
  Serial.print(", irCode: ");            
  Serial.print(results->value, HEX);    // 紅外線編碼
  Serial.print(",  bits: ");           
  Serial.println(results->bits);        // 紅外線編碼位元數   
}

void setup()
{
  Ethernet.begin( mac, ip, gateway, subnet );            //start Ethernet
  Serial.begin(9600);
  irrecv.enableIRIn();                                   // 啟動紅外線解碼
}

void loop()
{

// Create a client connection
Client client = server.available();
  if (client) {
    while (client.connected()) {
   if (client.available()) {
    char c = client.read();
    //read char by char HTTP request
    if (readString.length() < 30)
      {
        //store characters to string
        readString += c;
      }  
        //output chars to serial port
        //Serial.print(c);
        //if HTTP request has ended
        if (c == '\n') {
               if(readString.substring( 6, 7 ) == 'L')
               {
                 string_temp = readString.substring( 8, 9 );
                 to_int_temp[0] = string_temp[0];
                 input_nu = atoi(to_int_temp);
                 switch(input_nu)
                 {
                   case 1:           
                               //想在這裡做紅外線的接收
                              if (irrecv.decode(&results))                // 解碼成功,收到一組紅外線訊號
                              {         
                                             showIRProtocol(&results);            // 顯示紅外線協定種類
                                             irrecv.resume();                     // 繼續收下一組紅外線訊號        
                               }
                     break;
                   default:
                     break;                  
                 }
               }
          input_nu = 0;     
          // now output HTML data starting with standart header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          //set background to yellow
          client.print("<body style=background-color:yellow>");
          //send first heading
          client.println("<font color='red'><h1>HTTP test routines</font></h1>");

          client.println("</body></html>");
          //clearing string for next read
          readString="";
          //stopping client
          client.stop();
            }
          }
        }
      }
      
}
2#
 樓主| 發表於 2011-8-18 10:21:04 | 顯示全部樓層
sinocgt大大,我現在是想要做一個紅外線接收器,
例如:我從網路下192.168.111.50/?L=x(x為變數)如果變數為0的話,就會去做下面這個函數
// 顯示紅外線協定種類
void showIRProtocol(decode_results *results)
但如果我下了0這個變數時,沒有很快的去按搖控器的話,就會跑出  case 1:  這個區塊,所以想問有什麼比較好的方法?
3#
 樓主| 發表於 2011-9-2 10:08:12 | 顯示全部樓層
各位大大,想請問一下,我現在想要做個網路型的紅外線解碼器,想請問一下,我要怎麼把解到的碼送到網頁上,因為我在loop裡,有去看user是否有送參數進來,如果是我要的參數,它就會開會去收外紅線,只是收到了以後,我要怎麼讓它一直丟上網頁?



byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x3A, 0xB3 }; //physical mac address
byte ip[] = { 192, 168, 111, 50 };            // ip in lan
byte gateway[] = { 192, 168, 111, 254 };            // internet access via router
byte subnet[] = { 255, 255, 255, 0 };                   //subnet mask
Server server(80);                                      //server port
byte sampledata=50;            //some sample data - outputs 2 (ascii = 50 DEC)
String readString = String(30); //string for fetching data from address

IRsend irsend;                           // 定義 IRsend 物件來發射紅外線訊號

const int irReceiverPin = 2;             // 紅外線接收器 OUTPUT 訊號接在 pin 2
IRrecv irrecv(irReceiverPin);            // 定義 IRrecv 物件來接收紅外線訊號
decode_results results;                  // 解碼結果將放在 decode_results 結構的 result 變數裏

int input_nu;
String string_temp;
char to_int_temp[1];

// 顯示紅外線協定種類
void showIRProtocol(decode_results *results)
{
  Serial.print("Protocol: ");
  
  // 判斷紅外線協定種類
  switch(results->decode_type) {
   case NEC:
     Serial.print("NEC");
     break;
   case SONY:
     Serial.print("SONY");
     break;
   case RC5:
     Serial.print("RC5");
     break;
   case RC6:
     Serial.print("RC6");
     break;
   case DISH:
     Serial.print("DISH");
     break;
   case SHARP:
     Serial.print("SHARP");
     break;
   default:
     Serial.print("Unknown encoding");  
  }  

  // 把紅外線編碼印到 Serial port
  Serial.print(", irCode: ");            
  Serial.print(results->value, HEX);    // 紅外線編碼
  Serial.print(",  bits: ");           
  Serial.println(results->bits);        // 紅外線編碼位元數   
}


void setup()
{
  Ethernet.begin( mac, ip, gateway, subnet );            //start Ethernet
  Serial.begin(9600);
  irrecv.enableIRIn();                                   // 啟動紅外線解碼
}

void loop()
{
  
// Create a client connection
Client client = server.available();
  if (client) {
    while (client.connected()) {
   if (client.available()) {
    char c = client.read();
    //read char by char HTTP request
    if (readString.length() < 30)
      {
        //store characters to string
        readString += c;
      }  
        //output chars to serial port
        //Serial.print(c);
        //if HTTP request has ended
        if (c == '\n') {
               if(readString.substring( 6, 7 ) == 'L')
               {
                 string_temp = readString.substring( 8, 9 );
                 to_int_temp[0] = string_temp[0];
                 input_nu = atoi(to_int_temp);
                 switch(input_nu)
                 {
                   case 1:
                   for(int i = 0; i < 5000; i ++)
                   {
                        if(irrecv.decode(&results))
                        {                 
                          showIRProtocol(&results);                        
                          irrecv.resume();
                        }
                        delay(1);
                   }  
                     break;
                   default:
                     break;                  
                 }
               }
          input_nu = 0;  


          // now output HTML data starting with standart header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          //set background to yellow
          client.print("<body style=background-color:yellow>");
          //send first heading
          client.println("<font color='red'><h1>Netown test IR control html</font></h1>");
        
          client.println("</body></html>");
          //clearing string for next read
          readString="";
          //stopping client
          client.stop();
         
            }
          }
        }
      }
      
}
您需要登錄後才可以回帖 登錄 | 申請會員

本版積分規則

小黑屋|手機版|Archiver|機器人論壇 from 2005.07

GMT+8, 2024-5-17 01:46 , Processed in 0.102986 second(s), 7 queries , Apc On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表