Code doing loop without condition is true

hello friends,
I am having problem with my code. The thing is that code is being run in the loop without the sensor is active. I want it to be run when the sensor is active. can you help me out?

void loop()
{
// put your main code here, to run repeatedly:
int val1=LOW;
int val2=LOW;
int val3=LOW;
val1=digitalRead(pushb);
val2=digitalRead(vsen);
val3=digitalRead(ksen);
if (val1==HIGH)
{
Family_Message();
delay(1000);
}
else
if ((val2== HIGH)|| (val3== HIGH))
{
Emergency_Message();
delay(1000);
}
}

Only if you post ALL your code.

And tell us what the sensors are and how they are connected.

Welcome to the forum
And use code tags. You can edit your post by clicking on the :pencil2: down here :point_down:. Then highlight your code and use the code tags <|> up here :point_up_2:.

Try reading the guide to getting the most out of this forum

#include <SoftwareSerial.h>
#include <TinyGPS++.h>
#include <SD.h>
#include <SPI.h>

//setting software serial pins
SoftwareSerial SIM900(10,11);

/////////////declaring content////
TinyGPSPlus gps;

File myFile;
const int chipSelect=53;

double Latitude, Longitude;
String gmap = "Google Maps";

const int pushb=22;
const int vsen=23;
const int ksen=24;

void setup() {
// put your setup code here, to run once:

Serial.begin(19200);
SIM900.begin(19200);
Serial1.begin(9600);
Serial2.begin(9600);
pinMode(pushb, INPUT);
pinMode(vsen, INPUT);
pinMode(ksen, INPUT);
delay(1000);
Serial.print("INITIALIZING....");
SIM900.println("AT");//handshake withSIM900
upd_serial_SIM900();
/* SIM900.println("AT+GMGF=1");//CONGIGURE IN TEXT MODE
upd_serial_SIM900();
SIM900.println("AT+CMGS= "+23059293785"\r");// PHONE NUMBER TO BE TEXT
upd_serial_SIM900();
SIM900.print("Hey SUFYAAN HERE");
delay(100);
upd_serial_SIM900();
SIM900.println((char)26);
delay(100);
*/
}

void loop()
{
// put your main code here, to run repeatedly:
sen_con();
}

//updte the serial port for transmission or receiving data
void upd_serial_SIM900()
{
delay(500);
while(Serial.available())
{
SIM900.write(Serial.read());
}
while(SIM900.available())
{
Serial.write(SIM900.read());
}
}

// GPS informations
void gpsdata()
{
while(Serial1.available())
{
if(gps.encode(Serial1.read()))
{
Serial.println(gps.location.lat(),6);
Serial.println(gps.location.lng(),6);
delay(2000);
Latitude = gps.location.lat(),6;
Longitude = gps.location.lng(),6;
}

}
}

//////SD card
void sd()
{
while (!Serial2)
{

}
Serial.print("Initializing SD card..!");
if (!SD.begin())
{
Serial.println("Initializing failed..");
return;
}
Serial.println("Initializing Done..!");
myFile = SD.open("file.txt", FILE_READ);
if (myFile)
{
Serial.println("file.txt: ");
while (myFile.available())
{
Serial2.write(myFile.read());
myFile.println();
}
myFile.close();
}
else
{
Serial.println("Error file ccannot open");
}
}
/////
void Family_Message()
{
gpsdata();
SIM900.println("AT");
upd_serial_SIM900();
SIM900.println("AT+CMGF=1");
delay(1000);
upd_serial_SIM900();
SIM900.println("AT+CMGS= "+23059293785"\r");
delay(100);
upd_serial_SIM900();
SIM900.println("hey, I met an accident");
delay(100);
upd_serial_SIM900();
SIM900.println("\nHelp me out, follow the locations");
delay(100);
SIM900.print(gmap);
delay(100);
SIM900.print(Latitude, 6);
delay(100);
SIM900.print(",");
delay(100);
SIM900.println(Longitude, 6);
delay(100);
upd_serial_SIM900();
SIM900.println((char)26);
delay(1000);
}
void Emergency_Message()
{
gpsdata();
sd();
SIM900.println("AT");
upd_serial_SIM900();
SIM900.println("AT+CMGF=1");
delay(1000);
upd_serial_SIM900();
SIM900.println("AT+CMGS= "+23059293785"\r");
delay(100);
upd_serial_SIM900();
SIM900.println("Hello there, I met an accident");
delay(100);
SIM900.println("\n Help me out, follow the locations");
delay(100);
SIM900.print(gmap);
delay(100);
SIM900.print(Latitude, 6);
delay(100);
SIM900.print(",");
delay(100);
SIM900.println(Longitude, 6);
delay(100);
upd_serial_SIM900();
SIM900.println((char)26);
delay(1000);
}
void sen_con()
{
int val1=LOW;
int val2=LOW;
int val3=LOW;
val1=digitalRead(pushb);
val2=digitalRead(vsen);
val3=digitalRead(ksen);
if (val1==HIGH)
{
Family_Message();
delay(1000);
}
else
if ((val2== HIGH)|| (val3== HIGH))
{
Emergency_Message();
delay(1000);
}
}

i am using push button, knock sensor and vibration sensor

Well, that should work then.

