Hi!
I was trying to make a miniature sewer cleaning model with underground trash bin and SIM900A would send me a sms message if sewer is blocked with trash water level distance would become less than 3cm and it would start cleaning itself.(Gate, Gaw, drag pins are connected to relay module to control pneumatic cylinders.)
And if Infrared sensors would sense if the trash bins are full and would send me a sms message as well. Lift the trash bin by sending SIM900A a sms message from another phone. Everything works except it would take almost a minute to read the message and do the process. Distance and AT commands are mixed up on serial monitor.
I have no idea why it slows down but when i seperate the message receiving codes and ultrasonic message notifier code and run each of them on own it's fine.
AT commands are seperated to different lines when reading message. and even simple AT command would look like this
ADistance: 5
TDistance: 5
Distance: 5
ODistance: 5
KDistance: 5
SIM900A got it own external supply(4.2V and 2A)
I tried changing baud rates but seems this is not the problem.
and here's my code. Sorry, I suck at writing codes. : (
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10,11);
char msg,t;
int d;
int i;
int i3;
int i4;
int count3;
int count2;
int count4;
const int trigPin = 6;
const int echoPin = 7;
// defines variables
long duration;
int distance;
int Gate=2,Gaw=3,drag=4,TrashBin=5,count=0;
void setup() {
mySerial.println("AT+CMGF=1");
mySerial.print("AT+CNMI=1,2,0,0,0");
pinMode(8, INPUT);
pinMode(9, INPUT);
pinMode(12, INPUT);
pinMode(Gate, OUTPUT);
pinMode(Gaw, OUTPUT);
pinMode(drag, OUTPUT);
pinMode(TrashBin, OUTPUT);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(19200); // Starts the serial communication
mySerial.begin(9600);
Serial.println();
delay(100);
}
void loop() {
ultrasonic();
receive();
for(d=0;d<6;d++)
{if(distance<3)
{count2=count2+1;
delay(1000);}
else count2=0;}
if (count2>3)
{ Serial.println("SMS sending");
delay(1000);
mySerial.println("AT+CMGS="+959697092525"\r"); // Replace x with mobile number
delay(1000);
mySerial.println("Trash found-BHN ID(S0001)");
delay(100);
mySerial.println((char)26);// ASCII code of CTRL+Z
delay(1000);
Serial.println("SMS sent");
digitalWrite(Gate,HIGH);
delay(1000);
digitalWrite(drag,HIGH);
delay(1000);
digitalWrite(Gaw,HIGH);
delay(1000);
digitalWrite(drag,LOW);
delay(1000);
digitalWrite(Gaw,LOW);
delay(1000);
digitalWrite(Gate,LOW);
delay(1000);
}
for(i=0;i<6;i++)
{if(digitalRead(8)==0)
{count=count+1;
delay(1000);}
else count=0;}
if (count>6)
{ Serial.println("SMS sending");
mySerial.println("AT+CMGS="+959697092525"\r"); // Replace x with mobile number
delay(1000);
mySerial.println("BHN BinID(T0001) - FULL");
delay(100);
mySerial.println((char)26);// ASCII code of CTRL+Z
delay(1000);
Serial.println("SMS sent-Bin1");
receive();
}
for(i3=0;i3<6;i3++)
{if(digitalRead(9)==0)
{count3=count+1;
delay(1000);}
else count=0;}
if (count>6)
{
Serial.println("SMS sending");
//Sets the GSM Module in Text Mode
mySerial.println("AT+CMGS="+959679822800"\r"); // Replace x with mobile number
delay(100);
mySerial.println("BHN BinID(T0002) - FULL");
delay(100);
mySerial.println((char)26);// ASCII code of CTRL+Z
delay(100);
Serial.println("SMS sent-Bin2");
}
for(i4=0;i4<6;i4++)
{if(digitalRead(12)==0)
{count4=count4+1;
delay(1000);}
else count=0;}
if (count>6)
{ Serial.println("SMS sending");
//Sets the GSM Module in Text Mode
mySerial.println("AT+CMGS="+959679822800"\r"); // Replace x with mobile number
delay(100);
mySerial.println("BHN BinID(T0002) - FULL");
delay(100);
mySerial.println((char)26);// ASCII code of CTRL+Z
delay(100);
Serial.println("SMS sent-DragBin");
}
}
void ultrasonic(){
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
delay(40);
}
void receive(){
{
if (mySerial.available()>0)
{
t=mySerial.read();
if(t=='B')
digitalWrite(TrashBin,HIGH);
else if(t=='C')
digitalWrite(TrashBin,LOW);
if(t=='D')
digitalWrite(drag,HIGH);
else if(t=='E')
digitalWrite(drag,LOW);
delay(1000);
}
}}