I'm an absolute beginner is using arduino. I'm creating this device which uses multiprocessor mode. The master in the I2C has a GSM modem hooked up to it. The slave has a GPS as well as an HCSR04 sensor connected. HCSR04 senses the distance of the obstacles around and when the distance is within a defines limit, it powers up the LED. Now The master sends the request to slave every 1 minute, and then slave activates the GPS and then sends the co-ordinates to the master. Now I encountered following problems-
- After 1 minute after powering up the circuit, the led glowing stops and that stops forever. The stopping of the led operation probably indicates the slave is busy taking the coordinates. But it seems like it's caught in a loop.
- I'm getting the whole co-ordinates in the garbage form. The specific data is repeating. For example I get data like 020202020202020 or 198198198198........ like this. I've used the GPS code that I found on arduino.cc itself and it was for seperating the co-ordinates and writing them in sorted fashion but nothing is happening.
- The Master doesn't seem to receive the data, as the message isn't getting sent.
I'm using two arduino UNOs and I'm giving a 9V supply to them. The supply and other stuff isn't a problem as it seems to work just fine. I'm attaching a copy of the code.
Here's the code for MASTER-
#include <Wire.h>
#include<ctype.h>
//#include<Wire.h>
unsigned long t1, time;//variables for delay generation
int i = 0;
int data[100];// Stores the data of GPS received from slave
char msg[100];// Stores the ASCII converted message
void setup() {
// initialize serial communication
Serial.begin(9600);
Wire.begin();
Serial.print("\r");
delay(1000);
Serial.print("AT+CMGF=0\r");
delay(1000);
}
void loop()//This loop uses the millis function and creates the delay of 1 minute
{
t1 = millis() / 1000;
while (((millis() / 1000) - t1) < 60);
time++;
if (time == 1) {
message();
time = 0;
}
}
void message()// It requests data and sends the message using GSM modem
{
Wire.requestFrom(8,100);// Master requests the data from the slave
while (Wire.available()){}//check if message is available
for (i = 0; i < 100; i++)
{
data[i] = Wire.read();// Read the data
msg[i]=data[i]+48;//Convert to ASCII for sending to GSM
}
Serial.print("AT+CMGS=\"+918275256645\"\r"); //Number to which you want to send the sms
delay(1000);
for(i=0;i<100;i++)
{
Serial.print(msg[i]);//The text of the message to be sent
}
Serial.print("");
delay(1000);
Serial.write(0x1A);
delay(1000);
}
And the SLAVE code-
#include<Wire.h>
#include<string.h>
#include<ctype.h>
int gps=-1;//the variable where we store a single byte of GPS data
int i=0;
char cmd[7]="$GPRMC";
char buf[100];// To store the GPS data.
int ind=10;
int cont=0;
int cont1=0;
int cont2=0;
int data;
int indices[13];
char dat[100];
const int trigPin = 13;//The trigger of HCSR04
int echoPin = 12;//The echo of HCSR04
int motor=7;// Motor or LED is connected to Pin no.7
void setup() {
// put your setup code here, to run once:
pinMode(ind,INPUT);
Wire.begin(8);
Wire.onRequest(gpsfun);
// pinMode(led,OUTPUT);
Serial.begin(4800);
for(int i=0;i<100;i++){
buf[i]=' ';
}
}
void loop() {
// put your main code here, to run repeatedly:
long duration;
long duration1;
long cm;
long cm1;
//left code
// gpsfun();
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(5);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
cm = (duration/2)/29.1;
microsecondsToCentimeters(duration);
if(cm>20)//The distance distinction
{
digitalWrite(motor,LOW);
delay(2000);
digitalWrite(motor,HIGH);
delay(2000);
}
else
{
digitalWrite(motor,LOW);
delay(500);
digitalWrite(motor,HIGH);
delay(500);
}
}
void gpsfun(){
while(1)
{
gps=Serial.read();
if(gps==-1)
delay(100);
else
{
while(cont<100){
buf[cont1]=gps;
dat[cont1]=buf[cont1];
cont1++;
if(gps==13){
cont1=0;
cont2=0;
for(int i=1;i<7;i++){
buf[i]=cmd[i-1];
cont2++;
}
}
if(cont2==6){
for(int i=0;i<300;i++){
if(buf[i]==','){
indices[cont1]=i;
cont1++;
}
if(buf[i]=='*'){
indices[12]=i;
cont1++;
}
}
Serial.write("");
Serial.write("");
Serial.write("-----------");
for(int i=0;i<12;i++){
switch(i)
{
case 0 :Serial.print("Time in UTC (HhMmSs): ");break;
case 1 :Serial.print("Status (A=OK,V=KO): ");break;
case 2 :Serial.print("Latitude: ");break;
case 3 :Serial.print("Direction (N/S): ");break;
case 4 :Serial.print("Longitude: ");break;
case 5 :Serial.print("Direction (E/W): ");break;
case 6 :Serial.print("Velocity in knots: ");break;
case 7 :Serial.print("Heading in degrees: ");break;
case 8 :Serial.print("Date UTC (DdMmAa): ");break;
case 9 :Serial.print("Magnetic degrees: ");break;
case 10 :Serial.print("(E/W): ");break;
case 11 :Serial.print("Mode: ");break;
case 12 :Serial.print("Checksum: ");break;
}
for(int j=indices[i];j<(indices[i-1]-1);j++){
Serial.print(buf[j+1]);
}
Serial.write("");
}
Serial.write("-----------");
}
cont2=0;
for(int i=0;i<300;i++){
buf[i]=' ';
}
}
}
while(dat[i]){
Wire.write(dat[i]);//Send the data using the I2C
i++;
}
}
}
long microsecondsToCentimeters(long microseconds)//Converts the microsecnds processed by processor into centimeters.
{
return microseconds / 58.2;
}