Hello everyone I am trying to get my arduino uno to send me data from my ultrasonic range finder. I have a program right now that is allowing me to send it a message and then it does the ping but I am having trouble getting it to reply with the distance. I have another program that will let send me a message when it gets to a certain distance ( ==40) but I want it to send me all distances when I text it a code. Any help would be greatly appreciated and I am also very new to the arduino world. I am using and arduino uno and a sim 900 with a 4 pin ultrasonic range finder.
Thanks!
#include <GPRS_Shield_Arduino.h>
#include <SoftwareSerial.h>
#include <Wire.h>
#include <NewPing.h>
#define ledPin 13
#define TRIGGER_PIN 12 // Trigger Pin of sensor
#define ECHO_PIN 11 // Echo pin of sensor
#define MAX_DISTANCE 300 // Maximum distance we want to ping.
char inchar; // Will hold the incoming character from the GSM shield
SoftwareSerial SIM900(7, 8);
int led1 = 10;
int led2 = 11;
int led3 = 12;
int led4 = 13;
#define PIN_TX 7
#define PIN_RX 8
#define BAUDRATE 9600
#define PHONE_NUMBER "0000000000"
#define MESSAGE "X"
GPRS gprsTest(PIN_TX,PIN_RX,BAUDRATE);//RX,TX,Baud
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
void setup()
{
Serial.begin(19200);
// set up the digital pins to control
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // Open serial monitor
// wake up the GSM shield
SIM900power();
SIM900.begin(19200);
delay(20000); // give time to log on to network.
SIM900.print("AT+CMGF=1\r"); // set SMS mode to text
delay(100);
SIM900.print("AT+CNMI=2,2,0,0,0\r");
// blurt out contents of new SMS upon receipt to the GSM shield's serial out
delay(100);
Serial.println("Ready...");
}
void SIM900power()
// software equivalent of pressing the GSM shield "power" button
{
digitalWrite(9, HIGH);
delay(1000);
digitalWrite(9, LOW);
delay(7000);
}
void loop()
{
//If a character comes in from the cellular module...
if(SIM900.available() >0)
{
inchar=SIM900.read();
if (inchar=='#')
{
delay(10);
inchar=SIM900.read();
if (inchar=='a')
{
delay(10);
inchar=SIM900.read();
if (inchar=='0')
{
delay(50); // Wait 50ms between pings.
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
Serial.print("Ping: ");
Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
Serial.println("cm");
int X = uS / US_ROUNDTRIP_CM;
digitalWrite(ledPin,HIGH);
Serial.println("start to send message ...");
gprsTest.sendSMS(PHONE_NUMBER,MESSAGE); //define phone number and text
delay(1000);
}
{
SIM900.println("AT+CMGD=1,4"); // delete all SMS
}
}
}
}
}