我是Arduino编程的初学者。下面脚本的目的是按下按钮开启按钮(d == 0)等待8秒钟关闭泵并发送短信警报。释放按钮后,泵保持关闭状态(紧急关闭)。
这里我设法在按下按钮时打开泵并发送消息。释放按钮后,泵成功关闭。然而,8秒后关闭泵的延迟并没有真正起作用,这让我感到困惑。相反,发生的是在8秒延迟之后泵立即猛拉(切换)然后继续保持开启并发送消息。我很感激任何见解。
// Include the GSM library
#include <GSM.h>
#define PINNUMBER ""
// initialize the library instance
GSM gsmAccess;
GSM_SMS sms;
int pinout = 3;
int ledout = 13;
int d=1;
int a = 1;
void setup() {
pinMode(4,INPUT); // Input Power
pinMode(pinout , OUTPUT); // Motor Output
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("SMS Messages Sender");
// connection state
boolean notConnected = true;
// Start GSM shield
// If your SIM has PIN, pass it as a parameter of begin() in quotes
while (notConnected) {
if (gsmAccess.begin(PINNUMBER) == GSM_READY) {
notConnected = false;
} else {
Serial.println("Not connected");
delay(1000);
}
}
Serial.println("GSM initialized");
}
void loop() {
d=digitalRead(4);
// If digital state of pin 4 is zero start motor and power ON switch LED
if(d==0){
digitalWrite(pinout ,HIGH);
digitalWrite(ledout ,HIGH);
delay(8000); // This is the line that doesn't seem to work!
digitalWrite(pinout ,LOW); // This is the line that doesn't seem to work!
//for( a = 1; a < 2; a = a + 1 ){
if (a<2){
Serial.print("Enter a mobile number: ");
char remoteNum[20] = "4xxxxxxx22";
// sms text
Serial.print("Now, enter SMS content: ");
char txtMsg[200] = "Cow #57 SCC Abnormality Detected";
//readSerial(txtMsg);
Serial.println("SENDING");
Serial.println();
Serial.println("Message:");
Serial.println(txtMsg);
// send the message
sms.beginSMS(remoteNum);
sms.print(txtMsg);
sms.endSMS();
Serial.println("\nCOMPLETE!\n");
}
a = a+1;
Serial.println(a);
}else{
digitalWrite(pinout ,LOW);
digitalWrite(ledout ,LOW);
a=1;
}
//Serial.print(d); // Monitor state of Pin 4
}