How to end this loop

hi guys, let me tell you on what i’ve been working on…im using a gas sensor to detect LPG gas, and once its been detected, the output will be; GREEN LED will be LOW(safe), RED LED will be HIGH(danger),buzzer will be HIGH, and coordinates will be sent to a safe center through GPS/GSM module… ok, the problem is:

  1. How do i make the GREEN LED stay LOW,RED stay HIGH,buzzer stay HIGH, after during and after sending the message,i’ve found out, when the system reaches the sending msg part, the LED’S and buzzer all stay low, i know because it follows the loop. So i need help with this part pls

2)How do i keep the RED LED HIGH,buzzer HIGH, but only send the message once? i’ve found out, after the loop finishes, it returns and starts back again, i just want the system to send ONE warning message, and let the RED LED and buzzer be HIGH and only END when we push the reset button.

//CODINGS
void loop()
{
val = analogRead(sensorValue);
Serial.println(val);

if (val < 350)
{
sensor1=0;
}
else
{
sensor1=1;
}

if (sensor1==0)
{
digitalWrite(LEDgreen,HIGH)
digitalWrite(LEDred,LOW);
digitalWrite(buzzer,LOW);
delay(500);
}
else if ( sensor1==1)
{
digitalWrite(LEDgreen,LOW);
digitalWrite(LEDred,HIGH);
digitalWrite(buzzer,HIGH);
delay(500);
Serial.println(“AT”); //Send AT command
delay(1000);
Serial.println(“AT”);
delay(1000);
Serial.println(“AT+CREG=1”);
delay(2000);
//Send message
Serial.println(“AT+CMGF=1”);
delay(1000);
Serial.println(“AT+CMGS=“INSERT PHONE NUMBER””);//Change the receiver phone number
delay(1000);
Serial.print(“Warning, gas leak detected at:\n”);
Serial.print(“Global Clock:”);
UTC();
Serial.print(“Latitude:”);
latitude();
Serial.print(“Direction:”);
lat_dir();
Serial.print(“Longitude:”);
longitude();
Serial.print(“Direction:”);
lon_dir();
Serial.write(26);
}
}

PLEASE HELP ME THANK YOU! (below i’ve attached the real codings)

Edit_Code_GPS_SMS.ino (5.91 KB)

Change the whole way you are going about it.

Instead of the sensor triggering the outputs, use the sensor to trigger changes in a finite state machine.

You want it to stop and just sit until there is a Reset?

      Serial.print("Direction:");
      lon_dir();
      Serial.write(26);
// add indefinite hold
     while(1);  // sit here until reset occurs
    }

Or is there some condition that will allow it to restart?

void loop()
{
 if (messageFlag == 0){
  val = analogRead(sensorValue);
:
:
      Serial.print("Direction:");
      lon_dir();
      Serial.write(26);
messageFlag = 1;  //  Define conditions elsewhere that can clear the flag
    }
  } // added to complete flag test above

CrossRoads:
You want it to stop and just sit until there is a Reset?

      Serial.print("Direction:");

lon_dir();
     Serial.write(26);
// add indefinite hold
    while(1);  // sit here until reset occurs
   }

Yea this is what i mean, but will putting while(1) cause the system to loop and the message to be sent again? cause i only want the RED LED and BUZZER to be HIGH, and only send the message once after the (sensor1==1) condition has been met. So the RED LED and buzzer will continue to be HIGH during and after sending a message until i push the RESET button

while(1); where I put it will do that.

CrossRoads:
while(1); where I put it will do that.

So putting while(1) will make the RED LED and BUZZER continue to stay HIGH while sending only one message? Thanks, i'll be right back,im gonna try it out

I believe so. Digital outputs don't change after being written, until re-written, or the chip reset. I'm heading out, back tonight.

CrossRoads:
I believe so. Digital outputs don’t change after being written, until re-written, or the chip reset. I’m heading out, back tonight.

Thank you sir, it worked out well…I really appreciate it.

My last problem is this, my GPS module takes around 5 minutes to acquire the coordinates, therefore i need to put it in a while(1) loop until the latitude and longitude has been acquired. Therefore, i must let the program run around 5 mins,until the latitude and longitude has been acquired,then i am able to test my gas sensor. So how do i move from the while(1) loop to meet my conditions, because even after acquiring the coordinates, when i tested my gas sensor, it still run the while(1) loop. What i want is, when the conditions have been met ( val > 350 ) , the while(1) loop for acquiring the coordinates stops , and proceed to the LED&BUZZER high, and send out a message. Does putting the while(1) loop for coordinate acquiring in the void setup() incorrect?

