I have an Uno with a SIM900 GSM modem piggy backed and have been playing around with some Sketch code which allows me to send the SMS #a0b1c1d0 etc, which works ok. So I decided to try an incorporate an additional function which will allow it to re-act / switch 2 outputs based on an incoming call / RING. But unfortunately it will not do this.
I can imagine that this will be easy to solve but as I say, I'm just learning.
Here is copy of Sketch code:
#include <SoftwareSerial.h>
char inchar; // Will hold the incoming character from the GSM shield
char inring;
SoftwareSerial SIM900(7, 8);
int led1 = 10; // gives the pins a name
int led2 = 11;
int led3 = 12;
int led4 = 13;
int numring=0; // variable to store number of rings
int comring=3; // variable to keep value for number of desired rings before something happens
int onoff=0; // 0 = off, 1 = on // variable to keep value of led off/on state - but is initially set as 0
void setup()
{
Serial.begin(19200);
// set up the digital pins to control
pinMode(led1, OUTPUT); // sets these as outputs
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(2, OUTPUT);
pinMode(4, OUTPUT);
digitalWrite(led1, LOW); // set the powerup state of these variables
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(2, HIGH);
digitalWrite(4, LOW);
// wake up the GSM shield
SIM900power();
SIM900.begin(19200);
SIM900.print("AT+CLIP=1\r"); // turn on caller ID notification
delay(100);
}
void SIM900power()
// software equivalent of pressing the GSM shield "power" button
{
digitalWrite(9, HIGH);
delay(1000);
digitalWrite(9, LOW);
delay(7000);
}
void doSomething()
{
if (onoff==0)
{
onoff=1;
digitalWrite(2, HIGH);
digitalWrite(4, LOW);
Serial.println("D12 high D13 low");
}
else
if (onoff==1)
{
onoff=0;
digitalWrite(2, LOW);
digitalWrite(4, HIGH);
Serial.println("D12 low D13 high");
loop();
}
}
void loop()
//If a character comes in from the cellular module...
{ if(SIM900.available() >0)
{
inchar=SIM900.read();
if (inchar=='#')
{
delay(10);
inchar=SIM900.read();
if (inchar=='a')
{
delay(10);
inchar=SIM900.read();
if (inchar=='0')
{
digitalWrite(led1, LOW);
}
else if (inchar=='1')
{
digitalWrite(led1, HIGH);
}
delay(10);
inchar=SIM900.read();
if (inchar=='b')
{
inchar=SIM900.read();
if (inchar=='0')
{
digitalWrite(led2, LOW);
}
else if (inchar=='1')
{
digitalWrite(led2, HIGH);
}
delay(10);
inchar=SIM900.read();
if (inchar=='c')
{
inchar=SIM900.read();
if (inchar=='0')
{
digitalWrite(led3, LOW);
}
else if (inchar=='1')
{
digitalWrite(led3, HIGH);
}
delay(10);
inchar=SIM900.read();
if (inchar=='d')
{
delay(10);
inchar=SIM900.read();
if (inchar=='0')
{
digitalWrite(led4, LOW);
}
else if (inchar=='1')
{
digitalWrite(led4, HIGH);
}
delay(10);
SIM900.println("AT+CMGD=1,4"); // delete all SMS
delay(1000);
(SIM900.available() >0);
inring=SIM900.read();
if (inring=='R')
{
delay(10);
inring=SIM900.read();
if (inring=='I')
{
delay(10);
inring=SIM900.read();
if (inring=='N')
{
delay(10);
inring=SIM900.read();
if (inring=='G')
{
delay(10);
// So the phone (our GSM shield) has 'rung' once, i.e. if it were a real phone
// it would have sounded 'ring-ring' or 'blurrrrr' or whatever one cycle of your ring tone is
numring++; // add a single ring value to the numring variable
Serial.println("ring!");
if (numring==comring) // if both numring variable are the same as comring value then do the below
{
numring=0; // reset ring counter
delay (500);
doSomething();}
}
}
}
}
}
}
}
}
}
}
}