gsm and reed module

Hi guys so im using 2 module a gsm module and reed module. I am working on a simple lighting an led kind of thing except by turning on and turning off uses different modules. Initially, the reed module sense a magnet and when i text LEDon the led will turn on then I wan to turn it off when the reed module does not sense a magnet anymore. It turns on when I text LEDon but when i remove the magnet from the reed module it does not turn off. Can you tell me what is missing in my code? So here is my code.

#include <SoftwareSerial.h>


SoftwareSerial gsm(9, 10);
int reed = 12;
int led = 5;

void setup ()
{
  pinMode (reed, INPUT);
  pinMode (led,OUTPUT);
  Serial.begin(9600);
gsm.begin(9600);
delay(2000);             
gsm.println("AT+CMGF=1");    
delay(1000);

gsm.println("AT+CNMI=2,2,0,0,0");
delay(1000);
}
void loop ()
{
  int magnet = digitalRead (reed);
  while (gsm.available())
{
 delay(3); 
 String a = gsm.readString();
  if (a.indexOf("LEDon")>=0)
  {
   digitalWrite(led,HIGH);

  gsm.println("AT+CMGS=\"09xxxx\"\r");

  delay(1000);
  gsm.println("LED IS ON");
  delay(500);
 gsm.println(char(26));
  }
  else if (magnet ==HIGH)
  {
    digitalWrite (led,LOW);
   

  
  gsm.println("AT+CMGS=\"09xxxx\"\r");

  delay(1000);
   gsm.println("LED IS OFF");
  delay(500);
  gsm.println(char(26));
  }
}
}

I am just a beginner in arduino so please bear with me.

Your code says:

