-7
#include <SoftwareSerial.h>
SoftwareSerial SIM900(7, 8);
#include <SHT1x.h>
// Specify data and clock connections and instantiate SHT1x object
#define dataPin  10
#define clockPin 11
SHT1x sht1x(dataPin, clockPin);

const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
const int ledPin = 13;                 // LED connected to digital pin 13
const int buzzerpin= 9;          //buzzer connection pin 9 hehehe
int sensorValue = 0;        // value read from the sensor

void setup() {
   Serial.begin(9600); // Open serial connection to report values to host
   Serial.println("Starting up");
   pinMode(ledPin, OUTPUT);      // sets the digital pin as output
   pinMode(buzzerpin, OUTPUT);    // sets the digital pin as output

}
void loop()
{
  // print the results to the serial monitor:
  float temp_c;
  temp_c = sht1x.readTemperatureC();
  Serial.print("Temperature: ");
  Serial.print(temp_c, DEC);
  Serial.print("C / ");   
  sensorValue = analogRead(analogInPin); 
  Serial.print("Smoke sensor = " );                       
  Serial.println(sensorValue);     
 // read the analog in value:

  // determine alarm status
  if (sensorValue >= 350)
  {
    if (temp_c >= 26)
    {
     digitalWrite(ledPin, HIGH);   // sets the LED on
    digitalWrite(buzzerpin, HIGH);  // sets buzzer alarming
      goto x;
    }
  }
  else
  {
  digitalWrite(ledPin, LOW);    // sets the LED off
  digitalWrite(buzzerpin, LOW); // sets buzzer off
  }
  // wait 10 milliseconds before the next loop
  // for the analog-to-digital converter to settle
  // after the last reading:
  delay(2000);  

x:
void sendSMS() 
{

SIM900.print("AT+CMGF=1\r"); // AT command to SMS mode to text
delay(100);
SIM900.println("AT + CMGS = \"+639183524970\""); // Change this to your desired recipient mobile number. (international format)
delay(100);
SIM900.println("Hello! you did it my friend. Congrats!"); // 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
}
void loop()
{
sendSMS(); // Call this function to start sending the message
do {} while (1);
}


}
  • 5
    Congratulations, that's code (which you didn't even bother formatting properly). Did you want to share the error you're getting? – Ignacio Vazquez-Abrams Feb 23 '15 at 8:31
  • We need more to go on. You're basically asking someone to compile this for you to see the error messages. Why haven't you provided those? What have you tried so far to fix them? – JRobert Feb 25 '15 at 21:08
3

Function loop() has two definitions, one nested inside the other.

Function sendSMS() is defined inside the definition of loop().

Each function can only have one definition.

Functions cannot be defined inside other functions.

Try to avoid using goto, it's generally considered bad practice, and can make the code more difficult to understand.

Try to use indentation (spacing) to make the logical structures easier to check.

#include <SoftwareSerial.h>
SoftwareSerial SIM900(7, 8);
#include <SHT1x.h>
// Specify data and clock connections and instantiate SHT1x object
#define dataPin  10
#define clockPin 11
SHT1x sht1x(dataPin, clockPin);

const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
const int ledPin = 13;                 // LED connected to digital pin 13
const int buzzerpin= 9;          //buzzer connection pin 9 hehehe
int sensorValue = 0;        // value read from the sensor

void setup() {
    Serial.begin(9600); // Open serial connection to report values to host
    Serial.println("Starting up");
    pinMode(ledPin, OUTPUT);      // sets the digital pin as output
    pinMode(buzzerpin, OUTPUT);    // sets the digital pin as output
}
void loop()
{
    // print the results to the serial monitor:
    float temp_c;
    temp_c = sht1x.readTemperatureC();
    Serial.print("Temperature: ");
    Serial.print(temp_c, DEC);
    Serial.print("C / ");   
    sensorValue = analogRead(analogInPin); 
    Serial.print("Smoke sensor = " );                       
    Serial.println(sensorValue);     
    // read the analog in value:

    // determine alarm status
    if (sensorValue >= 350)
    {
        if (temp_c >= 26)
        {
            digitalWrite(ledPin, HIGH);   // sets the LED on
            digitalWrite(buzzerpin, HIGH);  // sets buzzer alarming
            // [MarkU] removed the goto x / label x / nested definition of loop()
            // goto x;

            // [MarkU] was this intended to call sendSMS() ?
            // sendSMS() nested function definition was found after "x:"
            sendSMS(); // Call this function to start sending the message
        }
    }
    else
    {
        digitalWrite(ledPin, LOW);    // sets the LED off
        digitalWrite(buzzerpin, LOW); // sets buzzer off
    }
    // wait 10 milliseconds before the next loop
    // for the analog-to-digital converter to settle
    // after the last reading:
    delay(2000);  

    // [MarkU] removed the goto x / label x / nested definition of loop()
    // x:

    // [MarkU] moved the function definition sendSMS() outside of function loop().

    // [MarkU] this is the second definition of void loop() -- not legal
    // void loop()
    // {
    //     sendSMS(); // Call this function to start sending the message
    //     
    //     // [MarkU] was this intended to halt operation?
    //     do {} while (1);
    //     
    // } // end of definition void loop() #2
} // [MarkU] end of definition void loop()

// [MarkU] moved the function definition sendSMS() outside of function loop().
// [MarkU] it's not valid to define a function inside of another function.
void sendSMS() 
{
    SIM900.print("AT+CMGF=1\r"); // AT command to SMS mode to text
    delay(100);
    SIM900.println("AT + CMGS = \"+639183524970\""); // Change this to your desired recipient mobile number. (international format)
    delay(100);
    SIM900.println("Hello! you did it my friend. Congrats!"); // 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
}
| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.