declared variable inside void setup is forgotten in void loopCoding DebuggingSpeed Control L298 motorsPassing pointers between setup and loopServotimer2, detach “stop”Arduino code to control 4 led's from 4 buttonsAdding music to loopServo doesn't work in 'for' loop running under 'if' loopInterrupts Problem with Flow sensorWhy a servo doesn`t move to angles properlyHow do I make a Servo Stop if it Hits Resistance?

RegEx with d doesn’t work in if-else statement with [[

What's is the easiest way to purchase a stock and hold it

Should I twist DC power and ground wires from a power supply?

Hotel booking: Why is Agoda much cheaper than booking.com?

Why does the setUID bit work inconsistently?

What color to choose as "danger" if the main color of my app is red

How would fantasy dwarves exist, realistically?

Lock out of Oracle based on Windows username

Are there any symmetric cryptosystems based on computational complexity assumptions?

Prints each letter of a string in different colors. C#

When did Britain learn about the American Declaration of Independence?

Is there any deeper thematic meaning to the white horse that Arya finds in The Bells (S08E05)?

Bookshelves: the intruder

Failing students when it might cause them economic ruin

Why is choosing a suitable thermodynamic potential important?

Good examples of "two is easy, three is hard" in computational sciences

Is my company merging branches wrong?

Why use a retrograde orbit?

pwaS eht tirsf dna tasl setterl fo hace dorw

Does a windmilling propeller create more drag than a stopped propeller in an engine out scenario

Divisor Rich and Poor Numbers

How to draw pentagram-like shape in Latex?

Have GoT's showrunners reacted to the poor reception of the final season?

Taylor series leads to two different functions - why?



declared variable inside void setup is forgotten in void loop


Coding DebuggingSpeed Control L298 motorsPassing pointers between setup and loopServotimer2, detach “stop”Arduino code to control 4 led's from 4 buttonsAdding music to loopServo doesn't work in 'for' loop running under 'if' loopInterrupts Problem with Flow sensorWhy a servo doesn`t move to angles properlyHow do I make a Servo Stop if it Hits Resistance?













1















if i declare a var in void setup() and try to do something with it in void loop(), it just says that the variable is undeclared. here is the code:



#include "Servo.h"

void setup()
Servo servo1;
int x_key = A0;
servo1.attach(2);
pinMode(A0, INPUT);
pinMode(2, OUTPUT);

int x_position = 90;


void loop()
if (analogRead(A0) < 512)
x_position++;
servo1.write(x_position);











share|improve this question









New contributor



ThisIsAron is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • You need to select your code and tap the (code formatting) button. I did it for you.

    – Duncan C
    May 12 at 19:29






  • 2





    Of course it's undeclared. The variable is in setup. It's not in loop.

    – Majenko
    May 12 at 19:30






  • 1





    Because the variable is only valid, where it is declared, to use the official term: it's scope. When the program moves out of the scope of this variable, it will get thrown away and be no longer available. You can declare it in global scope, meaning outside of any function

    – chrisl
    May 12 at 19:32











  • This is more a question about C/C++, not about Arduino

    – chrisl
    May 12 at 19:33











  • @chrisl true, but the OP doesn't know enough to know where to ask the question. We all have to start somewhere. I just wish people would do some self-study before running to the internet looking for somebody else to answer their questions for them.

    – Duncan C
    May 12 at 19:35















1















if i declare a var in void setup() and try to do something with it in void loop(), it just says that the variable is undeclared. here is the code:



#include "Servo.h"

void setup()
Servo servo1;
int x_key = A0;
servo1.attach(2);
pinMode(A0, INPUT);
pinMode(2, OUTPUT);

int x_position = 90;


void loop()
if (analogRead(A0) < 512)
x_position++;
servo1.write(x_position);











share|improve this question









New contributor



