2016-01-09 19 views
1

嗨即時嘗試使用millis在我的arduino代碼重新啓動funktion,但我可以使它工作。即時嘗試在arduino工作millis但不知道如何

這就是我希望它在3秒後關閉switch3重新啓動它。

if (readString.indexOf("?b24vb2ZmL2Z1bmN0aW9u") >= 0) 
    { 
    //What it does. 
    actionTransmitter.sendSignal(1, 'C', false); 
    switchstatus3 = false; 
    delay(3000); 
    actionTransmitter.sendSignal(1, 'C', true); 
    switchstatus3 = true; 
    } 

這是如何如何做到這一點和失敗。

if (readString.indexOf("?b24vb2ZmL2Z1bmN0aW9u") >= 0) 
    { 
    //What it does. 
    actionTransmitter.sendSignal(1, 'C', false); 
    switchstatus3 = false; 
    timing = true; 
    startTime = millis(); 
    if ((timing) && millis() - startTime >= 3000) { 
     actionTransmitter.sendSignal(1, 'C', true); 
     switchstatus3 = true; 
    } 
    } 

這是所有的arduino代碼。

#include <SPI.h> 
#include <String.h> 
#include <Ethernet.h> 
#include <RemoteReceiver.h> 
#include <RemoteTransmitter.h> 

byte mac[] = { 
    0x90, 0xA2, 0xDA, 0x00, 0x9B, 0x36 
}; // MAC address 
byte ip[] = { 
    192, 168, 0, 200 
}; // IP address 
EthernetServer server(9090); // Port 

//pins 
ActionTransmitter actionTransmitter(9); 
#define RELAY1 5 
#define RELAY2 6 
#define RELAY3 7 
#define RELAY4 8 

// Boolean funtion for state 
String readString = String(30); 
boolean switchstatus1 = false; 
boolean switchstatus2 = false; 
boolean switchstatus3 = false; 
boolean Relaystatus1 = false; 
boolean Relaystatus2 = false; 
boolean Relaystatus3 = false; 
boolean Relaystatus4 = false; 

//timmer 
boolean timing = false; 
unsigned long startTime; 

void setup() { 


    //Relay default off 
    digitalWrite(5, HIGH); 
    digitalWrite(6, HIGH); 
    digitalWrite(7, HIGH); 
    digitalWrite(8, HIGH); 

    // Inicia o Ethernet 
    Ethernet.begin(mac, ip); 


    //enable serial data print 
    Serial.begin(115200); 
    // RF reciver and transmitter pins 
    RemoteReceiver::init(0, 3, showCode); 
    // Relay board pins 
    pinMode(RELAY1, OUTPUT); 
    pinMode(RELAY2, OUTPUT); 
    pinMode(RELAY3, OUTPUT); 
    pinMode(RELAY4, OUTPUT); 


} 

