#include<LiquidCrystal.h>
LiquidCrystal lcd(13,12,11,10,9,8);
#define vibrate_sense 7
char str[70];
char *test="$GPGGA";
char logitude[10];
char latitude[10];
int i,j,k;
int temp;
//int Ctrl+z=26; //for sending msg
int led=13;
void setup()
{
lcd.begin(16,2);
Serial.begin(4800);
pinMode(vibrate_sense, INPUT);
pinMode(led, OUTPUT);
lcd.setCursor(0,0);
lcd.print("..Initializing..");
lcd.setCursor(0,1);
lcd.print("....System...");
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Searching for");
lcd.setCursor(0,1);
lcd.print("Gps Signals");
delay(2300);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Setup Completed");
delay(500);
lcd.setCursor(0,0);
lcd.print("Accident detect");
lcd.setCursor(0,1);
lcd.print("& Alert system");
delay(1500);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("No accident");
lcd.setCursor(0,1);
lcd.print("occured");
}
void loop()
{
if (digitalRead(vibrate_sense)==0)
{
for(i=18;i<27;i++) //extract latitude from string
{
latitude[j]=str[i];
j++;
}
for(i=30;i<40;i++) //extract longitude from string
{
logitude[k]=str[i];
k++;
}
lcd.clear();
lcd.setCursor(0,0); //display latitude and longitude on 16X2 lcd display
lcd.print("Latitude(N)");
lcd.print(latitude);
lcd.setCursor(0,1);
lcd.print("Longitude(E)");
lcd.print(logitude);
delay(500);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Accident has");
lcd.setCursor(0,1);
lcd.print("been Detected");
delay(500);
lcd.clear();
lcd.print("Sending SMS");
delay(500);
lcd.clear();
Serial.begin(9600);
Serial.println("AT+CMGF=1"); //select text mode
delay(10);
Serial.println("AT+CMGS=\"7066128969\""); // enter receipent number
Serial.println("Vehicle Accident occured:");
Serial.print("Latitude(N): "); //enter latitude in msg
Serial.println(latitude); //enter latitude value in msg
Serial.print("Longitude(E): "); //enter Longitude in Msg
Serial.println(logitude); //enter longitude value in msg
Serial.print("http://maps.google.com/maps?&z=15&mrt=yp&t=k&q=");
Serial.println(latitude);
Serial.println("+");
Serial.println(logitude);
Serial.write(26); //send msg Ctrl+z=26
lcd.setCursor(0,0);
lcd.print("SMS Sent");
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Help Please");
temp=0;
i=0;
j=0;
k=0;
delay(20000); // next reading within 20 seconds
Serial.begin(4800);
}
}
void serialEvent()
{
while (Serial.available()) //Serial incomming data from GPS
{
char inChar = (char)Serial.read();
str[i]= inChar; //store incomming data from GPS to temparary string str[]
i++;
if (i < 7)
{
if(str[i-1] != test[i-1]) //check for right string
{
i=0;
}
}
if(i >=60)
{
break;
}
}
}
This is my code for my project.
It is accident detection system and it has to work like, when I press the sensor or limit switch, it has to show a message on an LCD that an accident occurred. But it shows the message on the LCD WITHOUT pressing the sensor button.
What to do? I think there is a mistake in my program. Can anybody please tell me where it is?
-
you have not learned the basics before attempting your program ...... start by writing a short program that turns on an LED when the button is pressed and turns off the LED when the button is releasedjsotola– jsotola2019年01月22日 18:05:46 +00:00Commented Jan 22, 2019 at 18:05
-
Do you have a pullup resistor?gre_gor– gre_gor2019年01月22日 21:55:06 +00:00Commented Jan 22, 2019 at 21:55
-
Please do not vandalize your or anyone else's posts. By posting on the Stack Exchange network, you've granted a non-revocable right for SE to distribute that content (under the CC BY-SA 3.0 license). By SE policy, any vandalism will be reverted.gre_gor– gre_gor2019年02月19日 17:44:25 +00:00Commented Feb 19, 2019 at 17:44
1 Answer 1
First off I edited indentation in your code because incorrect indentation is little bit confusing.
#include <LiquidCrystal.h>
LiquidCrystal lcd(13,12,11,10,9,8);
#define vibrate_sense 7
char str[70];
char *test="$GPGGA";
char logitude[10];
char latitude[10];
int i,j,k;
int temp;
//int Ctrl+z=26; //for sending msg
int led = 13;
void setup(){
lcd.begin(16,2);
Serial.begin(4800);
pinMode(vibrate_sense, INPUT);
pinMode(led, OUTPUT);
lcd.setCursor(0,0);
lcd.print("..Initializing..");
lcd.setCursor(0,1);
lcd.print("....System...");
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Searching for");
lcd.setCursor(0,1);
lcd.print("Gps Signals");
delay(2300);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Setup Completed");
delay(500);
lcd.setCursor(0,0);
lcd.print("Accident detect");
lcd.setCursor(0,1);
lcd.print("& Alert system");
delay(1500);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("No accident");
lcd.setCursor(0,1);
lcd.print("occured");
}
void loop(){
if (digitalRead(vibrate_sense) == 0){
for(i = 18; i < 27; i ++){ //extract latitude from string
latitude[j]=str[i];
j++;
}
for(i=30;i<40;i++){ //extract longitude from string
logitude[k] = str[i];
k++;
}
lcd.clear();
lcd.setCursor(0,0); //display latitude and longitude on 16X2 lcd display
lcd.print("Latitude(N)");
lcd.print(latitude);
lcd.setCursor(0,1);
lcd.print("Longitude(E)");
lcd.print(logitude);
delay(500);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Accident has");
lcd.setCursor(0,1);
lcd.print("been Detected");
delay(500);
lcd.clear();
lcd.print("Sending SMS");
delay(500);
lcd.clear();
Serial.begin(9600);
Serial.println("AT+CMGF=1"); //select text mode
delay(10);
Serial.println("AT+CMGS=\"7066128969\""); // enter receipent number
Serial.println("Vehicle Accident occured:");
Serial.print("Latitude(N): "); //enter latitude in msg
Serial.println(latitude); //enter latitude value in msg
Serial.print("Longitude(E): "); //enter Longitude in Msg
Serial.println(logitude); //enter longitude value in msg
Serial.print("http://maps.google.com/maps?&z=15&mrt=yp&t=k&q=");
Serial.println(latitude);
Serial.println("+");
Serial.println(logitude);
Serial.write(26); //send msg Ctrl+z=26
lcd.setCursor(0,0);
lcd.print("SMS Sent");
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Help Please");
temp = 0;
i = 0;
j = 0;
k = 0;
delay(20000); // next reading within 20 seconds
Serial.begin(4800);
}
}
void serialEvent(){
while (Serial.available()){ //Serial incomming data from GPS
char inChar = (char)Serial.read();
str[i] = inChar; //store incomming data from GPS to temparary string str[]
i++;
if (i < 7){
if(str[i-1] != test[i-1]){ //check for right string
i = 0;
}
}
if(i >= 60){
break;
}
}
}
Second thing is this line if (digitalRead(vibrate_sense) == 0){
you are checking there vibrate_sense for value 0. Are you sure, that triggered value is 0 and not 1? Try this code to test which value is equal to triggered state.
#define vibrate_sense 7
void setup() {
Serial.begin(9600);
Serial.println("Sensor testing");
pinMode(vibrate_sense, INPUT);
}
void loop() {
Serial.println(digitalRead(vibrate_sense));
delay(300);
}
Last thing is connection of your sensor/button. For button you have to use also PULL_UP/PULL_DOWN resistor to have correct logic value on digital pin.