Can some one give me a hand with this code?

Hi Guys,

I am creating a animatronic hand that is controlled by a glove that I am wearing. This is my first Arduino project so I am still getting to grips with everything. The compiler I am using often refuses to tell me what errors are in my code but at one point I poked it enough to tell me that in my sending code that "servo.h can’t be found’ and in my receiving code it told me that some things were not defined, but it won’t tell me what…

Could you guys have a look at my code and help fix it?

Thanks in advance!!

Here is my receiving code…

#include <Servo.h>


#define MAX_FINGER 5 
// #include 
// create servo object to control a servo 
typedef struct { 
  Servo myservo; 
  byte servoAng; 
} SERVO;

SERVO servo[MAX_FINGER]; 

void setup() { 
  Serial.begin(9600); 
  int i = 0; 
  for (i = 0; i < MAX_FINGER; i++) { 
    servo[i].myservo.attach(i + 2); 
  } 
} 

void loop() { 
  if(Serial.available() >=5) { 
    int i = 0; 
    for (i = 0; i < MAX_FINGER; i++) { 
      servo[i].servoAng = Serial.read(); 
      servo[i].myservo.write( servo[i].servoAng ); 
    } 
    // Send the servo to the position read... (note: you get to make this happen) 
  } 
}

And here is my sending code…

#define MAX_FINGER 5 
typedef struct { 
  byte servoValue; 
  byte servoVal; 
  int iFinger; 
} SERVO; 

SERVO servo[MAX_FINGER]; 
const int ciDefault[] = { 200, 460};

void setup() { 
  Serial.begin(9600); 
} 
void init_default() { 
  int i = 0; 
  for (i = 0; i < MAX_FINGER; i++) { 
    servo[i].iFinger = analogRead(i); 
    if (servo[i].iFinger < ciDefault[0]) servo[i].iFinger = ciDefault[0]; 
    else if (servo[i].iFinger > ciDefault[1]) servo[i].iFinger = ciDefault[1]; 
    servo[i].servoVal = map(servo[i].iFinger, 460, 200, 255, 0); 
    Serial.print(servo[i].servoVal); 
  } 
}

void loop() { 
  init_default(); 
  delay(100); 
}

Thanks again!!

The compiler I am using

And why aren't you using the IDE? Or, at least falling back to that when you have problems with whatever you are using?

The receiver code compiles fine in the IDE. As does the sender code.

I don't really see the point of your SERVO structure. If you need to know the "angle" the servo is at, just use the "read" method in the Servo class.

PaulS:

The compiler I am using

And why aren't you using the IDE? Or, at least falling back to that when you have problems with whatever you are using?

The receiver code compiles fine in the IDE. As does the sender code.

Do you mean the IDE to input to the Arduinos?

Do you mean the IDE to input to the Arduinos?

Yes.

PaulS:

Do you mean the IDE to input to the Arduinos?

Yes.

Well the problem with that is that I don't actually have the Arduino's yet and I am just writing this on a temperamental version of Xcode. Does this code look like it will run? Even with the servo.h not found and the undefined problems Xcode gives me?

  int iFinger;

How many iFingers are you planning for the iGlove to have? How do you intend to deal with more than 255 servos, in terms of controlling or powering them?

    if (servo[i].iFinger < ciDefault[0]) servo[i].iFinger = ciDefault[0]; 
    else if (servo[i].iFinger > ciDefault[1]) servo[i].iFinger = ciDefault[1];

Perhaps you’d like to meet the constrain() function. No sense reinventing the wheel.

    Serial.print(servo[i].servoVal);
      servo[i].servoAng = Serial.read();

Serial.print() will take a value like 200 and convert it to ‘2’, ‘0’, ‘0’ and send those values to the serial port. You can not then read them all with one statement.

Serial.write() will send the byte-sized value as a single byte, that you can then read with one call.

Be sure to read and answer AWOL’s question, too.

GeorgeBA13: Well the problem with that is that I don't actually have the Arduino's yet and I am just writing this on a temperamental version of Xcode. Does this code look like it will run? Even with the servo.h not found and the undefined problems Xcode gives me?

You don't need to physically possess an Arduino. Just download the Mac version of the IDE from this site.

PaulS:

  int iFinger;

How many iFingers are you planning for the iGlove to have? How do you intend to deal with more than 255 servos, in terms of controlling or powering them?

    if (servo[i].iFinger < ciDefault[0]) servo[i].iFinger = ciDefault[0]; 

else if (servo[i].iFinger > ciDefault[1]) servo[i].iFinger = ciDefault[1];



Perhaps you'd like to meet the [constrain()](http://arduino.cc/en/Reference/Constrain) function. No sense reinventing the wheel.



Serial.print(servo[i].servoVal);





servo[i].servoAng = Serial.read();



Serial.print() will take a value like 200 and convert it to '2', '0', '0' and send those values to the serial port. You can not then read them all with one statement.

Serial.write() will send the byte-sized value as a single byte, that you can then read with one call.

