0

I just stayed on delay() since that is the only thing I know but the thing is delay() cannot be interrupted. What I'm trying to achieve with this code is:

  1. When the soilSensor detects moisture it would send a text message to a number only ONCE without terminating the program even if in the next loop the soilSensor is still detecting moisture.

  2. The soilSensor's delay is delay(60000) and we would still wait for while to finish the delay but what if in the middle of the soilSensor's delay(60000) there is a movement for just about 5 seconds? What to do so that the tiltSensor can overlap the soilSensor's delay and sends a message to a number?

I want this program to run forever. Please help me! I needed this ASAP!

#include <SoftwareSerial.h>
SoftwareSerial SIM900(7, 8);
int led=22;

void setup()
{
  Serial.begin(9600);
  SIM900.begin(19200);
  pinMode(led, OUTPUT);
  delay(1000); 
}

void sendSMS1()
{
  SIM900.println("AT + CMGS = \"+************\"");
  delay(100);
  Serial.println("Water Detected");
  delay(100);
  SIM900.println("Water Detected"); 
  delay(100);
  SIM900.println((char)26); 
  delay(100);
  SIM900.println();
  delay(100); 
}


void sendSMS2()
{
  SIM900.println("AT + CMGS = \"+************\"");
  delay(100);
  Serial.println("Movement Detected");
  delay(100);
  SIM900.println("Movement Detected"); 
  delay(100);
  SIM900.println((char)26); 
  delay(100);
  SIM900.println();
  delay(100);  
}


void loop()
{    
  int soilSensor = analogRead(A0);
  int tiltSensor = analogRead(A1);

  if(soilSensor >= 10)
  {
    digitalWrite(22, HIGH);
    Serial.println(soilSensor);
    sendSMS1();
    delay(60000);
  }
  else
  {
    digitalWrite(22, LOW);
    delay(60000); 
  } 

  if (tiltSensor >= 100)
  {
    digitalWrite(led, HIGH);
    Serial.println(tiltSensor);
    sendSMS2();
    delay(60000);
  }
  else 
  {
    digitalWrite(led, LOW);
    Serial.println(tiltSensor);
    delay(60000);
  }
}
  • 3
    BlinkWithoutDelay is good reference material for solving this issue. – uint128_t Feb 4 '16 at 4:53
  • If you were the Arduino, how would you go about solving the problem? – Majenko Feb 4 '16 at 12:29
1

You use millis() to keep track of time and also prevent blocking. Replicating your program logic:

long soil_timer = 0;
long tilt_timer = 0;
char soil_flag = 1;
char tilt_flag = 1;

void loop() { 
    int soilSensor = analogRead(A0); 
    int tiltSensor = analogRead(A1); 

  if (soil_flag == 1) {
    if (soilSensor >= 10) { 
      digitalWrite(22, HIGH);
      Serial.println(soilSensor);
      sendSMS1(); 
    }
   else { 
      digitalWrite(22, LOW);
    }
    soil_timer = millis();
    soil_flag = 0;
  }

  if (tilt_flag == 1) {
    if (tiltSensor >= 100) { 
       digitalWrite(led, HIGH); 
       Serial.println(tiltSensor);
       sendSMS2();
    } 
    else { 
       digitalWrite(led, LOW); 
       Serial.println(tiltSensor);
    } 
    tilt_flag = 0;
    tilt_timer = millis();
  }
  if (millis() - soil_timer >= 60000){
     soil_flag = 1;
  }
  if (millis() - tilt_timer >= 60000){
     tilt_flag = 1;
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.