按住按钮超过5秒,做一些事情。 Arduino的

时间:2016-05-04 16:35:40

标签: button arduino

我有一个爱好项目,发送一个字符串" 1"或字符串" 0"到我的网络服务器。我的arduino作为客户端工作,它工作,但现在我想添加另一个声明。 如果按住按钮5秒或更长时间,请发送字符串" 5"到我的网络服务器。我不知道如何让按钮计算我按住按钮的秒数。你能帮忙吗?

以下是代码:

#include <SPI.h>
#include <WiFi.h>


byte mac[] = { 0xDE, 0xFD, 0xBE, 0xEF, 0xFE, 0xED };      // MAC adresse 
char ssid[] = "test";                                     
char pass[] = "123456789";                                

IPAddress ip(192, 168, 0, 143);                           // Klient IP
IPAddress server(192,168,0,100);                          // Server IP
int port = 2056;                                          
boolean btnpressed = false;
int status = WL_IDLE_STATUS;


WiFiClient client;

unsigned long lastConnectionTime = 0;        
boolean lastConnected = false;               
const unsigned long postingInterval = 1000;  




void setup() {

  attachInterrupt(0, AlarmPressed, RISING);

  // Opne serial port
  Serial.begin(9600);
  while (!Serial) {
    ; 
                  }



  while ( status != WL_CONNECTED) { 
    Serial.print(" SSID: ");
    Serial.println(ssid);
    status = WiFi.begin(ssid, pass);
  delay(1000); // 
  printWifiStatus();
  Serial.println("Connecting to server");
        }
        }


void loop() {   



 if (client.available()) {
   char c = client.read();
   Serial.print(c);
   client.stop();

                         }



 if (!client.connected() && lastConnected) {

   Serial.println("Disconnecting.");     
   Serial.println();                        

   client.stop();                           
                                           }



 if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {



    SendData();



                                                                               }


 lastConnected = client.connected();
            } // Slutten paa loop





void AlarmPressed() {
  btnpressed = true;
                    }


void SendData() {

 // if there's a successful connection:
 if (client.connect(server, port)) {
   Serial.println("Connecting...");
   // Send data til server:
     if (btnpressed == false){
              client.write("0");

              Serial.print("No Alarm");
              Serial.println();
              btnpressed = false;

                             } 
            else {
               client.write("1");

               Serial.print("Alarm !");
               Serial.println();

               }              






   // note the time that the connection was made:
   lastConnectionTime = millis();
                                    }                        
 else {
   // if you couldn't make a connection:
   Serial.println("Connection failed");
   Serial.println("Disconnecting.");
   Serial.println();
   client.stop();
       }                                                     

                 }                                            


void printWifiStatus() {

  Serial.print(" SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:

  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("Signalstyrke til forbindelsen (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

0 个答案:

没有答案