我正在尝试调用函数 shield();在下面的草图中,但每次我这样做时,草图似乎在到达盾牌时停止了();

I'm trying to call the function shield(); in the sketch below, but very time I do so, the sketch seems to stop when reaching the shield();

#include <Password.h> 
#include <Keypad.h> 
#include "SIM900.h"
#include <SoftwareSerial.h>
#include "sms.h"
#include <LiquidCrystal.h>
#include <Servo.h>
#include <SPI.h>
#include <Ethernet.h>

Servo myservo; //declares servo

int led = 28;

int pos = 0; 
byte mac[ ] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };   //physical mac address
byte ip[ ] = { 10,0,1,16 };                      // ip in lan (that's what you need to use in your browser. ("10.0.1.2")
byte gateway[ ] = { 10,0, 1, 25 };                   // internet access via router
byte subnet[ ] = { 255, 255, 255, 0 };                  //subnet mask
EthernetServer server(80);                             //server port     
String readString;

Password password = Password( "1234" ); //password to unlock box, can be changed
SMSGSM sms;
int numdata;
boolean started=false;
char smsbuffer[160];
char n[20];
int calibrationTime = 30;   

LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

//the time when the sensor outputs a low impulse
long unsigned int lowIn;         
//the amount of milliseconds the sensor has to be low 
//before we assume all motion has stopped
long unsigned int pause = 5000;  

boolean lockLow = true;
boolean takeLowTime;  

int pirPin = 2;    //the digital pin connected to the PIR sensor's output 1 & 2
int ledPin = 4;
int pirPin1 = 5;
int ledPin1= 6;

const int switchPin3 =37;  //the digital pin connected to the magnetic contact sensor 1 & 2
const int ledPin3 = 13;

const int switchPin4 =31;
const int ledPin4 =33;

const byte ROWS = 4; // Four rows
const byte COLS = 4; // columns

// Define the Keymap
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = {44,45,47,46}; //connect to the row pinouts of the keypad
byte colPins[COLS] =  {39,41,49,53}; //connect to the column pinouts of the keypad

int x=0;


// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup()     //if i add sms(); function it workssss
{ 
   lcd.begin(16,2);

   Serial.begin(9600); //Start a Serial COM
    lcd.setCursor(0, 1);
   lcd.print("ABEL & HERWANDI");

    Serial.println(F("ABEL & HERWANDI"));
      lcd.setCursor(0, 1);
   lcd.print("SECURITY SYSTEM");
    lcd.setCursor(0, 1);
  Serial.print(F("SECURITY SYSTEM")); 
   lcd.setCursor(0, 1);
  lcd.print(F("GSM COM........."));
  Serial.print(F("GSM COM..."));


  /* if (gsm.begin(9600)) //Start the GSM COM
  {
    Serial.println(F("GOOD TO GO!!"));
     lcd.setCursor(0, 1);
    lcd.print(F("GOOD TO GO!!"));

  }
  else
  {
    lcd.print("");
    Serial.println(F("GSM NOT CONNECTED"));
     lcd.println(F("GSM NOT CONNECTED"));
  } */

  sensor1();
  sensor2();
  magnetic();
  magnetic2();


  Serial.write(0x01);
  delay(200); 
  pinMode(3, OUTPUT);  //green light for password evaluation
  pinMode(35, OUTPUT);  //red light for password evaluation
  pinMode(led, OUTPUT);
  myservo.attach(30); 
  keypad.addEventListener(keypadEvent);//add an event listener for this keypad
   // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
  Serial.println("Enter Your Code:");
  }

