INSTRUCTING ARDUINO TO RECALL A PREVIOUSLY GIVEN VALUE

Hi, I'm not having much luck trying to solve a problem which I shall explain to you now:

I am using Processing to communicate with Arduino via serial. On the Processing side, I have a textbox into which I would like to enter a measurement (of distance). This measurement will recalled for and utilised by a number of different functions.

move along x axis (distance)cm move along y axis (distance)cm
\ /
"distance"
/
complete cycle with margins defined by (distance) walk on your hands (distance)

This is what I would like to achieve:

The (distance) is submitted from Processing and received by Arduino > Arduino takes note of the (distance) and stores it away > Arduino receives some other instructions from Processing > The instructions tell arduino to move (distance)cm > Arduino scratches its head > Arduino REMEMBERS that it has already been told the distance before and recalls this figure.

At present the (distance) is received by Arduino using Serial.read(). I suppose what I am proposing is Serial.has-read-in-the-past()

Here is my code on the arduino side:

void loop() {
   while (Serial.available()) {
   delay(10);
   char c = Serial.read();  
	readString += c;}
        if (readString.length() >0) {
	Serial.println(readString);
	int n;
	char carray[6];
	readString.toCharArray(carray, sizeof(carray));
	n = atoi(carray);
    n = n * 1000;
    readString="";
    }
    if (incomingByte == 'X') {
      Xstepper.enableOutputs();
      Ystepper.disableOutputs();
      Xstepper.move(n); 
      Xstepper.runToPosition();
 
    if (incomingByte == 'Y') {
      Ystepper.enableOutputs();   
      Xstepper.disableOutputs();
      Ystepper.move(n); 
      Ystepper.runToPosition();
      }

The error message I Receive is "'n' was not declared in this scope". And I'm like "No stupid, i already told you what n is - have you forgotten already?"

Do i need to be doing something with SRAM to write a piece of information to memory so that I can recall this information. Any suggestion would be greatly appreciated.

Best, Arthur

Post the whole sketch. From what you have posted, we can't see if you have defined 'n' anywhere else. The compiler is telling you that you haven't defined it in a way that something that you want to see it can.

Ok. Read it more closely. You have defined 'n' in the if block, so it is local to that block. As soon as that block completes, 'n' goes out of scope, so yes, it is forgotten about. If you want it to remember it, define 'n' at the start of loop() instead.

if (readString.length() >0) {
	Serial.println(readString);
	int n;
	char carray[6];
	readString.toCharArray(carray, sizeof(carray));
	n = atoi(carray);
    n = n * 1000;
    readString="";
    }

n is a local variable. It goes out of scope when this block ends. It can not be referenced outside of this block.

You need to change the scope of the variable. If you want it's value to persist, seriously consider changing its name, too.

Global variables named c or i or n are just recipes for disaster.

I think all of you have put your finger on the button. Can I use other variables to include the lines:

if (incomingByte == 'X') {
Xstepper.enableOutputs();
Ystepper.disableOutputs();
Xstepper.move(n);
Xstepper.runToPosition();

within the scope of variable n?

is the idea of storing the variable using SRAM ridiculous?

Best, arthur

is the idea of storing the variable using SRAM ridiculous?

No.

Can I use other variables to include the lines...within the scope of variable n?

No idea what this means. As I pointed out earlier, n is a lousy name. It implies nothing about its contents.

haha ok thanks paul. well ive changed 'n' to 'distance' now and as you will be able to tell there is a snippet of code which reassembles ASCII input from Processing into numbers which the library 'accelstepper' can understand. so n (or 'distance') is the number I typed in from my processing sketch.

instead of messing around with control structures, I would like to try the SRAM approach. I would really appreciate some pointers as to how I might achieve this.

best, arthur

You are already using SRAM. That's where read/write variables are stored.

thats good news! but im still quite clueless. In essence, the problem is that I want to establish a global variable once that varible has been defined by an input from Processing. Its a contradiction of terms really: I want to create a variable global variable. I've been looking at the 'static' function. is this a valid route to go down

any help appreciated im quite lost.

arthur

If you want a global variable, declare it at the top of the sketch before your setup() and loop() functions.

int distance;

void setup()
{
// code can use distance here.
}

void loop()
{
// code can use distance here too.
}

void anotherFunction()
{
// and here too.
}

Hello I have a dilemma which niggling away at me and I though I would appeal for your help. Shouldn't be too much sweat for all you cHipsters.

I'll briefly outline the issue as far as I understand it before pasting my code. I'm sending information from Processing to Arduino. On the Processing side there is a simple textbox into which I would like to enter some numbers defining distance (of a motor). On the arduino side I have set up a function to read this information. IN A SEPARATE FUNCTION I have asked arduino to move a motor the distance ascribed by the previous function. However, because I have not defined the distance "within this scope" i.e. within the SAME scope, it wont work. I would like to define the distance in one function and to store this information. i would like to be able to access this 'stored information' for use in other functions.

Here's the code:

void loop() {
   while (Serial.available()) {
      delay(10);
      char c = Serial.read();  
      readString += c;}
      if (readString.length() >0) {
      Serial.println(readString);
      int n;
      char carray[6];
      readString.toCharArray(carray, sizeof(carray));
      distance = atoi(carray);
   readString="";
   }
   if (incomingByte == 'Y') {
      Ystepper.move(distance); 
      Ystepper.runToPosition();
   }
}

As you can see, 'distance' is established as a local variable within the first function. As we know "Local variables are only visible to the function in which they are declared." How do I make it visible to the second function (obviously without simply making it a global variable, since the distance will need to be updated as the programs runs).

I hope this makes sense! XD I would greatly appreciate any comments and I hope this thread will be useful to others.

Arthur

How do I make it visible to the second function (obviously without simply making it a global variable, since the distance will need to be updated as the programs runs).

Why would making it global prevent it from being updated?

just because its global doesn't mean you cant update it

Or you can read about pointers and proper use of reference and deference and pass the variable to the next function

winner10920:
Or you can read about pointers and proper use of reference and deference and pass the variable to the next function

Walk before run, perhaps.

Sorry I don't think I'm being very clear. Your right a global variable can be updated. However, It seems that my definition of 'distance' is only valid during the function in which it is defined. In the second function I would like to find a way to recall/remember this variable.

the following quotation is taken from the Function Declaration page on the Arduino site:
"Segmenting code into functions allows a programmer to create modular pieces of code that perform a defined task and then return to the area of code from which the function was "called"."
Source: http://arduino.cc/en/Reference/FunctionDeclaration

i would like my function to change the meaning of the variable 'distance'. I would like the variable 'distance' to take effect on other functions. I hope this helps.

best, Arthur

I showed you how to create a global distance variable in the other thread. Provided you don't also define local versions of distance, it will do what you want.

Here it is again:

int distance;

void setup()
{
// code can use distance here.
}

void loop()
{
// code can use distance here too.
}

void anotherFunction()
{
// and here too.
}

thankyou dx, but I'm not having any luck by defining the global variable before setup.

Provided you don't also define local versions of distance, it will do what you want.

It is necessary to define local versions of distance because i need to define it via processing using the function serial.read. so you see, distance cant exist before set us because it needs to be assigned while the program is running

It seems that the variable is forgotten after it has "return[ed] to the area of code from which the function was "called"."

is this the way arduino works? does it only use a variable during an active function? if so, how can i get around this problem.

Best, arthur

t I'm not having any luck by defining the global variable before setup.

Sorry, but the C compiler is immunised against luck.

It is necessary to define local versions of distance because i need to define it via processing using the function serial.read. so you see, distance cant exist before set us because it needs to be assigned while the program is running

I think you are confusing yourself. You don't define distance from Processing. You assign distance a value that you get from Serial.read().

if so, how can i get around this problem.

There is no problem to get around, other than the one you have created in your mind.

Post your complete sketch, and we'll see if we can sort it out.