Be sure to read and answer AWOL's question, too.

Well I am only using 5 servos, one for each finger and one for the thumb.

I have been told to use the constrain function before but I don’t really understand how to use it. Could you give me some help?

Are you saying that the Serial.print() won’t work and I should replace them with Serial.byte()?

Thank you for all of your help so far!

[quote author=Nick Gammon link=topic=131050.msg985725#msg985725 date=1352233157]

GeorgeBA13: Well the problem with that is that I don't actually have the Arduino's yet and I am just writing this on a temperamental version of Xcode. Does this code look like it will run? Even with the servo.h not found and the undefined problems Xcode gives me?

You don't need to physically possess an Arduino. Just download the Mac version of the IDE from this site. [/quote]

Thank you Nick I will get that now!

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

AWOL: I don't really see the point of your SERVO structure. If you need to know the "angle" the servo is at, just use the "read" method in the Servo class.

Thanks, this is something I have been told to do but I am not sure where to put it in my code. I am supposed to use Serial.read() aren't I? But what do I replace in my code?

By the way: Sorry that I am asking very basic questions but I am very new to this and I am still trying to get the hang of everything!

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

AWOL: http://arduino.cc/en/Reference/ServoRead

AWOL: http://arduino.cc/en/Reference/Constrain

Thanks! I'll look over these again. When you say you don't see the point in the servo structure does that mean that it will not work? Or just that it can be made neater by using these functions?

Bump anyone?

GeorgeBA13: Bump anyone?

Really? You're THAT impatient?

Bump anyone?

You don't even have an Arduino. What is the rush?

Well I am only using 5 servos, one for each finger and one for the thumb.

And yet you need an int to count them? Really?

Are you saying that the Serial.print() won't work and I should replace them with Serial.byte()?

Where did I say that?

Serial.write() will send the byte-sized value as a single byte, that you can then read with one call.

Certainly not here.

When you say you don't see the point in the servo structure does that mean that it will not work? Or just that it can be made neater by using these functions?

The code should work, although clearly you'll need to test it, and adjust the limits until you get everything working exactly right, but there is a lot of unnecessary stuff going on.

Arrch:

GeorgeBA13:
Bump anyone?

Really? You’re THAT impatient?

Hi again,
Yeah sorry about that I am running around being pretty busy at the moment and I thought I had left it about an hour and a half but it was only about 20 minutes!

PaulS:

Bump anyone?

You don’t even have an Arduino. What is the rush?

Well I am only using 5 servos, one for each finger and one for the thumb.

And yet you need an int to count them? Really?

Are you saying that the Serial.print() won’t work and I should replace them with Serial.byte()?

Where did I say that?

Serial.write() will send the byte-sized value as a single byte, that you can then read with one call.

Certainly not here.

When you say you don’t see the point in the servo structure does that mean that it will not work? Or just that it can be made neater by using these functions?

The code should work, although clearly you’ll need to test it, and adjust the limits until you get everything working exactly right, but there is a lot of unnecessary stuff going on.

Very true about that, please see my apology higher up in the post!

I though that I would use int as it is using numbers and as I use python quite a bit I always use ints. Is that a bad idea?

In your post here:

PaulS:

  int iFinger;

How many iFingers are you planning for the iGlove to have? How do you intend to deal with more than 255 servos, in terms of controlling or powering them?

    if (servo[i].iFinger < ciDefault[0]) servo[i].iFinger = ciDefault[0]; 

else if (servo[i].iFinger > ciDefault[1]) servo[i].iFinger = ciDefault[1];



Perhaps you'd like to meet the [constrain()](http://arduino.cc/en/Reference/Constrain) function. No sense reinventing the wheel.



Serial.print(servo[i].servoVal);





servo[i].servoAng = Serial.read();



Serial.print() will take a value like 200 and convert it to '2', '0', '0' and send those values to the serial port. You can not then read them all with one statement.

Serial.write() will send the byte-sized value as a single byte, that you can then read with one call.

Be sure to read and answer AWOL's question, too.

I thought that is what you meant.

Really I can probably live with a bit of unnecessary stuff for the first project! I’ll do some other ones and then perfect this!

Thanks a lot for the help!

I though that I would use int as it is using numbers and as I use python quite a bit I always use ints. Is that a bad idea?

It's just that Python runs on a PC with more than 2K of SRAM. You need to be aware of every wasted byte on the Arduino.

I meant for you to use Serial.write() to send the value. Your question was whether I meant for you to use Serial.byte(). That I did not mean (or say).

PaulS:

I though that I would use int as it is using numbers and as I use python quite a bit I always use ints. Is that a bad idea?

It's just that Python runs on a PC with more than 2K of SRAM. You need to be aware of every wasted byte on the Arduino.

I meant for you to use Serial.write() to send the value. Your question was whether I meant for you to use Serial.byte(). That I did not mean (or say).

Ok I understand. Sorry for the confusion and thanks for the clarification!