My code doesn't work. Need help!

I've modified my code to make it read an analogInput then send a message. I've it debugged and no errors at all. As I try it several times and do many modification as I can it still doesn't wanna work. I wonder if anyone can help me figure it out.

When the module senses a signal, it will turn the Arduino GSM Shield on within a certain time that is enough to send a message. Then it will automatically turn off.

The problem here is when I plug everything in place, nothing happens even there is a signal at the input pin 0

Thanks

// Example 55.2

#include <SoftwareSerial.h>
SoftwareSerial SIM900(7, 8);
const int analogPin = A0;    // pin that the sensor is attached to
const int threshold = 797;   // an arbitrary threshold level that's in the range of the analog input


void setup()
{
  pinMode(9, OUTPUT);
  SIM900.begin(19200);
 //SIM900power();  
  delay(10000);  // give time to log on to network. 
}

void SIM900power()
//software equivalent of pressing the GSM shield "power" button
{
  
  int analogValue = analogRead(analogPin);
  if(analogValue >= threshold){
    sendSMS();
  }
  digitalWrite(9,LOW);
  delay(1000);
  digitalWrite(9, HIGH);
  delay(2000);
  digitalWrite(9, LOW);
  delay(3000);
}


void sendSMS()
{
  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 E.123
  delay(100);
  SIM900.println("Message to send here!");        // message to send
  delay(100);
  SIM900.println((char)26);                       // End AT command with a ^Z, ASCII code 26
  delay(100); 
  SIM900.println();
  delay(5000);                                     // give module time to send SMS
  SIM900power();                                   // turn off module
}


void loop()
{
  sendSMS();
 // SIM900power(); //power off GSM SHIELD
  do {} while (1);
}
  digitalWrite(9,LOW);
  delay(1000);
  digitalWrite(9, HIGH);
  delay(2000);
  digitalWrite(9, LOW);
  delay(3000);

Is this the portion of code that turns the GSM module on and off ?
If so then you need to realise that during the delays nothing else will happen. Should you not be turning the module on, sending the SMS then turning it off ?

That's correct. The module will turn on when it senses a signal. I use a simple if function there but it doesn't look right.

Should I change the delay time?

Tracing the natural flow of events in your sketch from the boot.
First Setup will run

void setup()
{
  pinMode(9, OUTPUT);
  SIM900.begin(19200);
 //SIM900power();  
  delay(10000);  // give time to log on to network. 
}

Then the loop will run

void loop()
{
  sendSMS();
 // SIM900power(); //power off GSM SHIELD
  do {} while (1);
}

Since the do {} while (1); just causes an effective halt. This is as far as it goes. I don't see any "sensing" going on here.

You also have an interesting function void SIM900power() but this is never called (it is remarked out in your setup function)

Hi, I try again with one piece at a time.

I did the sound detection code and it worked just fine

#include <SoftwareSerial.h>
SoftwareSerial SIM900(7, 8);

int threshold = 306;
int sensePin= A0;


void setup()
{
  Serial.begin(9600); 
  pinMode(9, OUTPUT);
  
}

void loop()
{
  int analogValue=analogRead(sensePin);
  Serial.println(sensePin);
  if(analogValue > threshold){
  digitalWrite(9, HIGH);
  delay(100);
  }
}

but when I put the sendSMS piece together, the module still turned on when it detected a sound but it didn't send any message.
Here is my full program.
#include <SoftwareSerial.h>
SoftwareSerial SIM900(7, 8);

int threshold = 306; //threshold voltage to turn on/off the GSM module
int sensePin= A0; /analoginput pin A0


void setup()
{
  Serial.begin(9600); 
  pinMode(9, OUTPUT); //power pin for the GSM module
  
}

void loop()
{
  int analogValue=analogRead(sensePin);
  Serial.println(sensePin);
  if(analogValue > threshold){                        //compare the analog value from pin A0 to the given threshold 
  digitalWrite(9, HIGH);            //power up the GSM module
  delay(100);
  }
  sendSMS();                 //after the GSM module is powered up, it will go to this sendsms section to send a message
}


void sendSMS()
{
  SIM900.print("AT+CMGF=1\r");                                                        // AT command to send SMS message
  delay(100);
  SIM900.println("AT + CMGS = \"+64212377933\"");                                     // recipient's mobile number, in international format E.123
  delay(100);
  SIM900.println("Your Alarm has gone off. You got a visitor");        // message to send
  delay(100);
  SIM900.println((char)26);                       // End AT command with a ^Z, ASCII code 26
  delay(100); 
  SIM900.println();
  delay(5000);                                     // give module time to send SMS
}

Anyway, the program stuck at the turn on/off the GSM module. When I compile the program there is no problem.

You're calling sendSMS every time you pass through loop() - that isn't right, surely?

I want to call it once. Maybe I'll put a delay in there like 30min if the sensepin is still going.

as mentioned above, the module only turns on and off at the moment and it never calls the sendsms.

Please suggest.

Thanks

I mentioned it on my very first post on this thread but you're still not calling that function SIM900power() In fact, now you seem to have even deleted it completely.

Doing a quick google I find this page.
http://www.seeedstudio.com/wiki/GPRS_Shield_V1.0

About a third of the way down the page it says

As the timing of turn on/off , a >1s pulse was need to trigger the turning, and a >3.2s delay was need to get the timing stable. Add the following code in your firmware to turn on / off the shield without pressing the button:

void powerUpOrDown()

{
  pinMode(9, OUTPUT);
  digitalWrite(9,LOW);
  delay(1000);
  digitalWrite(9,HIGH);
  delay(2000);
  digitalWrite(9,LOW);
  delay(3000);
}

So you see? Just turning pin 9 HIGH won't cut it. You need to put this function back in and CALL IT to turn the thing on.