using same variable in multiple void loops.

Hey guys, When I declare a variable in one void loop, I cant use that same variable in another void loop. For my code below.

void keypadentry()
{

int exitcodeaa;
char key = keypad.getKey();

if (key)
{
if (key == ‘*’)
{
memset(entryStr, 0, sizeof(entryStr));
i=0;
key=0;
Serial.println("");
Serial.println(“Canceled”);
}

else if (key != ‘#’)
{

entryStr*= key;*

  • i++;*

  • Serial.print(key);*

  • }*

  • else*

  • {*

  • Serial.println("");*

  • i=0;*

  • key=0;*

  • String fileName = entryStr;*

  • memset(entryStr, 0, sizeof(entryStr));*

  • Serial.println(fileName);*

  • sim.println(“AT+CMGF=1”); *

  • delay(1000);*

  • sim.println(“AT+CMGS=”" + fileName + “”\r");*

  • delay(1000);*

  • String SMS = “Welcome to IPMS. Park in Area A. Your Exit Code Will Arrive Shortly.”;*

  • sim.println(SMS);*

  • delay(100);*

  • sim.println((char)26);// ASCII code of CTRL+Z*

  • delay(4000);*

  • sim.println(“AT+CMGF=1”); *

  • delay(1000);*

  • sim.println(“AT+CMGS=”" + fileName + “”\r");*

  • delay(1000);*

  • randomSeed(analogRead(0));*

  • randNumber = random(6100,8100);*

  • int SMSA = randNumber;*

  • sim.println(SMSA);*

  • delay(100);*

  • sim.println((char)26);*

  • delay(1000);*

  • randNumber = exitcodeaa; <<<<<<<<<<<<<<<<<<<<<<HERE*

  • lcd.clear();*

  • parkAreaA();*

  • }*
    }
    }
    you don’t need to understand everything I did but the point is to show that the random number generated as randNumber is also equal to exitcodeaa as noted (HERE) above.
    However now that randNumber is equal to exitcodeeaa I proceed with another void code…But the second void doesnt allow me to use the exitcodeaa from the first void. Unless I declare a new one.
    void exitkeypadentry()
    {

  • int exitcodeaa; <<<<<<<< I have to declare a new one, but I want to use the same one with registerd value from the first loop.*

  • lcd.setCursor(0,0);*

  • lcd.print(“Enter Exit Code”);*

  • char key = keypad.getKey();*

if (key)
{
_ if (key == ‘*’)_

  • {*

  • memset(entryStr, 0, sizeof(entryStr));*

  • i=0;*

  • key=0;*

  • Serial.println("");*

  • Serial.println(“Canceled”);*

  • }*

  • else if (key != ‘#’)*

  • {*

_ entryStr*= key;_
_
i++;_
_
Serial.print(key);_
_
}*_

* else*
* {*
* Serial.println("");*
* i=0;*
* key=0;*

* String exitcode = entryStr;*
* memset(entryStr, 0, sizeof(entryStr));*

* Serial.println(“Checking Code…”);*
* Serial.println(exitcode); <<<this is the exit code entered on the keypad and saved as exitcode*

* if(exitcode.toInt() == exitcodeaa) <<then if the code entered is the same as exitcodeaa = success.*
* {*
* lcd.clear();*
* lcd.setCursor(0,0);*
* myservox.write(130);*
* lcd.print(“GoodBye!”);*
* delay(3000);*
* lcd.clear();*
* myservox.write(30);*
* delay(20);*
* myservox.detach(); *

* }*
* else*
* {*
* lcd.clear();*
* lcd.setCursor(0,0);*
* lcd.print(“Incorrect Code”);*
* delay(3000);*
* lcd.clear();*
* }*

* }*
}
}

30 posts and you still don’t know how to post code? Read the how to, #7.

And your question answered:

https://www.arduino.cc/en/pmwiki.php?n=Reference/Scope

PS: Your post should be in the programming sub-section, not general electronics.

Please edit your post to add code tags.

You can have only one function named "loop". The keyword "void" in front of a function name indicates that the function does not return a value.

Variable scope is the subject you might want to come up to speed on. A link to help you along: https://www.w3adda.com/cplusplus-tutorial/cpp-variable-scope.

void keypadentry() is a [u]function[/u], not a loop.

A function performs some particular task and it runs once when “called”, then program execution returns to the main program (or wherever it was). A non-void function will also return a value. It is common to use a function over-and-over sometimes calling it from different places in the program and/or sometimes calling it from inside a loop.

A loop runs over-and over, usually until some condition is reached. In addition to the main loop() we can have:

[u]for() loops[/u]
[u]while() loops[/u]
[u]do-while() loops[/u]

You can have loops inside loops (nested loops), loops inside functions, functions inside loops, and functions inside functions, etc.

A loop that you can’t get out of is called an infinite loop and it’s usually a bug when you are “stuck in a loop”. The Arduino main loop() is an exception. It’s not a bug and you don’t normally break out of it.

The two most important concepts in programming are loops and conditional execution (if-statements, etc.).