Sending SMS automatically after hitting certain value

Hi guys! I'm using Arduino UNO and TinySine GSM SIM5320E model 3G/GPRS/GSM Shield for Arduino with GPS - European version SIM5320E. I powered the GSM with 9 V 2 A adaptor and also using USB.

I have a code that works absolutely fine for sending SMS after using command 's' in the serial monitor.

But can you please help in modifying the code, so that the GSM module can automatically send SMS to the mobile after the sensor (connected to A0 pin) hitting certain value (say 650 above)?

FONAtest.ino (24.6 KB)

Hi

If you don't need (your?) switch-case procedure anymore, something like this might work.

void loop() 
{
	//This is for enabling sms sending procedure
	boolean smsEnabled = false;	

	//This is for storing measured sensor value
	int analogValue0 = analogRead(A0);	

	//These two variables are for setting the limits for sensor value.
	//When upper limit has been reached, the sms will be sent and to avoid sending 1000 messages while sensor value is over upperValue,
	//therefore there is a lowerValue which has to be reached to activate upperValue limit again. This 650-300 is called hysteresis. Value of the lowerValue depends how low you'd like go go until sms sending procedure will activated again.

	int upperLimit = 650, lowerLimit = 300;	

	if(analogValue > upperLimit && smsEnabled == true)
	{	
		smsEnabled = false;
		
		// send an SMS!
		// Here you'll send an sms.
		...
	}

	if(analogValue < lowerLimit && smsEnabled == false)
	{
		smsEnabled = true;
	}
}

Edit: What I'm not sure about, is it too fast to read analogRead so often. It has to be tested.

-Tommi

Well I can modify the code saying: take the reading for every 5 minutes or so and then respond with SMS. But what I can understand is.. how can I use sendto variable to set a default number and message variable to send specific text/s?

Also with this part of my code:

if (!fona.sendSMS(sendto, message)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("Sent!"));
}

if fona.sendSMS is responsible for sending text message, how can I use this function in your version of code?
thnk u..

Rishi_me:
Well I can modify the code saying: take the reading for every 5 minutes..

Then you could use millis() -function to measure time between measurements

.

Rishi_me:
..how can I use sendto variable to set a default number and message variable to send specific text/s?

How you now define where it'd send the sms and what's in it ?

.

Rishi_me:
Also with this part of my code:

if (!fona.sendSMS(sendto, message))
{
Serial.println(F("Failed"));
}
else
{
Serial.println(F("Sent!"));
}

if fona.sendSMS is responsible for sending text message, how can I use this function in your version of code?
thnk u..

Just implement that code for sending sms inside of the if()-statement. If if-statement's conditions has been met, then it will send an sms.

-Tommi

The millis() you suggested me is exactly what I need for my project and the its all good now. Thank you so much for helping me out on this.