2

Hey I am trying to set up a project at the moment, I have a float level switch connected to pin 2 of my Arduino board, the other end is connected to the 5v in on the arduino.

I want to have the software display a message when the switch goes high but at the moment it goes straight to the message and I know that the switch is not set high as I have it in my hand.

In future it will send a text message when the signal goes high, being used for flood monitoring using a float level switch.

#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

  }

 }

}
Ôrel
  • 7,044
  • 3
  • 27
  • 46

1 Answers1

1

You should connect your input pin to GND via a 10KOhm resistor. See https://www.arduino.cc/en/Tutorial/DigitalPins :

"This also means however, that pins configured as pinMode(pin, INPUT) with nothing connected to them, ... will report seemingly random changes in pin state, picking up electrical noise from the environment, or capacitively coupling the state of a nearby pin. ".

And https://www.arduino.cc/en/Reference/DigitalRead :

If the pin isn't connected to anything, digitalRead() can return either HIGH or LOW (and this can change randomly).

In other words, when your switch is open, the input pin is not in a defined state (either on or off). By adding a pulldown resistor (a 10 KOhm resistor between your input pin and GND, the resistor "pulls down" (=toward the ground) your pin, unless the switch is closed, in which case the connection to +5V prevails (because it is a direct connection, without a resistor).

Alphonsos_Pangas
  • 514
  • 1
  • 3
  • 13
  • Yeah, I had it connected to a 100K Ohm resistor and it was not reading, I suspect that it may not be getting the full 5volts or a variation of that so going to test the input and output of the switch. – Garysully1986 Apr 04 '16 at 11:09
  • Yes, you could measure the voltage at the pin with the switch in the on and off state to see what is happening, and compare the values with the required values for the Arduino (or the ATMega328) – Alphonsos_Pangas Apr 04 '16 at 12:35