void loop(){

  lcd.setCursor(0, 1);
  keypad.getKey();

  magnetic1();
  magnetic3();
  sensor();
  sensor3();

  }
  //take care of some special events
  void keypadEvent(KeypadEvent eKey){
  switch (keypad.getState()){

  case PRESSED:

  lcd.print("");
  Serial.print("Enter: ");
  lcd.print("Enter:");
  lcd.print("");
  lcd.print(eKey);
  Serial.println(eKey);
  delay(10);


  switch (eKey){
    case 'A': checkPassword(); delay(1); break;
    case 'C': checkPassword(); delay(1); break;
    case 'D': checkPassword(); delay(1); break;
    case 'B': password.reset(); delay(1); break;
    case '*': checkPassword(); break;
    case '#': password.reset(); break;
     default: password.append(eKey); delay(1);
}
}
}
void checkPassword(){
if (password.evaluate()){  //if password is right open box
    Serial.println("Accepted");
    lcd.print("");
    lcd.print("Accepted");

    delay(50);
    digitalWrite(3, HIGH);//turn on
 // (sms.SendSMS("+8613668914901","Your Code was accepted! Security System is now activated!")); // send sms when password is correct
    //Add code to run if it work
    digitalWrite(3, HIGH);//turn on
    delay(5000); //wait 5 seconds
    digitalWrite(3, LOW);// turn off
    magnetic(); // calls the function and execute them
    magnetic2();
    sensor(); 
    sensor3();



}
else
{
    Serial.println("Denied"); //if passwords wrong keep box locked
    lcd.print("");
    lcd.print("Denied");
    delay(10);
    x++;

if (x==3)
    //add code to run if it did not work
   { 
     myservo.write(0);
    digitalWrite(35, HIGH); //turn on
    delay(500); //wait 5 seconds
    digitalWrite(35, LOW);//turn off  
   // (sms.SendSMS("+8613668914901","Someone is broking in, Wrong Password entered 3 times!! ")); // send the message
    Serial.println("USER WARNED");
    lcd.println("USER WARNED\n");
    digitalWrite(35, HIGH); //turn on
    delay(10000);
    digitalWrite(35,LOW);
   // while(1) { } // runs an infinite loop, the same as halting the whole systems
}
}
}
/////////////////////////////
//SETUP
void sensor1(){
  pinMode(pirPin, INPUT);
  pinMode(ledPin, OUTPUT);
  digitalWrite(pirPin, LOW);
  //give the sensor some time to calibrate
  Serial.print("sensor 1 calibration ");
  lcd.print("");
 /* lcd.print("sensor 1 calibration ");
   // for(int i = 0; i < calibrationTime; i++){
      Serial.print(".");
      lcd.print("");
      lcd.print(".");
      delay(1000);
      }
    Serial.println(" done");
    lcd.print("");
    lcd.println(" done");
    lcd.print(""); */
    lcd.println("SENSOR 1 ACTIVE");
    Serial.println("SENSOR 1 ACTIVE");
    delay(50);
  }
////////////////////////////
//LOOP
void sensor()
{
if (password.evaluate()) { // turns the sensor only when the password is correct
     if(digitalRead(pirPin) == HIGH){
       digitalWrite(ledPin, HIGH);  
       Serial.println("sensor1");
     // (sms.SendSMS("+8613668914901","Someone is broking in, Sensor 1 detected motion!!!"));
      //the led visualizes the sensors output pin state
       if(lockLow){  
         //makes sure we wait for a transition to LOW before any further output is made:
         lockLow = false;            
         delay(50);
         }   
         takeLowTime = true;
       }
     if(digitalRead(pirPin) == LOW){       
       digitalWrite(ledPin, LOW);  //the led visualizes the sensors output pin state
       if(takeLowTime){
        lowIn = millis();          //save the time of the transition from high to LOW
        takeLowTime = false;       //make sure this is only done at the start of a LOW phase
        }
       //if the sensor is low for more than the given pause, 
       //we assume that no more motion is going to happen
       if(!lockLow && millis() - lowIn > pause){  
           //makes sure this block of code is only executed again after 
           //a new motion sequence has been detected
           lockLow = true;                        
           delay(50);  
       } 
       }
       }
       }

     void sensor2() {
  pinMode(pirPin1, INPUT);
  pinMode(ledPin1, OUTPUT);
  digitalWrite(pirPin1, LOW);
  //give the sensor some time to calibrate
  Serial.print("sensor 2 calibration ");
  lcd.print("");
 /* lcd.print("sensor 2 calibration ");
   // for(int i = 0; i < calibrationTime; i++){
      Serial.print(".");
      lcd.print("");
      lcd.print(".");
      delay(1000);
      }
    Serial.println(" done");
    lcd.print("");
    lcd.println(" done");
    lcd.print("");*/
    lcd.println("SENSOR 2 ACTIVE");
    Serial.println("SENSOR 2 ACTIVE");
    delay(50);
  }

void sensor3()
{
if (password.evaluate()) { // turns the sensor only when the password is correct
     if(digitalRead(pirPin1) == HIGH){
       digitalWrite(ledPin1, HIGH);
       Serial.println("sensor2"); // to change to lcd.print 
       //(sms.SendSMS("+8613668914901","Someone is broking in, Sensor 2 detected motion!!!"));
      //the led visualizes the sensors output pin state
       if(lockLow){  
         //makes sure we wait for a transition to LOW before any further output is made:
         lockLow = false;            
         delay(50);
         }   
         takeLowTime = true;
       }

     if(digitalRead(pirPin1) == LOW){       
       digitalWrite(ledPin1, LOW);  //the led visualizes the sensors output pin state
       if(takeLowTime){
        lowIn = millis();          //save the time of the transition from high to LOW
        takeLowTime = false;       //make sure this is only done at the start of a LOW phase
        }
       //if the sensor is low for more than the given pause, 
       //we assume that no more motion is going to happen
       if(!lockLow && millis() - lowIn > pause){  
           //makes sure this block of code is only executed again after 
           //a new motion sequence has been detected
           lockLow = true;                        
           delay(50);   
       }
        }
       }
     }