Code_GPS_SMS_2.ino (6.12 KB)

while (val <= 350)…?

majenko:
while (val <= 350)…?

What exactly do you mean by “while( val<=350)”? Did you mean if(val<350) , you asked the program to do the while(1) searching the coordinates loop? I haven’t thought of that one…i’ll give it a try later on,THANK YOU for the idea

No, I mean, instead of while(1), which loops forever, use while(val <= 350) which loops until val > 350.

My last problem is this, my GPS module takes around 5 minutes to acquire the coordinates, therefore i need to put it in a while(1) loop until the latitude and longitude has been acquired.

How do you know when you have a good coordinate?
Can do this:

void loop(){
if (good_coordinatesFlag == 0){
get gps coordinates
   if (good_coordinates_received){
   good_coordinatesFlag = 1;
   }
}
if (good_coordinatesFlag == 1){
do your sensor testing stuff
}

}

pseudo code used, but should give you the idea.

That's the thing sir,the GPS im using takes time to acquire the coordinate,it wouldnt just get a specific coordinate directly after some time,but it will eventually becomes stable(based on what i've tried yesterday out on my yard)..but my project is suppose to sense gas leaks from kitchen stoves for houses,so there's is no right coordinate...i just wanna know if it is possible to get out from the while(1) loop(coordinates acquiring in void setup,can see the codings from my last two posts) when the gas sensor detects an input..i put the while(1) loop in the void setup since i thought, the program should not run anything else

CrossRoads:

My last problem is this, my GPS module takes around 5 minutes to acquire the coordinates, therefore i need to put it in a while(1) loop until the latitude and longitude has been acquired.

How do you know when you have a good coordinate?

pseudo code used, but should give you the idea.

Change this then:

if (good_coordinatesFlag == 0){
get gps coordinates
   if (good_coordinates_received){
   good_coordinatesFlag = 1;
   }
}

to

currentTime = millis();
if (currentMillis >= 300000UL){ // wait for 5 minutes - adjust this # as needed.
good_coordinatesFlag = 1;
}

How do you think i should include the codes you've given to my program,should i put it in the while(1) loop above the acquiring coordinates loop?? If i do that, can the loop stops after 5 minutes?Sorry to take up your time sir,would really appreciate it if you be more clearer,im still learning,thank you!

CrossRoads:
Change this then:
to

currentTime = millis();

if (currentMillis >= 300000UL){ // wait for 5 minutes - adjust this # as needed.
good_coordinatesFlag = 1;
}

The stuff goes in void loop just as I wrote it.
You need to add the variable declarations. Code structure is basically:

// presetup code
// pin assignments
// variable declarations
unsigned long currentMillis;

void setup(){
// pinMode calls
Serial.begin(speed); // if used
}

void loop(){
currentTime = millis();
if (currentMillis >= 300000UL){ // wait for 5 minutes - adjust this # as needed.
good_coordinatesFlag = 1;
}
if (good_coordinatesFlag == 1){
// do your sensor testing stuff
}

} // end loop

CrossRoads:
The stuff goes in void loop just as I wrote it.
You need to add the variable declarations. Code structure is basically:

// presetup code

// pin assignments
// variable declarations
unsigned long currentMillis;

void setup(){
// pinMode calls
Serial.begin(speed); // if used
}

void loop(){
currentTime = millis();
if (currentMillis >= 300000UL){ // wait for 5 minutes - adjust this # as needed.
good_coordinatesFlag = 1;
}
if (good_coordinatesFlag == 1){
// do your sensor testing stuff
}

} // end loop

Ohh so there's no need to make a while(1) loop for coordinate acquiring,just let te program run for 5 mins,and make it high? Haven't thought about this one yet..but im just not sure how to incorporate this if (currentMillis >= 300000UL), with my analog readings ..nvmd Will give it a try sir,thank you for your time will let you mnow how it turns up..

This
if (currentMillis >= 300000UL){
and this
if (good_coordinatesFlag == 1){
will always be true after 5 minutes, so this
// do your sensor testing stuff
will run continuously after that, taking readings, and settings the output HIGH if the sensor goes off.

// do your sensor testing stuff

Assuming your GPS device is emitting NMEA strings, it will tell you when it has a good position. You will get better results using that information than waiting for a fixed time. I'd put an LED on it too that gets lit once the device has a location so that you can move it closer to a window if it fails to see sufficient satellites.