void loop() { 
    // listen for incoming clients 
    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() < 30) 
     { 
      //store characters to string 
      readString += (c); 
     } 

     //if HTTP request has ended 
     if (c == '\n') 
     { 
      // Switch triggers by web 

      // Remote switch control by web 
      //switch 1 on 
      if (readString.indexOf("?dW5pdDFPTg==") >= 0) 
      { 
      // What it does. 
      actionTransmitter.sendSignal(1, 'A', true); 
      switchstatus1 = true; 
      } 
      //switch 1 off 
      if (readString.indexOf("?dW5pdDFPRkY=") >= 0) 
      { 
      //What it does. 
      actionTransmitter.sendSignal(1, 'A', false); 
      switchstatus1 = false; 
      } 
      //switch 2 on 
      if (readString.indexOf("?dW5pdDJPTg==") >= 0) 
      { 
      //What it does. 
      actionTransmitter.sendSignal(1, 'B', true); 
      switchstatus2 = true; 
      } 
      //switch 2 off 
      if (readString.indexOf("?dW5pdDJPRkY=") >= 0) 
      { 
      //What it does. 
      actionTransmitter.sendSignal(1, 'B', false); 
      switchstatus2 = false; 
      } 
      if (readString.indexOf("?dW5pdDNPTg==") >= 0) 
      { 
      //switch 3 on 
      actionTransmitter.sendSignal(1, 'C', true); 
      switchstatus3 = true; 
      } 
      //switch 3 off 
      if (readString.indexOf("?dW5pdDNPRkY=") >= 0) 
      { 
      //What it does. 
      actionTransmitter.sendSignal(1, 'C', false); 
      switchstatus3 = false; 
      } 

      //switch 3 off deley on function. 
      if (readString.indexOf("?b24vb2ZmL2Z1bmN0aW9u") >= 0) 
      { 
      //What it does. 
      actionTransmitter.sendSignal(1, 'C', false); 
      switchstatus3 = false; 
      timing = true; 
      startTime = millis(); 
      if ((timing) && millis() - startTime >= 3000) { 
       actionTransmitter.sendSignal(1, 'C', true); 
       switchstatus3 = true; 
      } 
      } 

      // Relay board control by web. 
      // Relay 1 on 
      if (readString.indexOf("cmVsYXkxb24=") >= 0) 
      { 
      //What it does. 
      Relaystatus1 = true; 
      digitalWrite(RELAY1, LOW); 

      } 
      // Relay 1 off 
      if (readString.indexOf("cmVsYXkxb2Zm") >= 0) 
      { 
      //What it does. 
      Relaystatus1 = false; 
      digitalWrite(RELAY1, HIGH); 
      } 
      // Relay 2 on 
      if (readString.indexOf("cmVsYXkyb24=") >= 0) 
      { 
      //What it does. 
      Relaystatus2 = true; 
      digitalWrite(RELAY2, LOW); 

      } 
      // Relay 2 off 
      if (readString.indexOf("cmVsYXkyb2Zm") >= 0) 
      { 
      //What it does. 
      Relaystatus2 = false; 
      digitalWrite(RELAY2, HIGH); 
      } 
      // Relay 3 on 
      if (readString.indexOf("cmVsYXkzb24=") >= 0) 
      { 
      //What it does. 
      Relaystatus3 = true; 
      digitalWrite(RELAY3, LOW); 
      } 
      // Relay 3 off 
      if (readString.indexOf("cmVsYXkzb2Zm") >= 0) 
      { 
      //What it does. 
      Relaystatus3 = false; 
      digitalWrite(RELAY3, HIGH); 
      } 
      // Relay 4 on 
      if (readString.indexOf("cmVsYXk0b24=") >= 0) 
      { 
      //What it does. 
      Relaystatus4 = true; 
      digitalWrite(RELAY4, LOW); 
      } 
      // Relay 4 off 
      if (readString.indexOf("cmVsYXk0b2Zm") >= 0) 
      { 
      //What it does. 
      Relaystatus4 = false; 
      digitalWrite(RELAY4, HIGH); 


      } 
      // Needed to Display Site: 
      client.println("HTTP/1.1 200 OK"); 
      client.println("Content-Type: text/html"); 
      client.println(); 
      client.print("<meta http-equiv='refresh'content='5'>"); 
      client.print("<font size='20'>"); 

      // Enabling command line 
      client.print("ArduinoOn"); 
      // Remote switch state indication. 
      //Switch state 1 
      if (switchstatus1 == true) { 
      client.print("Switch1On"); 
      } 
      if (switchstatus1 == false) { 
      client.print("Switch1Off"); 
      } 
      //Switch state 2 
      if (switchstatus2 == true) { 
      client.print("Switch2On"); 
      } 
      if (switchstatus2 == false) { 
      client.print("Switch2Off"); 
      } 
      //Switch state 3 
      if (switchstatus3 == true) { 
      client.print("Switch3On"); 
      } 
      if (switchstatus3 == false) { 
      client.print("Switch3Off"); 
      } 

      // Relay state indication. 
      // Relay 1 
      if (Relaystatus1 == true) { 
      client.print("Relay1On"); 
      } 
      if (Relaystatus1 == false) { 
      client.print("Relay1Off"); 
      } 
      // Relay 2 
      if (Relaystatus2 == true) { 
      client.print("Relay2On"); 
      } 
      if (Relaystatus2 == false) { 
      client.print("Relay2Off"); 
      } 
      // Relay 3 
      if (Relaystatus3 == true) { 
      client.print("Relay3On"); 
      } 
      if (Relaystatus3 == false) { 
      client.print("Relay3Off"); 
      } 
      // Relay 4 
      if (Relaystatus4 == true) { 
      client.print("Relay4On"); 
      } 
      if (Relaystatus4 == false) { 
      client.print("Relay4Off"); 
      } 
      //clearing string for next read. 
      readString = ""; 

      // close the connection: 
      client.stop(); 
     } 
     } 
    } 
    } 
} 

//RC Remote control 

void showCode(unsigned long receivedCode, unsigned int period) { 
    // Note: interrupts are disabled. You can re-enable them if needed. 

    // Print the received code. 
    Serial.print("Code: "); 
    Serial.print(receivedCode); 
    Serial.print(", period duration: "); 
    Serial.print(period); 
    Serial.println("us."); 

    // Switch 1 off 
    if (receivedCode == 352833) 
    { 
    switchstatus1 = true; 

    } 
    // Switch 1 Off 
    if (receivedCode == 352829) 

    { 
    switchstatus1 = false; 

    } 
    // Switch 2 on 
    if (receivedCode == 353805) 
    { 
    switchstatus2 = true; 

    } 
    // Switch 2 off 
    if (receivedCode == 353801) 

    { 
    switchstatus2 = false; 

    } 
    // Switch 3 on 
    if (receivedCode == 354129) 
    { 
    switchstatus3 = true; 

    } 
    // Switch 3 off 
    if (receivedCode == 354125) 

    { 
    switchstatus3 = false; 

    } 
} 

回答

0

我不明白什麼是使用第一種方法的問題。如果你想要3s,那麼只需延遲3000和voilá。但既然你想要第二種形式,你的代碼應該看起來像這樣:

if (readString.indexOf("?b24vb2ZmL2Z1bmN0aW9u") >= 0) 
    { 
    //What it does. 
    actionTransmitter.sendSignal(1, 'C', false); 
    switchstatus3 = false; 
    timing = true; 
    startTime = millis(); 
    if(timing){ 

     while(millis() - startTime < 3000); // This will wait 3s 

     actionTransmitter.sendSignal(1, 'C', true); 
     switchstatus3 = true; 
    } 
    } 
相關問題