1 Variable; 2 Functions

Hey All, I'm having some trouble figuring out how to get a variable in one function (ex. Zone1 in Loop) transferred to another function (Zone1 to postData). So, if Zone1 gets triggered then it goes to void postData() to be posted as Zone1, but if Zone2 gets triggered, then Zone2. The only other way I can see how to do this is to put the postData code under each Zone, but this seems like too much code for this. Thanks for any help.

void loop();
{
 Zone1State = digitalRead(Zone1);
 if (Zone1State == HIGH) {
   digitalWrite(ledPin, HIGH);
   Serial.println("Zone 1 Alarm");
   beep(speaker,4186,500);
   postData();
   delay(1000); 
}


 Zone2State = digitalRead(Zone2);
 if (Zone2State == HIGH) {
   digitalWrite(ledPin, HIGH);
   Serial.println("Zone 2 Alarm");
   beep(speaker,4186,500);
   beep(speaker,4186,500);
   postData();
   delay(1000);
}

//Continues until Zone9
}

void postData()
{
   if (client.connect(server, 80)) {
    Serial.println("Connected!");   
    client.print("GET http://someurl.com/data_send.php?Zone=");
    client.print("INSERT WHICH ZONE ALARM");
    client.print("&");
    client.print("status=");
    client.print("On");
    client.println(" HTTP/1.1");
     client.print("Host: www.someurl.com");
    client.println("Connection: close");
    client.println();
 }

You should be passing an argument to postData, just like you do for client.print(), digitalWrite(), etc.

jeeep: Hey All, I'm having some trouble figuring out how to get a variable in one function (ex. Zone1 in Loop) transferred to another function (Zone1 to postData). So, if Zone1 gets triggered then it goes to void postData() to be posted as Zone1, but if Zone2 gets triggered, then Zone2. The only other way I can see how to do this is to put the postData code under each Zone, but this seems like too much code for this. Thanks for any help.

all you do is put the variable outside the functions. Here's an example:

First, one that WON'T work:

void function1 (void) {
    int flag = 3;
}

void function2 (void) {
    if (flag == 3) {
        Serial.print ("I see the three!\n");
    } else {
        Serial.print ("Nope, I don't see three\n");
    }
}

Compiling this results in an error: ``` error: ‘flag' was not declared in this scope ```

Now, put the flag OUTSIDE the functions:

int flag;
void function1 (void) {
    flag = 3;
}

void function2 (void) {
    if (flag == 3) {
        Serial.print ("I see the three!\n");
    } else {
        Serial.print ("Nope, I don't see three\n");
    }
}

This compiles and running function2 results in the Arduino printing "I see three!".

Hope this helps.

Krupski: all you do is put the variable outside the functions. Here's an example:

First, one that WON'T work:

void function1 (void) {
    int flag = 3;
}

void function2 (void) {     if (flag == 3) {         Serial.print ("I see the three!\n");     } else {         Serial.print ("Nope, I don't see three\n");     } }




Compiling this results in an error: 
**```**
**error: ‘flag' was not declared in this scope**
**```**



Now, put the flag OUTSIDE the functions:



int flag; void function1 (void) {     flag = 3; }

void function2 (void) {     if (flag == 3) {         Serial.print ("I see the three!\n");     } else {         Serial.print ("Nope, I don't see three\n");     } }




This compiles and running function2 results in the Arduino printing "I see three!".

Hope this helps.

Very poor design.. Global scope variables changed via multiple functions lead to side effects in code 100% of time side efgects mean random results on variable.. as there is no syncronisation regarding which change was last.

You neecd to think about the design, loot at C+ code ideas and make the variable a property of a class, then it will only be changed by the property write, it can also then be accessed from anywhere with a property read..

Global scope variables changed via multiple functions lead to side effects in code 100% of time side efgects mean random results on variable.

Sorry but I don't agree with that. None of the effects on global variables are random. Global variables have their place if used properly, just like any other type of variable.

If you don't like people using global variables what do you think about people who declare two or more local variables with the same name then attempt to use the value of the one created in function A in function B ?

tgsuperspec: Very poor design.. Global scope variables changed via multiple functions lead to side effects in code 100% of time side efgects mean random results on variable.. as there is no syncronisation regarding which change was last.

You neecd to think about the design, loot at C+ code ideas and make the variable a property of a class, then it will only be changed by the property write, it can also then be accessed from anywhere with a property read..

How very odd. With such small code I use globals a lot and have a whole collection of bug free code.

Perhaps 100% side effects is just a TAD exaggerated?

Also you might want to take a closer look at AVR's to see that they don't have much room to waste.