ThisIsAron is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • You need to select your code and tap the (code formatting) button. I did it for you.

    – Duncan C
    May 12 at 19:29






  • 2





    Of course it's undeclared. The variable is in setup. It's not in loop.

    – Majenko
    May 12 at 19:30






  • 1





    Because the variable is only valid, where it is declared, to use the official term: it's scope. When the program moves out of the scope of this variable, it will get thrown away and be no longer available. You can declare it in global scope, meaning outside of any function

    – chrisl
    May 12 at 19:32











  • This is more a question about C/C++, not about Arduino

    – chrisl
    May 12 at 19:33











  • @chrisl true, but the OP doesn't know enough to know where to ask the question. We all have to start somewhere. I just wish people would do some self-study before running to the internet looking for somebody else to answer their questions for them.

    – Duncan C
    May 12 at 19:35













1












1








1








if i declare a var in void setup() and try to do something with it in void loop(), it just says that the variable is undeclared. here is the code:



#include "Servo.h"

void setup()
Servo servo1;
int x_key = A0;
servo1.attach(2);
pinMode(A0, INPUT);
pinMode(2, OUTPUT);

int x_position = 90;


void loop()
if (analogRead(A0) < 512)
x_position++;
servo1.write(x_position);











share|improve this question









New contributor



ThisIsAron is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











if i declare a var in void setup() and try to do something with it in void loop(), it just says that the variable is undeclared. here is the code:



#include "Servo.h"

void setup()
Servo servo1;
int x_key = A0;
servo1.attach(2);
pinMode(A0, INPUT);
pinMode(2, OUTPUT);

int x_position = 90;


void loop()
if (analogRead(A0) < 512)
x_position++;
servo1.write(x_position);








arduino-uno programming c++ motor servo






share|improve this question









New contributor



ThisIsAron is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.










share|improve this question









New contributor



ThisIsAron is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








share|improve this question




share|improve this question








edited May 12 at 19:29









Duncan C

2,2542720




2,2542720






New contributor



ThisIsAron is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








asked May 12 at 19:27









ThisIsAronThisIsAron

61




61




New contributor



ThisIsAron is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




New contributor




ThisIsAron is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.














  • You need to select your code and tap the (code formatting) button. I did it for you.

    – Duncan C
    May 12 at 19:29






  • 2





    Of course it's undeclared. The variable is in setup. It's not in loop.

    – Majenko
    May 12 at 19:30






  • 1





    Because the variable is only valid, where it is declared, to use the official term: it's scope. When the program moves out of the scope of this variable, it will get thrown away and be no longer available. You can declare it in global scope, meaning outside of any function

    – chrisl
    May 12 at 19:32











  • This is more a question about C/C++, not about Arduino

    – chrisl
    May 12 at 19:33











  • @chrisl true, but the OP doesn't know enough to know where to ask the question. We all have to start somewhere. I just wish people would do some self-study before running to the internet looking for somebody else to answer their questions for them.

    – Duncan C
    May 12 at 19:35

















  • You need to select your code and tap the (code formatting) button. I did it for you.

    – Duncan C
    May 12 at 19:29






  • 2





    Of course it's undeclared. The variable is in setup. It's not in loop.

    – Majenko
    May 12 at 19:30






  • 1





    Because the variable is only valid, where it is declared, to use the official term: it's scope. When the program moves out of the scope of this variable, it will get thrown away and be no longer available. You can declare it in global scope, meaning outside of any function

    – chrisl
    May 12 at 19:32











  • This is more a question about C/C++, not about Arduino

    – chrisl
    May 12 at 19:33











  • @chrisl true, but the OP doesn't know enough to know where to ask the question. We all have to start somewhere. I just wish people would do some self-study before running to the internet looking for somebody else to answer their questions for them.

    – Duncan C
    May 12 at 19:35
















You need to select your code and tap the (code formatting) button. I did it for you.

– Duncan C
May 12 at 19:29





You need to select your code and tap the (code formatting) button. I did it for you.

– Duncan C
May 12 at 19:29




2




2





Of course it's undeclared. The variable is in setup. It's not in loop.

– Majenko
May 12 at 19:30





Of course it's undeclared. The variable is in setup. It's not in loop.

