Hello guys, I need help.. I made a simple code for the GSM shield to send message to my mobile phone after digital pin2 (through A0 pin) has a HIGH value ,the code works fine, however, as long as the GSM is always ON, it also always send the same messages even digital pin2 has LOW value already.. I need help.................................................
#include <SoftwareSerial.h>
SoftwareSerial mySerial1(2, 3);
//GSM gsmAccess;
//GSM_SMS sms;
int lev1 = A0;// 1st level input
int alert1 = 2;//1st level output
int a1 = 850; // minimum threshold level
int a2 = 876; // maximum threshold level
void setup()
{
pinMode(lev1, INPUT); // A0 as input
pinMode(alert1, OUTPUT);// pin 2 as output
mySerial1.begin(9600);
Serial.begin(9600);
delay(500);
}
void loop()
{
// for level 1:
int value1 = analogRead(lev1);
if (value1 > a1 && value1 < a2)
{
digitalWrite(alert1, HIGH);
Serial.print("Level 1 = ");
Serial.println(value1);
delay(1000);
}
else
{
digitalWrite(alert1, LOW);
}
if (mySerial1.available()>0)
Serial.write(mySerial1.read());
{
mySerial1.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
delay(500); // Delay of 1000 milli seconds or 1 second
mySerial1.println("AT+CMGS=\"+63__________\"\r"); // Replace x with mobile number
delay(500);
mySerial1.println("Hello World");// The SMS text you want to send
delay(60000);
mySerial1.println((char)26);// ASCII code of CTRL+Z
delay(500);
}
}
if (mySerial1.available() > 0)
Serial.write(mySerial1.read());
{
mySerial1.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
delay(500); // Delay of 1000 milli seconds or 1 second
mySerial1.println("AT+CMGS=\"+63__________\"\r"); // Replace x with mobile number
and so on ...
Which lines of code will be executed only when the test returns true and which will be executed regardless of the result of the test ?
Well basically its purpose is just to send the message if the test is true and I lack the codes to make the GSM shield to stop sending if A0 goes below and above the threshold values (a1 and a2)... Coz what happens now is that the GSM shield wont stop sending message coz it receives true, unless there'll be some codes to make it stop..
mySerial1.println("Hello World");// The SMS text you want to send
delay(1000);
mySerial1.println((char)26);// ASCII code of CTRL+Z
delay(500);
No.
When
if (mySerial1.available() > 0)
returns true then only
Serial.write(mySerial1.read());
will be executed
Whereas
mySerial1.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
delay(500); // Delay of 1000 milli seconds or 1 second
mySerial1.println("AT+CMGS=\"+63__________\"\r"); // Replace x with mobile number
delay(500);
mySerial1.println("Hello World");// The SMS text you want to send
delay(60000);
mySerial1.println((char)26);// ASCII code of CTRL+Z
delay(500);
will be executed each time through loop().
Put braces around the code to be executed when the test returns true so that it is not executed unconditionally each time through loop()
Can you help me? I'm not an expert in programming arduino. And I definitely don't know how to put braces.. Or what topic should I search to learn putting braces? I just need what area(s) do I have to look in the internet for this problem..
if (mySerial1.available() > 0) //if this is true
{ //execute the code in the pair of braces
Serial.write(mySerial1.read());
mySerial1.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
delay(500); // Delay of 1000 milli seconds or 1 second
mySerial1.println("AT+CMGS=\"+63__________\"\r"); // Replace x with mobile number
delay(500);
mySerial1.println("Hello World");// The SMS text you want to send
delay(60000);
mySerial1.println((char)26);// ASCII code of CTRL+Z
delay(500);
}
Can I suggest that you always use a pair of braces to enclose the code to be executed when a test is true even if there is only one line of code.
I tried it just now but I still keep on receiving text messages. I don't know if my idea is right but I'm thinking of adding a function that would limit the GSM shield in sending SMS, like say 3 messages every true values?
Yeah, It's working..right now I'm looking on improving the codes, I'm looking for limiting the sent messages.. I'm constructing another set of codes, I hope this works..
pin 2 is my serial Rx and at the same time the output pin of my A0 input signal. Coz i have a separate circuit that produces 4vdc when i shorted the circuit's (the separate circuit) ground and input terminal. the 4vdc goes to A0 Arduino and I assigned D2 (pin 2) as output..