Will this code work for Arudino.

Hi Guys,

I am making a anamatronic hand and I am wondering if this code will work. It is new condensed version and I wonder if it will work. There is a sending glove that sends the movement of my hand to a seperate ardino via xbees.

#define MAX_FINGER 5 
typedef struct { 
byte servoValue; 
byte servoVal; 
int iFinger; 
} DEFAULT 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); 
}

That is the sending one

Here is the receiving: 
// #include 
// create servo object to control a servo 
typedef struct { 
Servo myservo; 
byte servoAng; 
}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) 
} 
}

It might work if you get rid of the italics.
Go back to your post, click on "modify", highlight the code, the click on the # icon on the toolbar, then click on "save"

Is that better?

What do you think?
Have another look at reply #1

Hmmm… I’m gonna say it will… maybe…
Thanks for the help!

GeorgeBA13:
Hmmm... I'm gonna say it will... maybe..

I'm gonna say ... maybe not.

// Your code should be INSIDE the code section.

Really why not?

Bump?

Well first of all, try compiling it. It doesn’t compile at all.

There are several issues.

Firstly, you cannot declare a struct like this:

typedef struct { 
byte servoValue; 
byte servoVal; 
int iFinger; 
} DEFAULT servo[MAX_FINGER];

Try this instead:

typedef struct { 
  byte servoValue; 
  byte servoVal; 
  int iFinger; 
} SERVO; 

SERVO servo[MAX_FINGER];

Notice that DEFAULT cannot be used as it is a #define in another part of the Arduino core (part of analogRead()). Secondly you can’t create a struct at the same time as you define it. You have to seperate the creation of an object from the creation of a type.

For the servo side, you forgot this line:

#include <Servo.h>

Which goes at the very top and includes the servo library.

There is one place in your code where you have used a ‘-’ sign instead of an ‘=’ sign. In another place you missed a ‘)’.

The next thing to point out is that if you format the code correctly (autoformat helps), then it is easier to read.

After fixing those, you get this:
Code1:

#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) 
  } 
}

Code2:

#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); 
}

Which compiles. Whether it works or not, I haven’t looked through it to see (it is 1am here and I am off to bed).

typedef struct { 
  byte servoValue; 
  byte servoVal; 
  int iFinger; 
} SERVO;

How many fingers do you have? Are there really going to be more than 255 of them?