I have a program where specific SMS sent to a GSM module turns one LED on. Then i have another condition if that LED is on and if ultrasonic sensor detects object closer than 20cm to it, it should call specific phone number. It all works properly until i take of GPRS GSM shield off the supply and plug it back on, then it calls me randomly when i send message and won't turn led off. Is it possible that my GSM shield is broken or will backup battery solve the problem?
I just noticed that at the moment i send SMS to GSM module, ultrasonic sensor automatically detects distance at 5cm or less even tho nothing came close to sensor, is it also because i'm missing backup battery?
Could you post your code?
Sometimes it works perfectly and sometimes it doesn't. When i use button instead of ultrasonic sensor to trigger phone call, it always works...
heres full code:
#include <SoftwareSerial.h>
#define trigPin 2
#define echoPin 3
// Configure software serial port
SoftwareSerial SIM900(7, 8);
//Variable to save incoming SMS characters
char incoming_char=0;
String message;
int led = 9;
int sirena = 5;
long duration;
int distance;
void setup() {
SIM900.begin(19200);
Serial.begin(19200);
// Give time to your GSM shield log on to network
//delay(20000);
// AT command to set SIM900 to SMS mode
SIM900.print("AT+CMGF=1\r");
// Set module to send SMS data to serial out upon receipt
SIM900.print("AT+CNMI=2,2,0,0,0\r");
pinMode(led, OUTPUT);
pinMode(sirena, OUTPUT);
//digitalWrite(led,LOW);
pinMode(led2, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
// Calculate the distance:
distance = duration * 0.034 / 2;
Serial.print("Distance = ");
Serial.print(distance);
Serial.println(" cm");
if(SIM900.available() >0) {
message = SIM900.readString();
//Get the character from the cellular serial port
}else
{
message = "";
}
if(message.indexOf("+381601416081")> -1){
if(message.indexOf("ON") > -1){
Serial.println("LED ON");
digitalWrite(led, HIGH);
}
else if(message.indexOf("OFF") > -1){
Serial.println("LED OFF");
digitalWrite(led,LOW);
}
}
if( distance < 20 && digitalRead(led) == HIGH){
SIM900.println("ATD + +381601416081;");
SIM900.println();
digitalWrite(sirena, HIGH);
delay(300);
digitalWrite(sirena, LOW);
}
}
You probably should remove your phone number when posting code. I am checking through it right now.
This is what i get when i send SMS to module, it randomly drops down to 0 cm while closest object to ultrasonic sensor is 137cm away (ceiling).
Does that only happen when you send a SMS to it? It seems as if those are false readings that are not initiated by sending a SMS to it.
Yes it only happens if i send SMS to it. Sometimes it would work fine, but if I take GSM module off supply and plug it back on it would drop to 0cm when i send SMS after that. So my guess is that GSM module somehow interrupts the work of ultrasonic sensor
I'm not sure what's causing the problem, but if it is only when you send a SMS, you could implement some code that would skip over the ultrasonic sensor for however many times after you send a SMS.
After I send SMS to it, then about 20 seconds ultrasonic sensor calculates false distance, then it gets back to calculating true distance... Should i skip this period of 20 seconds with a delay?
This loops over the ultrasonic sensor 20 times. Try adding something like this:
#include <SoftwareSerial.h>
#define trigPin 2
#define echoPin 3
// Configure software serial port
SoftwareSerial SIM900(7, 8);
//Variable to save incoming SMS characters
char incoming_char=0;
String message;
int led = 9;
int sirena = 5;
int timer = 0;
int timer2 = 0;
long duration;
int distance;
void setup() {
SIM900.begin(19200);
Serial.begin(19200);
// Give time to your GSM shield log on to network
//delay(20000);
// AT command to set SIM900 to SMS mode
SIM900.print("AT+CMGF=1\r");
// Set module to send SMS data to serial out upon receipt
SIM900.print("AT+CNMI=2,2,0,0,0\r");
pinMode(led, OUTPUT);
pinMode(sirena, OUTPUT);
//digitalWrite(led,LOW);
pinMode(led2, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
if (timer == 1){
timer2++;
if (timer2 == 20){
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
// Calculate the distance:
distance = duration * 0.034 / 2;
Serial.print("Distance = ");
Serial.print(distance);
Serial.println(" cm");
timer2 = 0;
timer = 0;
}
}
else {
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
// Calculate the distance:
distance = duration * 0.034 / 2;
Serial.print("Distance = ");
Serial.print(distance);
Serial.println(" cm");
}
if(SIM900.available() >0) {
message = SIM900.readString();
timer=1;
//Get the character from the cellular serial port
}else
{
message = "";
}
if(message.indexOf("+381601416081")> -1){
if(message.indexOf("ON") > -1){
Serial.println("LED ON");
digitalWrite(led, HIGH);
}
else if(message.indexOf("OFF") > -1){
Serial.println("LED OFF");
digitalWrite(led,LOW);
}
}
if( distance < 20 && digitalRead(led) == HIGH){
SIM900.println("ATD + +381601416081;");
SIM900.println();
digitalWrite(sirena, HIGH);
delay(300);
digitalWrite(sirena, LOW);
}
}
Sometimes it lasts more than 20 times that's why I don't feel safe doing that.
I somehow fixed it with millis by doing this:
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= interval){
previousMillis = currentMillis;
if(state == LOW){
state = HIGH;
}else{
state = LOW;
}
digitalWrite(trigPin, state);
}
Now for some reason all i get printed is correct distance and 0s. And i made a condition to ignore 0s. Now it works somehow.
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.