So i finally had time to look at the project again.
Thank you all for your suggestion.
I made two versions of the code.
The first version uses the state change detection.
But i am having problems getting more input til send different sms on activation
This is the first code:
#include <SoftwareSerial.h>
SoftwareSerial SIM900(7, 8);
// this constant won't change:
const int buttonPin = 3; // the pin that the pushbutton is attached to
const int ledPin = 13; // the pin that the LED is attached to
// Variables will change:
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup() {
SIM900.begin(19200);
//SIM900power_on();
pinMode(buttonPin, INPUT);
}
void SIM900power()
{
digitalWrite(9, HIGH);
delay(1000);
digitalWrite(9, LOW);
delay(5000);
}
void sendSMS()
{
SIM900.print("AT+CMGF=1\r"); // AT command to send SMS message
delay(100);
SIM900.println("AT + CMGS = \"xxxxxxxxxx"\""); // recipient's mobile number, in international format
delay(100);
SIM900.println("sms test"); // message to send
delay(100);
SIM900.println((char)26); // End AT command with a ^Z, ASCII code 26
delay(100);
SIM900.println();
delay(10000); // give module time to send SMS
}
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
SIM900power(); // turn on GSM shield
delay(15000);
sendSMS(); // send sms
delay(50000);
SIM900power(); // turn on GSM shield
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
}
This is the second code, using a simple debounce.
This code works with multi input, but it is a bit quick and dirty.
So any ideas to clean it up, would greatly be appreciated.
#include <Arduino.h>
#include <SoftwareSerial.h>
SoftwareSerial SIM900(7, 8);
// this constant won't change:
const int Pin3 = 3;
const int Pin4 = 4;
// Variables will change:
int pin3State; // the current reading from the input pin
int lastpin3State = HIGH; // the previous reading from the input pin
int pin4State; // the current reading from the input pin
int lastpin4State = HIGH; // the previous reading from the input pin
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
SIM900.begin(19200);
pinMode(Pin3, INPUT);
pinMode(Pin4, INPUT);
}
void sendSMS_pin3()
{
SIM900.print("AT+CMGF=1\r"); // AT command to send SMS message
delay(100);
SIM900.println("AT + CMGS = \"xxxxxxxxxxx\""); // recipient's mobile number, in international format
delay(100);
SIM900.println("High Water Alarm 3"); // message to send
delay(100);
SIM900.println((char)26); // End AT command with a ^Z, ASCII code 26
delay(100);
SIM900.println();
delay(10000); // give module time to send SMS
}
void sendSMS_pin4()
{
SIM900.print("AT+CMGF=1\r"); // AT command to send SMS message
delay(100);
SIM900.println("AT + CMGS = \"xxxxxxxxxx\""); // recipient's mobile number, in international format
delay(100);
SIM900.println("High Water Alarm 4"); // message to send
delay(100);
SIM900.println((char)26); // End AT command with a ^Z, ASCII code 26
delay(100);
SIM900.println();
delay(10000); // give module time to send SMS
}
void loop() {
{
int reading = digitalRead(Pin3);
// If the switch changed, due to noise or pressing:
if (reading != lastpin3State) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
// if the pin3 state has changed:
if (reading != pin3State) {
pin3State = reading;
if (pin3State == HIGH) {
digitalWrite(9, HIGH);
delay(1000);
digitalWrite(9, LOW);
delay(5000); // turn on GSM shield
delay(25000);
sendSMS_pin3(); // send sms
digitalWrite(9, HIGH);
delay(1000);
digitalWrite(9, LOW);
delay(5000);
}
}
}
lastpin3State = reading;
}
{
int reading = digitalRead(Pin4);
// If the switch changed, due to noise or pressing:
if (reading != lastpin4State) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
// if the pin3 state has changed:
if (reading != pin4State) {
pin4State = reading;
if (pin4State == HIGH) {
digitalWrite(9, HIGH);
delay(1000);
digitalWrite(9, LOW);
delay(5000); // turn on GSM shield
delay(25000);
sendSMS_pin4(); // send sms
digitalWrite(9, HIGH);
delay(1000);
digitalWrite(9, LOW);
delay(5000);
}
}
}
lastpin4State = reading;
}
}
About the AT+CREG?
It looks like a really good idea, but it looks a bit complex for my abilities.
If you have a good idea to implement it, i would like to hear it.
thank you all for any help