But there’s more to developing a project than just buying, connecting and praying.

I’ll guarantee that God hasn’t written a line of code in all eternity.

Please post all the code, inserted in the code tags

#include <SoftwareSerial.h>
#include <TinyGPS++.h>
#include <SD.h>
#include <SPI.h>


//setting software serial pins
SoftwareSerial SIM900(10,11);





/////////////declaring content////
TinyGPSPlus gps;

File myFile;
const int chipSelect=53;

double Latitude, Longitude;
String gmap = "http://www.google.com/maps/place/";


const int pushb=22;
const int vsen=23;
const int ksen=24;



void setup() {
  // put your setup code here, to run once:
  
  Serial.begin(19200);
  SIM900.begin(19200);
  Serial1.begin(9600);
  Serial2.begin(9600);
  pinMode(pushb, INPUT);
  pinMode(vsen, INPUT);
  pinMode(ksen, INPUT);
  delay(1000);
  Serial.print("INITIALIZING....");
  SIM900.println("AT");//handshake withSIM900
  upd_serial_SIM900();
 /* SIM900.println("AT+GMGF=1");//CONGIGURE IN TEXT MODE
  upd_serial_SIM900();
  SIM900.println("AT+CMGS= \"+23059293785\"\r");// PHONE NUMBER TO BE TEXT
  upd_serial_SIM900();
  SIM900.print("Hey SUFYAAN HERE");
  delay(100);
  upd_serial_SIM900();
  SIM900.println((char)26);
  delay(100);
  */
}

void loop() 
{
  // put your main code here, to run repeatedly:
    sen_con();
}

//updte the serial port for transmission or receiving data
void upd_serial_SIM900()
{
  delay(500);
  while(Serial.available())
  {
    SIM900.write(Serial.read());
  }
  while(SIM900.available())
  {
    Serial.write(SIM900.read());
  }
 }

 // GPS informations
 void gpsdata()
 {
  while(Serial1.available())
  {
    if(gps.encode(Serial1.read()))
    {
      Serial.println(gps.location.lat(),6);
      Serial.println(gps.location.lng(),6);
      delay(2000);
      Latitude = gps.location.lat(),6;
      Longitude = gps.location.lng(),6; 
    }
  
 }
 }

 //////SD card
 void sd()
 {
 while (!Serial2)
 {
  
 }
 Serial.print("Initializing SD card..!");
 if (!SD.begin())
 {
  Serial.println("Initializing failed..");
  return;
 }
 Serial.println("Initializing Done..!");
 myFile = SD.open("file.txt", FILE_READ);
 if (myFile)
 {
  Serial.println("file.txt: ");
  while (myFile.available())
  {
    Serial2.write(myFile.read());
    myFile.println();
  }
  myFile.close();
 }
 else
 {
  Serial.println("Error file ccannot open");
 }
 }
 /////
 void Family_Message()
 {
  gpsdata();
  SIM900.println("AT");
  upd_serial_SIM900();
  SIM900.println("AT+CMGF=1");
  delay(1000);
  upd_serial_SIM900();
  SIM900.println("AT+CMGS= \"+23059293785\"\r");
  delay(100);
  upd_serial_SIM900();
  SIM900.println("hey, I met an accident");
  delay(100);
  upd_serial_SIM900();
  SIM900.println("\nHelp me out, follow the locations");
  delay(100);
  SIM900.print(gmap);
  delay(100);
  SIM900.print(Latitude, 6);
  delay(100);
  SIM900.print(",");
  delay(100);
  SIM900.println(Longitude, 6);
  delay(100);
  upd_serial_SIM900();
  SIM900.println((char)26);
  delay(1000);
 }
 void Emergency_Message()
 {
  gpsdata();
  sd();
  SIM900.println("AT");
  upd_serial_SIM900();
  SIM900.println("AT+CMGF=1");
  delay(1000);
  upd_serial_SIM900();
  SIM900.println("AT+CMGS= \"+23059293785\"\r");
  delay(100);
  upd_serial_SIM900();
  SIM900.println("Hello there, I met an accident");
  delay(100);
  SIM900.println("\n Help me out, follow the locations");
  delay(100);
  SIM900.print(gmap);
  delay(100);
  SIM900.print(Latitude, 6);
  delay(100);
  SIM900.print(",");
  delay(100);
  SIM900.println(Longitude, 6);
  delay(100);
  upd_serial_SIM900();
  SIM900.println((char)26);
  delay(1000);
 }
 void sen_con()
 {
  int val1=LOW;
  int val2=LOW;
  int val3=LOW;
  val1=digitalRead(pushb);
  val2=digitalRead(vsen);
  val3=digitalRead(ksen);
  if (val1==HIGH)
  {
    Family_Message();
    delay(1000);
  }
  else
  if ((val2== HIGH)|| (val3== HIGH))
  {
    Emergency_Message();
    delay(1000);
  }
 }

You aren't using INPUT_PULLUP so, for your button at least, you MUST have an external pull-up or pull-down resistor. Show that part of your circuit.

OK so what does this code have to do with the original code you posted, and your original question?

Also there is a lot more going on in this code that you showed on your physical layout diagram in post #8.

We don't like being made fools of here and this is what it looks like to me.

So please be honest about your project because I don't react favourably to lies.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.