Sim800 push button to send messsage

Hello, my first post.

I am simply (in theory) wanting to send one of three pre-selected messages when one of three buttons are pressed. (3 buttons to send 3 different messages and one LED to alert the message has been sent) See the attached code to look at what I have done so far…

my issue is using a push button (momentary) to add value to my three (defined at top);

int button1press
int button2press
int button3press

i know I need to do something like:

if b1pin = high
button1press ++
else

if b2pin = high
button2press ++
else

if b3pin = high
button3press ++

or something like that… but I need help incorporating that into my code, I would really appreciate any help you could give. I have done most of the hard work I think it’s just incorporating the above to trigger the code I already have attached.

Thanks in advance. :slight_smile:

SMS_button_sender.ino (1.93 KB)

Hello, my first post.

I am simply (in theory) wanting to send one of three pre-selected messages when one of three buttons are pressed. (3 buttons to send 3 different messages and one LED to alert the message has been sent) See the attached code to look at what I have done so far…

my issue is using a push button (momentary) to add value to my three (defined at top);

int button1press
int button2press
int button3press

i know I need to do something like:

if b1pin = high
button1press ++
else

if b2pin = high
button2press ++
else

if b3pin = high
button3press ++

or something like that… but I need help incorporating that into my code, I would really appreciate any help you could give. I have done most of the hard work I think it’s just incorporating the above to trigger the code I already have attached.

Thanks in advance. :slight_smile:

#include <GSM.h>
 char remoteNumber[20]= "fjrglr"; //my arduino's phone number
#define PINNUMBER ""

int button1press = 0;   //holding values for trigering abtton press (need to add to these values when button press)
int button2press = 0;
int button3press = 0;

int ledPin = 13;  //LED will light for a few seconds when sms is sent.

int b1Pin = 5;  //button input pins
int b2Pin = 6;
int b3Pin = 7;



// initialize the library instance
GSM gsmAccess; 
GSM_SMS sms;

void setup(){

  pinMode(ledPin, OUTPUT);
  pinMode(b1Pin, INPUT); 
  pinMode(b2Pin, INPUT); 
  pinMode(b3Pin, INPUT); 
  
  // initialize serial communications
  Serial.begin(9600);
  

  Serial.println("SMS Messages Sender");

  // connection state
  boolean notConnected = true;

  // Start GSM shield
  // If your SIM has PIN, pass it as a parameter of begin() in quotes
  while(notConnected)
  {
    if(gsmAccess.begin(PINNUMBER)==GSM_READY)
      notConnected = false;
    else
    {
      Serial.println("Not connected");
      delay(1000);
    }
  }

  Serial.println("GSM initialized");
    sms.beginSMS(remoteNumber);
  sms.print("Test Message - GSM Connection OK... Transmissions have been requested to this number.");
  sms.endSMS(); 
}




void loop(){
  
  if(button1press > 0){
    sms.beginSMS(remoteNumber);
    sms.print("Button 1 has been Pressed");
    sms.endSMS(); 
    button1press = 0;
    digitalWrite(13, HIGH);   
    delay(3000);              
    digitalWrite(13, LOW);         
  }

  if(button2press > 0){
    sms.beginSMS(remoteNumber);
    sms.print("Button 2 has been Pressed");
    sms.endSMS(); 
    button2press = 0;
    digitalWrite(13, HIGH);   
    delay(3000);              
    digitalWrite(13, LOW);   
  }

  if(button3press > 0){
    sms.beginSMS(remoteNumber);
    sms.print("Button 3 has been Pressed");
    sms.endSMS(); 
    button3press = 0;
    digitalWrite(13, HIGH);   
    delay(3000);              
    digitalWrite(13, LOW);   
 
  } 
  }

From the code, I cant see any code that read the button Press.

You should do some thing like.

void loop(){
  button1Press=digitalRead(b1Pin);
  button2Press = digitalRead(b2Pin);
  button3Press = digitalRead(b3Pin);

  if(button1Press==HIGH){
    //send sms 'button1 pressed'
    button1Press=LOW;
  }
  else if(button2Press==HIGH){
    //send sms 'button2 pressed'
    button2Press=LOW;
  }
}