Robofun 機器人論壇

標題: 求救 兩組程式碼做結合~ [打印本頁]

作者: man3838    時間: 2013-11-15 18:04
標題: 求救 兩組程式碼做結合~
本帖最後由 man3838 於 2013-11-15 18:16 編輯

兩組程式碼結合後
紅外線卻失效了 求解


紅外線感應器PIR


const int PIRSensor = 2; // 紅外線動作感測器連接的腳位


const int ledPin = 13; // LED 腳位


int sensorValue = 0; // 紅外線動作感測器訊號變數


int val = 0;


void setup() {


Serial.begin(4800); //設定終端機的Port位
pinMode(PIRSensor, INPUT);
pinMode(ledPin, OUTPUT);
}


void loop(){




sensorValue = digitalRead(PIRSensor);


if (sensorValue == HIGH) {
digitalWrite(ledPin, HIGH); // 有人,開燈
val = digitalRead(13);
Serial.println("1234");


}


else {


digitalWrite(ledPin, LOW); // 沒人,關燈


}


}
結合後


#include <WiShield.h>
#define WIRELESS_MODE_INFRA   1
#define WIRELESS_MODE_ADHOC   2
unsigned char local_ip[]       = {192,168,0,101};  
unsigned char gateway_ip[]     = {192,168,0,1};   
unsigned char subnet_mask[]    = {255,255,255,0};
char ssid[] = {"dlink"};   
unsigned char security_type    = 0;               
const prog_char security_passphrase[] PROGMEM = {"abcde"};   
prog_uchar wep_keys[] PROGMEM = {
   0x41, 0x42, 0x43, 0x44, 0x45, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
unsigned char wireless_mode = WIRELESS_MODE_INFRA;
unsigned char ssid_len;
unsigned char security_passphrase_len;
char recvChar;
char sms[8];
char message[32];
const int PIRSensor = 2;
const int ledPin = 13;


const int Motor_M1 = 7;     
int sensorValue = 0;
struct NetworkPackage
{
   int dataLength;
   char data[32];
};
NetworkPackage  mNetworkPack;  
void setup()
{


   Serial.begin(9600);




   recvChar = NULL;
   WiFi.init();


  pinMode(Motor_M1, OUTPUT);
  pinMode(PIRSensor, INPUT);
  pinMode(ledPin, OUTPUT);
}
void loop()
{
   if(NULL != recvChar) {


      if(!strcmp(mNetworkPack.data, "bulbon"))
        bulbon(0,0);
      if(!strcmp(mNetworkPack.data, "bulboff"))
        bulboff(0,0);
        if(!strcmp(mNetworkPack.data, "fanon"))
        fanon(0,0);
      if(!strcmp(mNetworkPack.data, "fanoff"))
        fanoff(0,0);


      recvChar = NULL;
   }


   WiFi.run();


   sensorValue = digitalRead(PIRSensor);


if (sensorValue == HIGH) {
   digitalWrite(ledPin, LOW);
}
else {
digitalWrite(ledPin, HIGH);
}
}
void bulbon(byte flag, byte numOfValues)
{
  Serial.println("bulbon : ");
  digitalWrite( Motor_M1, HIGH);


}
void bulboff(byte flag, byte numOfValues)
{
  Serial.println("bulboff : ");
  digitalWrite( Motor_M1, LOW);


}
void fanon(byte flag, byte numOfValues)
{
  Serial.println("fanon : ");
  digitalWrite( Motor_M1, HIGH);


}
void fanoff(byte flag, byte numOfValues)
{
  Serial.println("fanoff : ");
  digitalWrite( Motor_M1, LOW);


}
兩組程式碼結合後
紅外線卻失效了 求解



作者: man3838    時間: 2013-11-15 18:17
遙控開關
#include <WiShield.h>
#define WIRELESS_MODE_INFRA   1
#define WIRELESS_MODE_ADHOC   2
unsigned char local_ip[]       = {192,168,0,101};  
unsigned char gateway_ip[]     = {192,168,0,1};   
unsigned char subnet_mask[]    = {255,255,255,0};
char ssid[] = {"dlink"};   
unsigned char security_type    = 0;               
const prog_char security_passphrase[] PROGMEM = {"abcde"};   
prog_uchar wep_keys[] PROGMEM = {
   0x41, 0x42, 0x43, 0x44, 0x45, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
unsigned char wireless_mode = WIRELESS_MODE_INFRA;
unsigned char ssid_len;
unsigned char security_passphrase_len;
char recvChar;
char sms[8];
char message[32];
  
const int Motor_M1 = 7;     
int sensorValue = 0;
struct NetworkPackage
{
   int dataLength;
   char data[32];
};
NetworkPackage  mNetworkPack;  
void setup()
{
   
   Serial.begin(9600);
     
   recvChar = NULL;
   WiFi.init();

  pinMode(Motor_M1, OUTPUT);

}
void loop()
{
   if(NULL != recvChar) {
      
      if(!strcmp(mNetworkPack.data, "bulbon"))
        bulbon(0,0);
      if(!strcmp(mNetworkPack.data, "bulboff"))
        bulboff(0,0);
        if(!strcmp(mNetworkPack.data, "fanon"))
        fanon(0,0);
      if(!strcmp(mNetworkPack.data, "fanoff"))
        fanoff(0,0);
         
      recvChar = NULL;
   }
   
   WiFi.run();  
   
}
void bulbon(byte flag, byte numOfValues)
{
  Serial.println("bulbon : ");
  digitalWrite( Motor_M1, HIGH);
  
}
void bulboff(byte flag, byte numOfValues)
{
  Serial.println("bulboff : ");
  digitalWrite( Motor_M1, LOW);
   
}
void fanon(byte flag, byte numOfValues)
{
  Serial.println("fanon : ");
  digitalWrite( Motor_M1, HIGH);
   
}
void fanoff(byte flag, byte numOfValues)
{
  Serial.println("fanoff : ");
  digitalWrite( Motor_M1, LOW);
}
作者: vegewell    時間: 2013-11-19 05:07
可能是執行WiFi來不及執行digitalRead(PIRSensor)
你可以試
WiFi.run();
之後 加入delay(50);
若依然無效,請參考:
Arduino PIR eMail Alarm = Arduino + PIR Sensor + WiFi Shield + Google Engine App
---------
http://sinocgtchen.blogspot.tw/2 ... rt-arduino-pir.html




歡迎光臨 Robofun 機器人論壇 (https://robofun.net/forum/) Powered by Discuz! X3.2