Robofun 機器人論壇

 找回密碼
 申請會員
搜索
熱搜: 活動 交友 discuz
查看: 8971|回復: 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-17 21:19:18 | 只看該作者
不太懂你要做什麼,可以再說清楚一點嗎?
3#
發表於 2011-8-17 21:20:50 | 只看該作者
不太懂你要做什麼,可以再說清楚一點嗎?
4#
 樓主| 發表於 2011-8-18 10:21:04 | 只看該作者
sinocgt大大,我現在是想要做一個紅外線接收器,
例如:我從網路下192.168.111.50/?L=x(x為變數)如果變數為0的話,就會去做下面這個函數
// 顯示紅外線協定種類
void showIRProtocol(decode_results *results)
但如果我下了0這個變數時,沒有很快的去按搖控器的話,就會跑出  case 1:  這個區塊,所以想問有什麼比較好的方法?
5#
發表於 2011-8-18 18:03:02 | 只看該作者
把程式改成這樣試試看!

void loop()
{

                               //想在這裡做紅外線的接收
                              if (irrecv.decode(&results))                // 解碼成功,收到一組紅外線訊號
                              {         
                                             irrecv.resume();                     // 繼續收下一組紅外線訊號        
                               }
// 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:           
                            showIRProtocol(&results);
                     break;
                   default:
                     break;                  
                 }
               }
          input_nu = 0;     
          // now output HTML data starting with standart header
          client.println("HTTP/1.1 200 OK");
   ............................
6#
 樓主| 發表於 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();
         
            }
          }
        }
      }
      
}
7#
發表於 2011-9-19 11:10:45 | 只看該作者
你可能需要在HTML語法中埋入timer,固定一段時間就去sync server!
8#
發表於 2011-10-8 22:53:43 | 只看該作者
請問各位前輩, 我依照coopermaa大在 http://coopermaa2nd.blogspot.com/2011/03/11.html
的做法上傳程式後出現
IRsend::sendSharp(long unsigned int, int)':
D:\PizG\Arduino\arduino-1.0-rc1\libraries\IRremote\IRremote.cpp:689: error: 'delay' was not declared in this scope
請問要如何解決?
9#
發表於 2011-10-14 18:50:14 | 只看該作者
你上傳的是我 http://coopermaa2nd.blogspot.com/2011/03/11.html 這篇
裏面的範例程式就出現這個問題嗎?還是你上傳的是你自己寫的程式?

我發現你用的是 arduino-1.0-rc1,而我之前是用 arduino-0022
不曉得跟 arduino ide 版本有沒有關,待會我試試看
10#
發表於 2011-10-14 18:57:29 | 只看該作者
還真的是 Arduino IDE 版本的問題耶!用 Arduino-1.0rc 版本編譯會失敗:




http://code.google.com/p/arduino/wiki/Arduino1 這邊查
發現原來 arduino ide 1.0rc 內部做了一些調整
它把 Arduino core 的 WProgram.h 改名為 Arduino.h 了
所以,你可以去改一下 IRRemoteInt.h L17 這行,把:

#include <WProgram.h>

改成:

#include <Arduino.h>

這樣就可以編譯過關了。(只不過要注意,這時用舊版 Arduino IDE 編譯的話,反而會過不了關喔!)
11#
發表於 2011-10-16 19:57:30 | 只看該作者
本帖最後由 pizg 於 2011-10-16 20:16 編輯

非常感謝coopermaa 大大, 您真是熱心.
我的確是使用 arduino ide 1.0rc 版,
依照您的說明改成Arduino.h後, 又出現新的問題,
如下所示, 請問該如何解決?

'uint8_t' does not name a type
struct irparams_t' has no member named 'rawlen'
D:\PizG\Arduino\arduino-1.0-rc1\libraries\IRremote\IRremote.cpp:529: error: 'volatile struct irparams_t' has no member named 'rawlen'
12#
發表於 2011-10-18 20:03:15 | 只看該作者
我發現 IRRemote Library 有兩個版本

一個版本的錯誤訊息是前面那個
另一個較舊版的 IRRemote 遇到的錯誤訊息是這個:



不過,不管是哪個版本,在使用 arduino IDE rc1.0 的時候
都是改 IRremoteInt.h 把 #include <WProgram.h> 換成 #include <Arduino.h> 就可以了啊
一個是在 L17,一個是在 L15
您需要登錄後才可以回帖 登錄 | 申請會員

本版積分規則

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

GMT+8, 2024-4-30 00:37 , Processed in 0.242714 second(s), 9 queries , Apc On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

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