void magnetic() 
{

pinMode(switchPin3, INPUT);
pinMode(ledPin3, OUTPUT);
digitalWrite(switchPin3, HIGH);
}

void magnetic1()
{
if(digitalRead(switchPin3) == LOW){
digitalWrite(ledPin3, LOW);
}
else{
  if (password.evaluate()){ // turns the sensor only when the password is correct
digitalWrite(ledPin3, HIGH);
Serial.println("Magnetic sensor1"); // to change to lcd.print
//(sms.SendSMS("+8613668914901","Someone is broking in, magnetic sensor 1 detected motion!!"));
  }
}
}


void magnetic2() 
{
pinMode(switchPin4, INPUT);
pinMode(ledPin4, OUTPUT);
digitalWrite(switchPin4, HIGH);
}

void magnetic3()
{
if(digitalRead(switchPin4) == LOW){
digitalWrite(ledPin4, LOW);
}
else{ if (password.evaluate()){ // turns the sensor only when the password is correct
digitalWrite(ledPin4, HIGH);
Serial.println("Magnetic sensor2");// to change to lcd.print
//(sms.SendSMS("+8613668914901","Someone is broking in,magnetic sensor 2 detected motion!!"));
}
}
}


void shield() {

  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {   
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {
          //store characters to string
          readString += c;
          //Serial.print(c);
         }

         //if HTTP request has ended
         if (c == '\n') {          
           Serial.println(readString); //print to serial monitor for debuging

           client.println("HTTP/1.1 200 OK"); //send new page
           client.println("Content-Type: text/html");
           client.println();     
           client.println("<HTML>");
           client.println("<HEAD>");
           client.println("<meta name='apple-mobile-web-app-capable' content='yes' />");
           client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
           client.println("<link rel='stylesheet' type='text/css' href='http://randomnerdtutorials.com/ethernetcss.css' />");
           client.println("<TITLE>Arduino Home Security System</TITLE>");
           client.println("</HEAD>");
           client.println("<BODY>");
           client.println("<H1>Abel & Herwandi</H1>");
           client.println("<hr />");
           client.println("<br />");  
           client.println("<H2>Arduino with Ethernet Shield</H2>");
           client.println("<br />");  
           client.println("<a href=\"/?button1on\"\">Turn On LED</a>");
           client.println("<a href=\"/?button1off\"\">Turn Off LED</a><br />");   
           client.println("<br />");     
           client.println("<br />"); 
           client.println("<a href=\"/?button2on\"\">Rotate Left</a>");
           client.println("<a href=\"/?button2off\"\">Rotate Right</a><br />"); 
           client.println("<p>Designed by Abel & Herwandi. SCUT Class 5</p>");  
           client.println("<br />"); 
           client.println("</BODY>");
           client.println("</HTML>");

           delay(1);
           //stopping client
           client.stop();
           //controls the Arduino if you press the buttons
           if (readString.indexOf("?button1on") >0){
               digitalWrite(led, HIGH);
           }
           if (readString.indexOf("?button1off") >0){
               digitalWrite(led, LOW);
           }
           if (readString.indexOf("?button2on") >0){
                for(pos = 0; pos < 180; pos += 3)  // goes from 0 degrees to 180 degrees 
                {                                  // in steps of 1 degree 
                  myservo.write(pos);              // tell servo to go to position in variable 'pos' 
                  delay(15);                       // waits 15ms for the servo to reach the position 
                } 
           }
           if (readString.indexOf("?button2off") >0){
                for(pos = 180; pos>=1; pos-=3)     // goes from 180 degrees to 0 degrees 
                {                                
                  myservo.write(pos);              // tell servo to go to position in variable 'pos' 
                  delay(15);                       // waits 15ms for the servo to reach the position 
                } 
           }
            //clearing string for next read
            readString="";  

         }
       }
    }
}
}

运行 在 Arduino 1.0.6 上,MAC OSX,Mega2560,使用 Arduino 以太网扩展板。

我试图在下面的草图中调用函数 shield(),但每次我这样做时,草图似乎在到达 shield() 函数时停止,我试图将在void loop,void setup,void check password中运行,但始终停止所有程序。

经过长时间的阅读,我自己发现了问题!主要问题是以太网屏蔽引脚,似乎我使用的一些键盘引脚也在以太网屏蔽中使用,引脚 51 或 53 ,所以我只需要找出以太网屏蔽引脚,问题就解决了。