2016-03-31 5 views
2

ちょっと私は現在プロジェクトを立ち上げようとしています。私はArduinoボードのピン2にフロートレベルスイッチを接続していて、もう片方はarduinoの5vに接続しています。Arduinoはフロートレベルのスイッチ信号を読み取っていません

スイッチがハイになったときにソフトウェアにメッセージを表示させたいのですが、メッセージがすぐに表示され、スイッチが高く設定されていないことがわかりました。

将来、フロートレベルスイッチを使用して洪水監視に使用される信号が高くなるとテキストメッセージを送信します。

#include "SIM900.h" 
#include <SoftwareSerial.h> 
#include "sms.h" 

SMSGSM sms; 

//To change pins for Software Serial, use the two lines in GSM.cpp. 

int numdata;  
boolean started=false; 
char smsbuffer[160];  
char n[20];  
int closed=0;//Sets initial signal to 0  
const int switchPin = 2;  
int switchState = 0;   // current state of the button  
int lastswitchState = 0; // previous state of the button 

void setup()  
{ 
    //Serial connection.  
    Serial.begin(9600);  
    Serial.println("GSM Shield for Flood Early Warning System \n");  
    pinMode(switchPin, INPUT);  

    //Start configuration of shield with baudrate.  
    //For http uses is recommended to use 4800 or slower.  
} 

void loop() {  
    closed=digitalRead(switchPin);  
    // compare the buttonState to its previous state  
    if (switchState != lastswitchState) {  
    // if the state has changed, increment the counter  
    if (switchState == HIGH) {  
     // if the current state is HIGH then the button  
     // wend from off to on: 

     Serial.println("on");  
    } else {  
     // if the current state is LOW then the button  
     // wend from on to off:  
     Serial.println("off");  
    } 

    // Delay a little bit to avoid bouncing  
    delay(50);  
    } 

    // save the current state as the last state,  
    //for next time through the loop  
    lastswitchState = switchState; 


if (closed == HIGH) {  
    Serial.println ("Switch signal received"); 

    if (gsm.begin(2400)){  
    Serial.println("\n status=READY");  
    started=true;  
    }  
    else Serial.println("\n status=IDLE"); 



    if(started){ 
    //Enable this two lines if you want to send an SMS. 
    if (sms.SendSMS("0871234567", "Arduino SMS"))//Number you wish to text and the message to be sent 

     Serial.println("\nSMS sent OK");//Alert for Serial monitor once sms sent 

    } 

} 

} 
+0

編集ありがとうございました:) – Garysully1986

答えて

1

入力ピンは10KΩの抵抗を介してGNDに接続してください。 https://www.arduino.cc/en/Tutorial/DigitalPinsを参照してください:

「これはまた、それらに接続されて何もpinMode(ピン、INPUT)として構成されたピンは、...環境からの電気的ノイズを拾って、ピンの状態で一見ランダムな変更を報告することを、しかし意味、または近くのピンの状態を容量結合することができます。 "

そしてhttps://www.arduino.cc/en/Reference/DigitalRead

ピンが何に接続されていない場合、digitalRead()HIGHまたはLOWのどちらかを返すことができます(これはランダムに変更することができます)。

つまり、スイッチが開いているとき、入力ピンは定義済みの状態(オンまたはオフ)ではありません。プルダウン抵抗(入力ピンとGNDの間に10KΩの抵抗を追加すると、スイッチが閉じていない限り、抵抗はピンを "プルダウン"(=グランドに向かって)します。その場合、+ 5Vへの接続が優先されます

+0

ええ、私はそれを100KΩの抵抗に接続していましたが、それは読んでいませんでした。完全な5Vの電圧が得られていない可能性があります。スイッチの入力と出力をテストすることになります。 – Garysully1986

+0

はい、スイッチのオン/オフ状態でピンの電圧を測定し、何が起きているのかを確認し、値をArduino(またはATMega328)の値と比較して、 –

関連する問題