I am Totally new here so excuses my lack of knowledge, I've been tying to send a SMS once power is down even though I managed to send a SMS Arduino won't stop sending SMS.
I have rewrite it countless time but still stuck, I really have no idea what is going on.
I simply want to send one SMS once power is down and do nothing when power is back on.
it's a shame this is my second week yet I've been stuck in the same loop. I am posting 3 different set of codes. Sorry I can't separate them anyways
Feel free to suggest, edit Any help is welcome
#include <SoftwareSerial.h>
SoftwareSerial mySerial(9, 10);
char msg;
int pushButton (2);
bool SMSSent = false;
void setup()
{
pinMode(2, INPUT_PULLUP);
pinMode(13, OUTPUT);
mySerial.begin (9600); //Setting the baud rate of GSM Module
delay(500);
Serial.begin (9600); //Setting the baud rate of Serial Monitor (Arduino)
delay(500); // Delay to prevent this "************GSM SIM900A BEGIN"
Serial.println ("GSM MODULE BEGIN"); //To print on serial monitor
delay(1000);
}
void loop()
{
bool SMSSent = false;
if (digitalRead (2) == LOW);
delay(100);
SMSSent = !SMSSent;
mySerial.write(sendSMS(), SMSSent);
Serial.println("SMS SENT SUCCESSFUL");
}
delay(50);
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
delay(100);
}
else {
sendSMS();
delay(1000);
digitalWrite(13, HIGH);
delay(800);
digitalWrite(13, LOW);
delay(800);
}
}
void sendSMS()
{
mySerial.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
delay(1200);
mySerial.println("AT+CMGS=\"+9000000000\"\r");
delay(1200);
mySerial.println("POWER DOWN");
delay(200);
mySerial.println((char)26);
delay(1200);
}
#include <SoftwareSerial.h>
SoftwareSerial mySerial(9, 10);
char msg;
void setup()
{
pinMode(2, INPUT_PULLUP);
pinMode(13, OUTPUT);
mySerial.begin (9600); //Setting the baud rate of GSM Module
delay(500);
Serial.begin (9600); //Setting the baud rate of Serial Monitor (Arduino)
delay(500); // Delay to prevent this "************GSM SIM900A BEGIN"
Serial.println ("GSM MODULE BEGIN"); //To print on serial monitor
delay(1000);
}
void loop()
{
digitalRead(2);
bool SMSSent = false;
if (SMSSent = true)
{while (2 == HIGH)
Serial.println("SMS SENT SUCCESSFUL");
delay(50);
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
delay(100);
}
else {
sendSMS();
delay(1000);
digitalWrite(13, HIGH);
delay(800);
digitalWrite(13, LOW);
delay(800);
}
}
void sendSMS()
{
mySerial.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
delay(1200);
mySerial.println("AT+CMGS=\"+9000000000\"\r");
delay(1200);
mySerial.println("POWER DOWN");
delay(200);
mySerial.println((char)26);
delay(1200);
}
#include <SoftwareSerial.h>
SoftwareSerial mySerial(9, 10);
char msg;
void setup()
{
pinMode(2, INPUT_PULLUP);
pinMode(13, OUTPUT);
mySerial.begin (9600); //Setting the baud rate of GSM Module
delay(500);
Serial.begin (9600); //Setting the baud rate of Serial Monitor (Arduino)
delay(500); // Delay to prevent this "************GSM SIM900A BEGIN"
Serial.println ("GSM MODULE BEGIN"); //To print on serial monitor
delay(1000);
}
void loop()
{
int SMSSent;
int pushButton = digitalRead(2);
Serial.println(pushButton);
delay(1000);
if (pushButton == HIGH)
{
sendSMS();
}
if (SMSSent == 0)
{
sendSMS();
SMSSent = 1;
Serial.println("SMS SENT SUCCESSFUL");
digitalWrite(13, HIGH);
delay(200);
digitalWrite(13, LOW);
delay(200);
} else {
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(500); }
}
void sendSMS()
{
mySerial.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
delay(1000);
mySerial.println("AT+CMGS=\"+90000000000\"\r");
delay(1000);
mySerial.println("POWER DOWN");
delay(100);
mySerial.println((char)26);
delay(1000);
}
use == is for comparison not = which is assignment but since you initialized the variable to false just before - the compiler would Throw that code away as the test will never be true
It does not matter what you are trying to do, to do it once is always the same and requires an understanding of loop. Loop is the most important concept, try to think in a looping manner when you code.
In loop “do x” means to “do x” again and again and again. The logic is simple if you want to do it only once. You just need to have conditional statements that remember it has been done. So in pseudocode:
If stateX is 0 (== comparator here)
Do x and Change stateX to 1
Now loop checks if stateX is 0 (which will be true the first time as, when you declare it globally you initialise it to 0). Because it is true the if statement code is run. The code changes it to 1 so the if statement will no longer be true the second time through loop.
Problems you can face;
If you declare stateX as 0 in loop then every time through loop it will reset to 0 and the code in the if statement will always run.
If you use the = instead of the == you will be changing rather than comparing the variable.
You declare a variable in the wrong scope {} and so it is not visible to the block of code you want to use it in (see global and local variables and scope)
Try always to run through your code the way the microcontroller does, so in loop you need to check through at least twice to see what you are doing.
Never code 2 things at once eg buttons and your sms once only code. Code each individually in its own sketch and get it working how you want before combining. That way you know where bugs are and things are less complex. Study the important concepts of arduino:
Loop
Millis timing
State machines
Edge detection
Functions
Arrays
I was trying to say if the SMS has already been sent do not sent anymore sms
Here is the latest version. bear in mind you'll be confused here coz I am trying all sort of things just to make it right. you can ignore all the garbage and related to only things that matters.
#include <SoftwareSerial.h>
SoftwareSerial mySerial(9, 10);
char msg;
int SMSSent;
void setup()
{
pinMode(2, INPUT_PULLUP);
pinMode(13, OUTPUT);
mySerial.begin (9600); //Setting the baud rate of GSM Module
delay(500);
Serial.begin (9600); //Setting the baud rate of Serial Monitor (Arduino)
delay(500); // Delay to prevent this "************GSM SIM900A BEGIN"
Serial.println ("GSM MODULE BEGIN"); //To print on serial monitor
delay(1000);
}
void loop()
{ SMSSent == 0;
pinMode(2, INPUT_PULLUP);
int pushButton = digitalRead(2);
Serial.print(pushButton);
delay(1000);
if (pushButton == HIGH)
{
sendSMS();
SMSSent = !SMSSent;
SMSSent = 1;
Serial.println("SMS SENT SUCCESSFUL");
delay(1000);
digitalWrite(13, HIGH); /* LED blink */
delay(200);
digitalWrite(13, LOW);
delay(200);
}
}
void sendSMS()
{
mySerial.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
delay(1000);
mySerial.println("AT+CMGS=\"+971585997391\"\r");
delay(1000);
mySerial.println("sim900a sms");
delay(100);
mySerial.println((char)26);
delay(1000);
}
#include <SoftwareSerial.h>
SoftwareSerial mySerial(9, 10);
char msg;
bool SmsAlreadySent = false;
const byte pushButtonPin = 2;
const byte ledPin = 13;
void sendSMS() {
mySerial.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
delay(1000);
mySerial.println("AT+CMGS=\"+971585997391\"\r");
delay(1000);
mySerial.println("sim900a sms");
delay(100);
mySerial.println((char)26);
delay(1000);
SmsAlreadySent = true;
}
void setup() {
pinMode(pushButtonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
Serial.begin (9600); //Setting the baud rate of Serial Monitor (Arduino)
mySerial.begin (9600); //Setting the baud rate of GSM Module
Serial.println ("GSM MODULE BEGIN"); //To print on serial monitor
}
void loop() {
int pushButtonStatus = digitalRead(pushButtonPin);
Serial.print(pushButtonStatus);
if ( not SmsAlreadySent && (pushButtonStatus == LOW)) { // if we have not sent the SMS and the button is being pushed (LOW means pushed as the pin is set as INPUT_PULLUP)
sendSMS();
Serial.println("SMS SENT SUCCESSFULLY");
digitalWrite(ledPin, HIGH); /* LED blink */
delay(200);
digitalWrite(ledPin, LOW);
delay(200);
}
}
you set your bool variable to true when the SMS is sent and You never reset it (it will work then only once until you reboot the arduino)
void loop() {
int pushButtonStatus = digitalRead(pushButtonPin);
delay(1000);
Serial.print(pushButtonStatus);
delay(1000);
if ( not SmsAlreadySent && (pushButtonStatus == LOW)) { // if we have not sent the SMS and the button has been pushed
sendSMS();