– Majenko
May 12 at 19:30




1




1





Because the variable is only valid, where it is declared, to use the official term: it's scope. When the program moves out of the scope of this variable, it will get thrown away and be no longer available. You can declare it in global scope, meaning outside of any function

– chrisl
May 12 at 19:32





Because the variable is only valid, where it is declared, to use the official term: it's scope. When the program moves out of the scope of this variable, it will get thrown away and be no longer available. You can declare it in global scope, meaning outside of any function

– chrisl
May 12 at 19:32













This is more a question about C/C++, not about Arduino

– chrisl
May 12 at 19:33





This is more a question about C/C++, not about Arduino

– chrisl
May 12 at 19:33













@chrisl true, but the OP doesn't know enough to know where to ask the question. We all have to start somewhere. I just wish people would do some self-study before running to the internet looking for somebody else to answer their questions for them.

– Duncan C
May 12 at 19:35





@chrisl true, but the OP doesn't know enough to know where to ask the question. We all have to start somewhere. I just wish people would do some self-study before running to the internet looking for somebody else to answer their questions for them.

– Duncan C
May 12 at 19:35










1 Answer
1






active

oldest

votes


















4














Yes, that is how C and C++ (and most other C-like languages) work. Variables have "scope". Any variable define inside a pair of curly braces (between a and a ) is only visible inside those braces.



If you want to reference a variable in both setup() and loop(), you have to make it a global variable, defined at the top of your code.



#include "Servo.h"
Servo servo1;
int x_position = 90; //Define your global var(s) here



void setup()
int x_key = A0; //This var only exists inside the setup function
servo1.attach(2);
pinMode(A0, INPUT);
pinMode(2, OUTPUT);



void loop()
if (analogRead(A0) < 512)
x_position++;
servo1.write(x_position);







share|improve this answer




















  • 2





    Surely servo1 should be global as well?

    – copper.hat
    May 13 at 1:02











  • Yup, that too. I was in a bit of a hurry when I made that post.

    – Duncan C
    2 days ago











Your Answer






StackExchange.ifUsing("editor", function ()
return StackExchange.using("schematics", function ()
StackExchange.schematics.init();
);
, "cicuitlab");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "540"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);






ThisIsAron is a new contributor. Be nice, and check out our Code of Conduct.









draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2farduino.stackexchange.com%2fquestions%2f65337%2fdeclared-variable-inside-void-setup-is-forgotten-in-void-loop%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









4














Yes, that is how C and C++ (and most other C-like languages) work. Variables have "scope". Any variable define inside a pair of curly braces (between a and a ) is only visible inside those braces.



If you want to reference a variable in both setup() and loop(), you have to make it a global variable, defined at the top of your code.



#include "Servo.h"
Servo servo1;
int x_position = 90; //Define your global var(s) here



void setup()
int x_key = A0; //This var only exists inside the setup function
servo1.attach(2);
pinMode(A0, INPUT);
pinMode(2, OUTPUT);



void loop()
if (analogRead(A0) < 512)
x_position++;
servo1.write(x_position);







share|improve this answer




















  • 2





    Surely servo1 should be global as well?

    – copper.hat
    May 13 at 1:02











  • Yup, that too. I was in a bit of a hurry when I made that post.

    – Duncan C
    2 days ago















4














Yes, that is how C and C++ (and most other C-like languages) work. Variables have "scope". Any variable define inside a pair of curly braces (between a and a ) is only visible inside those braces.



If you want to reference a variable in both setup() and loop(), you have to make it a global variable, defined at the top of your code.



#include "Servo.h"
Servo servo1;
int x_position = 90; //Define your global var(s) here



void setup()
int x_key = A0; //This var only exists inside the setup function
servo1.attach(2);
pinMode(A0, INPUT);
pinMode(2, OUTPUT);



void loop()
if (analogRead(A0) < 512)
x_position++;
servo1.write(x_position);







share|improve this answer




















  • 2





    Surely servo1 should be global as well?

    – copper.hat
    May 13 at 1:02











  • Yup, that too. I was in a bit of a hurry when I made that post.

    – Duncan C
    2 days ago