while (gsm.available())
{
    if (a.indexOf("LEDon") >= 0)
    {
        // send LED is On
    }
    else if (magnet == HIGH)
    {

That means you only check the reed switch input when you receive a GSM message and it does NOT contain "LEDon". That seems wrong. I think you want to check the reed switch input AFTER you take care of GSM messages.

Do you want the GSM messages and the reed switch to both control the LED? You probably want the reed switch to only control the LED when it changes. For that see File->Examples->02.Digital->StateChangeDetection.

johnwasser:
Your code says:

while (gsm.available())

{
    if (a.indexOf("LEDon") >= 0)
    {
        // send LED is On
    }
    else if (magnet == HIGH)
    {




That means you only check the reed switch input when you receive a GSM message and it does NOT contain "LEDon". That seems wrong. I think you want to check the reed switch input AFTER you take care of GSM messages.

Do you want the GSM messages and the reed switch to both control the LED? You probably want the reed switch to only control the LED when it changes. For that see File->Examples->02.Digital->StateChangeDetection.

So after I send the message "LEDon" and the led turn on i will change the detection?

johnwasser:
Your code says:

while (gsm.available())

{
    if (a.indexOf("LEDon") >= 0)
    {
        // send LED is On
    }
    else if (magnet == HIGH)
    {




That means you only check the reed switch input when you receive a GSM message and it does NOT contain "LEDon". That seems wrong. I think you want to check the reed switch input AFTER you take care of GSM messages.

Do you want the GSM messages and the reed switch to both control the LED? You probably want the reed switch to only control the LED when it changes. For that see File->Examples->02.Digital->StateChangeDetection.

I want the gsm to control when to turn on the led and the reed switch to turn off the led

ymmot17:
I want the gsm to control when to turn on the led and the reed switch to turn off the led

OK. Take the ‘magnet’ part out of the GSM part. Use the StateChangeDetection example to detect when the reed switch input goes from LOW to HIGH and turn off the LED then (if it is on). Something like this:

#include <SoftwareSerial.h>
SoftwareSerial gsm(9, 10);
const int ReedSwitchPin = 12;
const int LEDPin = 5;
boolean PreviousReedSwitchState;
boolean LEDState = LOW;
void setup () {
  pinMode (ReedSwitchPin, INPUT);
  PreviousReedSwitchState = digitalRead(ReedSwitchPin);
  pinMode (LEDPin, OUTPUT);
  digitalWrite(LEDPin, LEDState);
  Serial.begin(9600);
  gsm.begin(9600);
  delay(2000);
  gsm.println("AT+CMGF=1");
  delay(1000);
  gsm.println("AT+CNMI=2,2,0,0,0");
  delay(1000);
}
void loop () {
  int reedSwitchState = digitalRead (ReedSwitchPin);
  // Look for LEDon message from GSM
  if (gsm.available()) {
    String a = gsm.readString();
    if (a.indexOf("LEDon") >= 0) {
      if (LEDState == HIGH)
        GSMSendMessage("LED WAS ALREADY ON");
      else
        GSMSendMessage("LED IS ON");
      LEDState = HIGH;
      digitalWrite (LEDPin, LEDState);
    }
  }
  // Look for reed switch input going HIGH
  // Note: Debounce might be needed here
  if (reedSwitchState != PreviousReedSwitchState) {
    // State has changed.
    PreviousReedSwitchState = reedSwitchState;
    if (reedSwitchState == HIGH) {  // Magnet moved away
      GSMSendMessage("LED IS OFF");
      LEDState = LOW;
      digitalWrite (LEDPin, LEDState);
    }
  }
}
void GSMSendMessage(const char *message) {
  gsm.println("AT+CMGS=\"09xxxx\"\r");
  delay(1000);
  gsm.println(message);
  delay(500);
  gsm.println(char(26));
}

johnwasser:
OK. Take the ‘magnet’ part out of the GSM part. Use the StateChangeDetection example to detect when the reed switch input goes from LOW to HIGH and turn off the LED then (if it is on). Something like this:

#include <SoftwareSerial.h>

SoftwareSerial gsm(9, 10);
const int ReedSwitchPin = 12;
const int LEDPin = 5;
boolean PreviousReedSwitchState;
boolean LEDState = LOW;
void setup () {
  pinMode (ReedSwitchPin, INPUT);
  PreviousReedSwitchState = digitalRead(ReedSwitchPin);
  pinMode (LEDPin, OUTPUT);
  digitalWrite(LEDPin, LEDState);
  Serial.begin(9600);
  gsm.begin(9600);
  delay(2000);
  gsm.println(“AT+CMGF=1”);
  delay(1000);
  gsm.println(“AT+CNMI=2,2,0,0,0”);
  delay(1000);
}
void loop () {
  int reedSwitchState = digitalRead (ReedSwitchPin);
  // Look for LEDon message from GSM
  if (gsm.available()) {
    String a = gsm.readString();
    if (a.indexOf(“LEDon”) >= 0) {
      if (LEDState == HIGH)
        GSMSendMessage(“LED WAS ALREADY ON”);
      else
        GSMSendMessage(“LED IS ON”);
      LEDState = HIGH;
      digitalWrite (LEDPin, LEDState);
    }
  }
  // Look for reed switch input going HIGH
  // Note: Debounce might be needed here
  if (reedSwitchState != PreviousReedSwitchState) {
    // State has changed.
    PreviousReedSwitchState = reedSwitchState;
    if (reedSwitchState == HIGH) {  // Magnet moved away
      GSMSendMessage(“LED IS OFF”);
      LEDState = LOW;
      digitalWrite (LEDPin, LEDState);
    }
  }
}
void GSMSendMessage(const char *message) {
  gsm.println(“AT+CMGS=“09xxxx”\r”);
  delay(1000);
  gsm.println(message);
  delay(500);
  gsm.println(char(26));
}

Thank you so much! I haven’t tried this yet but i know this will work. Can I ask another question? This is just part of my project. My main concept is texting the system 1 PILL, 2 PILL or 3 PILL then leds would light up that corresponds to the number of pills to be taken. Then once a led turn on there is a magnet in a container cap that once you open it the led will turn off. Then once you get 1, 2 or 3 pills the gsm will send a message to a caregiver or something that 1, 2 or 3 pills are taken from the container. I already made the code for the pills taken but I can’t seem to combine them. Should I add another void for that? This is the code that I have to combine to the code that you made for me. Thank you so much!

#include <SoftwareSerial.h>

#include <HX711.h>

SoftwareSerial gsm(9, 10);
HX711 scale(A1, A0);
long val = 0;
long quantity = 0;
float count = 0;
long newquantity = 0;

void setup() {
Serial.begin(9600);
gsm.begin(9600);
delay(2000);
gsm.println(“AT+CMGF=1”);
delay(1000);

gsm.println(“AT+CNMI=2,2,0,0,0”);
delay(1000);
}

void loop() {

count = count + 1;
val= scale.read ();
quantity = newquantity;
newquantity = ((((((val - 107400)/305890.0f) * 50))-35.9)/0.555);

delay (1000);

if (newquantity == quantity - 1)
{
gsm.println(“AT+CMGS=“09561345333”\r”);

delay(1000);
gsm.println(“1 PILL WAS TAKEN”);
delay(500);
gsm.println(char(26));
}

else if (newquantity == quantity + 1)
{
gsm.println(“AT+CMGS=“09561345333”\r”);

delay(1000);
gsm.println(“1 PILL WAS ADDED”);
delay(500);
gsm.println(char(26));
}

else if (newquantity == quantity - 2)
{
gsm.println(“AT+CMGS=“09561345333”\r”);

delay(1000);
gsm.println(“2 PILLS WERE TAKEN”);
delay(500);
gsm.println(char(26));
}

else if (newquantity == quantity + 2)
{
gsm.println(“AT+CMGS=“09561345333”\r”);

delay(1000);
gsm.println(“2 PILLS WERE ADDED”);
delay(500);
gsm.println(char(26));

}
else if(newquantity == quantity - 3)
{
gsm.println(“AT+CMGS=“09561345333”\r”);

delay(1000);
gsm.println(“3 PILLS WERE TAKEN”);
delay(500);
gsm.println(char(26));

}
else if (newquantity == quantity +3)
{
gsm.println(“AT+CMGS=“09561345333”\r”);

delay(1000);
gsm.println(“3 PILLS WERE ADDED”);
delay(500);
gsm.println(char(26));

}

delay (5000);
}