Setting a string value

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
jdoscher
 
Posts: 124
Joined: Tue Jul 22, 2008 12:36 pm

Setting a string value

Post by jdoscher »

Hi All,

I want to set a string variable that can be changed depending on which function is running it. The problem is that I can set the value initially, but my function doesn't seem to pickup the value change. For example in the beginning of my script, I set it like below. Any ideas?
char motorDirection[] = "Not set";
Then later on in a function, I want to change it in this function:
void raiseAngle()
{
  int sensorValue = analogRead(sensorPin); // read the input pin
  mappedValue = map(sensorValue, 5, 804, 0, 92);
  motorLinSpeed--;
  char motorDirection[] = "Raise";
  motorEcho();

  if (motorLinSpeed <= -200) {
    motorLinSpeed = -200;
  }
  md.setM1Speed(motorLinSpeed);
}
Then later, I want to print it out.
void motorEcho()
{
  if (DEBUGPrint == true) {
    Serial.print("Motor:");
    Serial.println(motorDirection);
  }
}

User avatar
adafruit_support_bill
 
Posts: 88039
Joined: Sat Feb 07, 2009 10:11 am

Re: Setting a string value

Post by adafruit_support_bill »

Your second declaraton

Code: Select all

char motorDirection[] = "Raise"
is declaring a local motorDirection array on the stack. It is not modifying the motorDirection you declared globally. You can use the C string functions to operate on your array. But a better (safer) route is to use the String object in the Arduino library. That has a full set of string manipulation functions.

http://arduino.cc/en/Reference/StringObject

jdoscher
 
Posts: 124
Joined: Tue Jul 22, 2008 12:36 pm

Re: Setting a string value

Post by jdoscher »

Thank you- I have switched the variable to a String type- but if I understand you correctly, I can't set the value in one sub/function then read it back in another? I have calculations when to raise, lower, rotate positive and rotate negative two motors depending on my calcs, and I'd like to set a string so that my echo/serial.print routine can be used for all of them for debugging.

Thanks!
Jay

User avatar
adafruit_support_bill
 
Posts: 88039
Joined: Sat Feb 07, 2009 10:11 am

Re: Setting a string value

Post by adafruit_support_bill »

but if I understand you correctly, I can't set the value in one sub/function then read it back in another?
Not quite what I said. It is possible to modify a global array from within one function and access if from another. What your code did was to define a completely different local array. Although it had the same name as the global array, it was in a physically different memory location and accessible only to that function. So anything you did to it would not be visible outside that function.

You can perform string operation on global character arrays using standard C string functions. But the Arduino string object is safer to use because it protects you from overrunning the array.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”