4












4








4







Yes, that is how C and C++ (and most other C-like languages) work. Variables have "scope". Any variable define inside a pair of curly braces (between a and a ) is only visible inside those braces.



If you want to reference a variable in both setup() and loop(), you have to make it a global variable, defined at the top of your code.



#include "Servo.h"
Servo servo1;
int x_position = 90; //Define your global var(s) here



void setup()
int x_key = A0; //This var only exists inside the setup function
servo1.attach(2);
pinMode(A0, INPUT);
pinMode(2, OUTPUT);



void loop()
if (analogRead(A0) < 512)
x_position++;
servo1.write(x_position);







share|improve this answer















Yes, that is how C and C++ (and most other C-like languages) work. Variables have "scope". Any variable define inside a pair of curly braces (between a and a ) is only visible inside those braces.



If you want to reference a variable in both setup() and loop(), you have to make it a global variable, defined at the top of your code.



#include "Servo.h"
Servo servo1;
int x_position = 90; //Define your global var(s) here



void setup()
int x_key = A0; //This var only exists inside the setup function
servo1.attach(2);
pinMode(A0, INPUT);
pinMode(2, OUTPUT);



void loop()
if (analogRead(A0) < 512)
x_position++;
servo1.write(x_position);








share|improve this answer














share|improve this answer



share|improve this answer








edited May 13 at 8:52









Sim Son

36617




36617










answered May 12 at 19:31









Duncan CDuncan C

2,2542720




2,2542720







  • 2





    Surely servo1 should be global as well?

    – copper.hat
    May 13 at 1:02











  • Yup, that too. I was in a bit of a hurry when I made that post.

    – Duncan C
    2 days ago












  • 2





    Surely servo1 should be global as well?

    – copper.hat
    May 13 at 1:02











  • Yup, that too. I was in a bit of a hurry when I made that post.

    – Duncan C
    2 days ago







2




2





Surely servo1 should be global as well?

– copper.hat
May 13 at 1:02





Surely servo1 should be global as well?

– copper.hat
May 13 at 1:02













Yup, that too. I was in a bit of a hurry when I made that post.

– Duncan C
2 days ago





Yup, that too. I was in a bit of a hurry when I made that post.

– Duncan C
2 days ago










ThisIsAron is a new contributor. Be nice, and check out our Code of Conduct.









draft saved

draft discarded


















ThisIsAron is a new contributor. Be nice, and check out our Code of Conduct.












ThisIsAron is a new contributor. Be nice, and check out our Code of Conduct.











ThisIsAron is a new contributor. Be nice, and check out our Code of Conduct.














Thanks for contributing an answer to Arduino Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2farduino.stackexchange.com%2fquestions%2f65337%2fdeclared-variable-inside-void-setup-is-forgotten-in-void-loop%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Get product attribute by attribute group code in magento 2get product attribute by product attribute group in magento 2Magento 2 Log Bundle Product Data in List Page?How to get all product attribute of a attribute group of Default attribute set?Magento 2.1 Create a filter in the product grid by new attributeMagento 2 : Get Product Attribute values By GroupMagento 2 How to get all existing values for one attributeMagento 2 get custom attribute of a single product inside a pluginMagento 2.3 How to get all the Multi Source Inventory (MSI) locations collection in custom module?Magento2: how to develop rest API to get new productsGet product attribute by attribute group code ( [attribute_group_code] ) in magento 2

Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

Magento 2.3: How do i solve this, Not registered handle, on custom form?How can i rewrite TierPrice Block in Magento2magento 2 captcha not rendering if I override layout xmlmain.CRITICAL: Plugin class doesn't existMagento 2 : Problem while adding custom button order view page?Magento 2.2.5: Overriding Admin Controller sales/orderMagento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 : File Upload issue in UI Component FormMagento2 Not registered handleHow to configured Form Builder Js in my custom magento 2.3.0 module?Magento 2.3. How to create image upload field in an admin form