SMS error

I need to be able to call the function Sensor when Button is on logic level 1 but I keep getting the same error:PAP_Vers_o_SMS_Sensor_com_Vari_veis.ino:103:4: error: expected initializer before 'while'

Even if I add that initializer the code will ask the cell phone number and send the result from sensor by SMS from GSM Shield ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
 SMS sender

*/

// Include the GSM library
#include <GSM.h>

#define PINNUMBER ""

// initialize the library instance
GSM gsmAccess;
GSM_SMS sms;
int ledsens = 7 ;
int v = 0; 
int botao = 5;
boolean turnsRunnig = false; // 

void setup()
{
  // initialize serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
   pinMode(botao, INPUT); 
  v = digitalRead(botao); 

  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");
}

void loop()
{
 Serial.print("Enter a mobile number: ");
  char remoteNum[20];  // telephone number to send sms
  readSerial(remoteNum);
  Serial.println(remoteNum);

  // sms text
  Serial.print("Now, enter SMS content: ");
  char txtMsg[200];
  readSerial(txtMsg);
  Serial.println("SENDING");
  Serial.println();
  Serial.println("Message:");
  Serial.println(txtMsg);

  // send the message
  sms.beginSMS(remoteNum);
  sms.print(txtMsg);
  sms.endSMS();
  Serial.println("\nCOMPLETE!\n");
}

/*
  Read input serial
 */
int readSerial(char result[])
{
  int i = 0;
  while (1)
  {
    while (Serial.available() > 0)
    {
      char inChar = Serial.read();
      if (inChar == '\n')
      {
        result[i] = '\0';
        Serial.flush();
        return 0;
      }
      if (inChar != '\r')
      {
        result[i] = inChar;
        i++;
      }
    }
  }
}

void sensor()
   
   while( v > 0 ) 
   {
    { Serial.begin(9600);
     const int pingPin = 7;

   long duration, inches, cm;
   pinMode(pingPin, OUTPUT);
   digitalWrite(pingPin, LOW);
   delayMicroseconds(2);
   digitalWrite(pingPin, HIGH);
   delayMicroseconds(5);
   digitalWrite(pingPin, LOW);


   pinMode(pingPin, INPUT);
   duration = pulseIn(pingPin, HIGH);
   inches = microsecondsToInches(duration);
   cm = microsecondsToCentimeters(duration);

   Serial.print(inches);
   Serial.print("in, ");
   Serial.print(cm);
   Serial.print("cm");
   Serial.println();

  delay(100);
    
  }

 {long microsecondsToInches(long microseconds)

 return microseconds / 74 / 2;  
}


 {long microsecondsToCentimeters(long microseconds)

  return microseconds / 29 / 2;
}
  
}
From line 99 on: There seems to be a lot of missplaced braces. I'd guess the the brace from line 103 is supposed to be on line 100.

What are the lines 130/136?
The lines from 130/136 come with arduino example of Ping sensor so that's why i keep them.
That's not the issue. It's about the braces. Do you spot the mismatches?

For instance the very last closing }. Where is the opening {?
Look at those lines 130-136 again. What do they do? Look at how they are called on line 117 and 118. What are they supposed to do?

You are using readSerial(), sensor(), microsecondsToInches and microsecondsToCentimeters() before they are defined.

Here is the code from lines 99-141 indented according to the braces. This should help you see what's wrong:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
void
sensor()
    while (v > 0)
        {
            {
                Serial.begin(9600);
                const int pingPin = 7;

                long duration,
                    inches,
                    cm;
                pinMode(pingPin, OUTPUT);
                digitalWrite(pingPin, LOW);
                delayMicroseconds(2);
                digitalWrite(pingPin, HIGH);
                delayMicroseconds(5);
                digitalWrite(pingPin, LOW);


                pinMode(pingPin, INPUT);
                duration = pulseIn(pingPin, HIGH);
                inches = microsecondsToInches(duration);
                cm = microsecondsToCentimeters(duration);

                Serial.print(inches);
                Serial.print("in, ");
                Serial.print(cm);
                Serial.print("cm");
                Serial.println();

                delay(100);

            }

            {
                long microsecondsToInches(long microseconds)

                    return microseconds / 74 / 2;
            }


            {
                long microsecondsToCentimeters(long microseconds)

                    return microseconds / 29 / 2;
            }

        }

This is the example code from SensorPing that I need to use and I didn't need to define anything and I was able to read the values from sensor.
My problem is when paste this code to void sensor it says those variables aren't difined.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const int pingPin = 7;

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
}

void loop()
{

  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);


  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();

  delay(100);
}

long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{

  return microseconds / 29 / 2;
}
Well, it seems that you do not understand the meaning of curly braces. Read this:

http://www.arduino.cc/en/Reference/Braces
After talking to my teacher he said to rewrite the code and no longer have that error.

Now it says I didn't define sensor in Void loop but that part of code was told by my teacher that defining sensor(); and create a void sensor after void loop would enable me to call the code in Void Sensor but it get me that error.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <GSM.h>

#define PINNUMBER ""

// initialize the library instance
GSM gsmAccess;
GSM_SMS sms;
int ledsens = 7 ;
int v = 0; 
int botao = 5;


void setup()
{
  // initialize serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
   pinMode(botao, INPUT); 
  v = digitalRead(botao); 

  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");
}

void loop()
{
 sensor(); 
 
 Serial.print("Enter a mobile number: ");
  char remoteNum[20];  // telephone number to send sms
  readSerial(remoteNum);
  Serial.println(remoteNum);

  // sms text
  Serial.print("Now, enter SMS content: ");
  char txtMsg[200];
  readSerial(txtMsg);
  Serial.println("SENDING");
  Serial.println();
  Serial.println("Message:");
  Serial.println(txtMsg);
//  txtMsg = "Esta Cheio"
  // send the message
  sms.beginSMS(remoteNum);
  sms.print(txtMsg);
  sms.endSMS();
  Serial.println("\nCOMPLETE!\n");
}

/*
  Read input serial
 */
int readSerial(char result[])
{
  int i = 0;
  while (1)
  {
    while (Serial.available() > 0)
    {
      char inChar = Serial.read();
      if (inChar == '\n')
      {
        result[i] = '\0';
        Serial.flush();
        return 0;
      }
      if (inChar != '\r')
      {
        result[i] = inChar;
        i++;
      }
    }
  }
}


void sensor()
A function must be known before you call it. Either you write the whole function before you use it or a prototype.
The prototype is the head of the function + ;
In your case:
1
2
3
4
5
void sensor(); // This is the prototype

void loop()
{
 sensor();  // This is the actual call of the function 


Read more about functions:

http://arduino.cc/en/Reference/FunctionDeclaration
Topic archived. No new replies allowed.