Teachable Robotic Arm

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.
User avatar
mlandergan
 
Posts: 56
Joined: Sat Jul 23, 2011 6:15 pm

Teachable Robotic Arm

Post by mlandergan »

Hello,

I am in the processing of building a robotic arm using an arduino uno, laser cut pieces and six analog servos from adafruit. Here is what the robot looks like so far http://i.imgur.com/1HFAxsXh.jpg

I am using the record/playback code to record the movements of the base servo and then play it back. What I am trying to do is change the code given and incorporate all six servos so when I hit record it records the movements of all six servos and when I hit replay it replays all the movements of each servo.

Here is a link to the code that Adafruit provided. https://github.com/adafruit/Feedback-Se ... d-and-Play

I believe this should be an easy task but I am at a lost on how to start.
Thanks

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Teachable Robotic Arm

Post by adafruit_support_rick »

mlandergan wrote:I believe this should be an easy task but I am at a lost on how to start.
To get started with recording, you want to define all six analog feedback pins.

Code: Select all

uint8_t servo1FeedbackPin = A0;
uint8_t servo2FeedbackPin = A1;
uint8_t servo3FeedbackPin = A2;
uint8_t servo4FeedbackPin = A3;
uint8_t servo5FeedbackPin = A4;
uint8_t servo6FeedbackPin = A5;
Then, in recordServo, you want to break out the code inside the while loop

Code: Select all

  pinMode(analogPin, INPUT); 
  while (digitalRead(buttonPin)) {
     uint16_t a = analogRead(analogPin);
     
     Serial.print("Read analog: "); Serial.print(a);
     if (a < CALIB_MIN) a = CALIB_MIN;
     if (a > CALIB_MAX) a = CALIB_MAX;
     a = map(a, CALIB_MIN, CALIB_MAX, 0, 254);
     Serial.print(" -> "); Serial.println(a);
     EEPROM.write(addr, a);
     addr++;
     if (addr == 512) break;
     delay(SAMPLE_DELAY);
  }
  if (addr != 512) EEPROM.write(addr, 255);
into a new function. Then call that function six times, once for each servo feedback line:

Code: Select all

void recordAllServos(uint8_t servoPin, uint8_t buttonPin) {
  uint16_t addr = 0;
  
  Serial.println("Recording");
  digitalWrite(ledPin, HIGH);
 
  pinMode(servo1FeedbackPin, INPUT); 
  pinMode(servo2FeedbackPin, INPUT); 
  pinMode(servo3FeedbackPin, INPUT); 
  pinMode(servo4FeedbackPin, INPUT); 
  pinMode(servo5FeedbackPin, INPUT); 
  pinMode(servo6FeedbackPin, INPUT); 

  while (digitalRead(buttonPin)) 
  {
     readServo(servo1FeedbackPin);
     readServo(servo2FeedbackPin);
     readServo(servo3FeedbackPin);
     readServo(servo4FeedbackPin);
     readServo(servo5FeedbackPin);
     readServo(servo6FeedbackPin);

     if (addr > 506) break;
     delay(SAMPLE_DELAY);
  }
  if (addr != 512) EEPROM.write(addr, 255);

  digitalWrite(ledPin, LOW);

  Serial.println("Done");
  delay(250);
}

void readServo(uint8_t analogPin)
{
     uint16_t a = analogRead(analogPin);
     
     Serial.print("Read analog pin "); Serial.print(analogPin); Serial.print(": "); Serial.print(a);
     if (a < CALIB_MIN) a = CALIB_MIN;
     if (a > CALIB_MAX) a = CALIB_MAX;
     a = map(a, CALIB_MIN, CALIB_MAX, 0, 254);
     Serial.print(" -> "); Serial.println(a);
     EEPROM.write(addr, a);
     addr++;
}
This will write the servo positions in groups of six to eeprom. Rewrite playServo similarly to play back from eeprom in groups of six.

Also, if you're using an ATMega328 processor, like in a Uno, there are 1024 bytes in eeprom, not 512, so you can bump that in the code.

User avatar
mlandergan
 
Posts: 56
Joined: Sat Jul 23, 2011 6:15 pm

Re: Teachable Robotic Arm

Post by mlandergan »

Ok cool, I am working on the code now. I bumped up the bytes to 1024. I am still getting some errors in my code. Here is my code and errors.

Code: Select all

// Example code for recording and playing back servo motion with a 
// analog feedback servo
// http://www.adafruit.com/products/1404


#include <Servo.h>
#include <EEPROM.h>

#define CALIB_MAX 512
#define CALIB_MIN 100
#define SAMPLE_DELAY 25 // in ms, 50ms seems good

uint8_t recordButtonPin = 12;
uint8_t playButtonPin = 7;
uint8_t servo1Pin = 9;
uint8_t servo1FeedbackPin = A0;
uint8_t servo2FeedbackPin = A1;
uint8_t servo3FeedbackPin = A2;
uint8_t servo4FeedbackPin = A3;
uint8_t servo5FeedbackPin = A4;
uint8_t servo6FeedbackPin = A5;
uint8_t ledPin = 13;

Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
Servo servo5;
Servo servo6;
  
void setup() {
  Serial.begin(9600);
  pinMode(recordButtonPin, INPUT);
  digitalWrite(recordButtonPin, HIGH);
  pinMode(playButtonPin, INPUT);
  digitalWrite(playButtonPin, HIGH);
  pinMode(ledPin, OUTPUT);
  
  Serial.println("Servo RecordPlay");
}

void loop() {
 if (! digitalRead(recordButtonPin)) {
   delay(10);
   // wait for released
   while (! digitalRead(recordButtonPin));
   delay(20);
   // OK released!
   recordServo(servo1Pin, servo1FeedbackPin, recordButtonPin);
 }
 
  if (! digitalRead(playButtonPin)) {
   delay(10);
   // wait for released
   while (! digitalRead(playButtonPin));
   delay(20);
   // OK released!
   playServo(servoPin, playButtonPin);
 }
}

void playServo(uint8_t servoPin, uint8_t buttonPin) {
  uint16_t addr = 0;
  Serial.println("Playing");


  servo1.attach(servo1Pin);
  while (digitalRead(buttonPin)) {    
    uint8_t x = EEPROM.read(addr);
    Serial.print("Read EE: "); Serial.print(x);
    if (x == 255) break;
    // map to 0-180 degrees
    x = map(x, 0, 254, 0, 180);
    Serial.print(" -> "); Serial.println(x);
    myServo.write(x);
    delay(SAMPLE_DELAY);
    addr++;
    if (addr == 512) break;
  }
  Serial.println("Done");
  servo1.detach();
  delay(250);  
}

void recordAllServos(uint8_t servoPin, uint8_t buttonPin) {
  uint16_t addr = 0;
  
  Serial.println("Recording");
  digitalWrite(ledPin, HIGH);
 
  pinMode(servo1FeedbackPin, INPUT); 
  pinMode(servo2FeedbackPin, INPUT); 
  pinMode(servo3FeedbackPin, INPUT); 
  pinMode(servo4FeedbackPin, INPUT); 
  pinMode(servo5FeedbackPin, INPUT); 
  pinMode(servo6FeedbackPin, INPUT); 

  while (digitalRead(buttonPin)) 
  {
     readServo(servo1FeedbackPin);
     readServo(servo2FeedbackPin);
     readServo(servo3FeedbackPin);
     readServo(servo4FeedbackPin);
     readServo(servo5FeedbackPin);
     readServo(servo6FeedbackPin);

     if (addr > 506) break;
     delay(SAMPLE_DELAY);
  }
  if (addr != 1024) EEPROM.write(addr, 255);

  digitalWrite(ledPin, LOW);

  Serial.println("Done");
  delay(250);
}

void readServo(uint8_t analogPin)
{
     uint16_t a = analogRead(analogPin);
     
     Serial.print("Read analog pin "); Serial.print(analogPin); Serial.print(": "); Serial.print(a);
     if (a < CALIB_MIN) a = CALIB_MIN;
     if (a > CALIB_MAX) a = CALIB_MAX;
     a = map(a, CALIB_MIN, CALIB_MAX, 0, 254);
     Serial.print(" -> "); Serial.println(a);
     EEPROM.write(addr, a);
     addr++;
}

Errors:
sketch_dec02b.cpp: In function 'void loop()':
sketch_dec02b:48: error: 'recordServo' was not declared in this scope
sketch_dec02b:57: error: 'servoPin' was not declared in this scope
sketch_dec02b.cpp: In function 'void playServo(uint8_t, uint8_t)':
sketch_dec02b:74: error: 'myServo' was not declared in this scope
sketch_dec02b.cpp: In function 'void readServo(uint8_t)':
sketch_dec02b:126: error: 'addr' was not declared in this scope

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

Re: Teachable Robotic Arm

Post by adafruit_support_bill »

error: 'recordServo' was not declared in this scope
You changed the name of the function to recordAllServos. You have to change all the references to that function also.
error: 'servoPin' was not declared in this scope
As above. You changed the names of the servo pin definitions. You need to change the references as well.

User avatar
mlandergan
 
Posts: 56
Joined: Sat Jul 23, 2011 6:15 pm

Re: Teachable Robotic Arm

Post by mlandergan »

Ok thank you, I made those corrections and here is my code

Code: Select all

// Example code for recording and playing back servo motion with a 
// analog feedback servo
// http://www.adafruit.com/products/1404


#include <Servo.h>
#include <EEPROM.h>

#define CALIB_MAX 512
#define CALIB_MIN 100
#define SAMPLE_DELAY 25 // in ms, 50ms seems good

uint8_t recordButtonPin = 12;
uint8_t playButtonPin = 7;
uint8_t servo1Pin = 9;
uint8_t servo2Pin = 10;
uint8_t servo1FeedbackPin = A0;
uint8_t servo2FeedbackPin = A1;
uint8_t servo3FeedbackPin = A2;
uint8_t servo4FeedbackPin = A3;
uint8_t servo5FeedbackPin = A4;
uint8_t servo6FeedbackPin = A5;
uint8_t ledPin = 13;

Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
Servo servo5;
Servo servo6;
  
void setup() {
  Serial.begin(9600);
  pinMode(recordButtonPin, INPUT);
  digitalWrite(recordButtonPin, HIGH);
  pinMode(playButtonPin, INPUT);
  digitalWrite(playButtonPin, HIGH);
  pinMode(ledPin, OUTPUT);
  
  Serial.println("Servo RecordPlay");
}

void loop() {
 if (! digitalRead(recordButtonPin)) {
   delay(10);
   // wait for released
   while (! digitalRead(recordButtonPin));
   delay(20);
   // OK released!
   recordAllServos(servo1Pin, servo1FeedbackPin, recordButtonPin);
 }
 
  if (! digitalRead(playButtonPin)) {
   delay(10);
   // wait for released
   while (! digitalRead(playButtonPin));
   delay(20);
   // OK released!
   playAllServo(servo1Pin, playButtonPin);
 }
}

void playAllServo(uint8_t servoPin, uint8_t buttonPin) {
  uint16_t addr = 0;
  Serial.println("Playing");


  servo1.attach(servo1Pin);
  while (digitalRead(buttonPin)) {    
    uint8_t x = EEPROM.read(addr);
    Serial.print("Read EE: "); Serial.print(x);
    if (x == 255) break;
    // map to 0-180 degrees
    x = map(x, 0, 254, 0, 180);
    Serial.print(" -> "); Serial.println(x);
    servo1.write(x);
    delay(SAMPLE_DELAY);
    addr++;
    if (addr == 512) break;
  }
  Serial.println("Done");
  servo1.detach();
  delay(250);  
}

void recordAllServos(uint8_t servoPin, uint8_t buttonPin) {
  uint16_t addr = 0;
  
  Serial.println("Recording");
  digitalWrite(ledPin, HIGH);
 
  pinMode(servo1FeedbackPin, INPUT); 
  pinMode(servo2FeedbackPin, INPUT); 
  pinMode(servo3FeedbackPin, INPUT); 
  pinMode(servo4FeedbackPin, INPUT); 
  pinMode(servo5FeedbackPin, INPUT); 
  pinMode(servo6FeedbackPin, INPUT); 

  while (digitalRead(buttonPin)) 
  {
     readServo(servo1FeedbackPin);
     readServo(servo2FeedbackPin);
     readServo(servo3FeedbackPin);
     readServo(servo4FeedbackPin);
     readServo(servo5FeedbackPin);
     readServo(servo6FeedbackPin);

     if (addr > 506) break;
     delay(SAMPLE_DELAY);
  }
  if (addr != 1024) EEPROM.write(addr, 255);

  digitalWrite(ledPin, LOW);

  Serial.println("Done");
  delay(250);
}

void readAllServo(uint8_t analogPin)
{
     uint16_t a = analogRead(analogPin);
     
     Serial.print("Read analog pin "); Serial.print(analogPin); Serial.print(": "); Serial.print(a);
     if (a < CALIB_MIN) a = CALIB_MIN;
     if (a > CALIB_MAX) a = CALIB_MAX;
     a = map(a, CALIB_MIN, CALIB_MAX, 0, 254);
     Serial.print(" -> "); Serial.println(a);
     EEPROM.write(addr, a);
     addr++;
}

But I am still getting these errors:
Teachable_Arm_Mark.cpp: In function 'void loop()':
Teachable_Arm_Mark:10: error: too many arguments to function 'void recordAllServos(uint8_t, uint8_t)'
Teachable_Arm_Mark:49: error: at this point in file
Teachable_Arm_Mark.cpp: In function 'void recordAllServos(uint8_t, uint8_t)':
Teachable_Arm_Mark:100: error: 'readServo' was not declared in this scope
Teachable_Arm_Mark.cpp: In function 'void readAllServo(uint8_t)':
Teachable_Arm_Mark:127: error: 'addr' was not declared in this scope

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

Re: Teachable Robotic Arm

Post by adafruit_support_bill »

error: too many arguments to function 'void recordAllServos(uint8_t, uint8_t)'
The error message is pretty clear. It looks like you changed the function call signature from:
void recordServo(uint8_t servoPin, uint8_t analogPin, uint8_t buttonPin)
to:
void recordAllServos(uint8_t servoPin, uint8_t buttonPin)
Since your new function has only 2 parameters, you can't call it with 3 as before.

User avatar
mlandergan
 
Posts: 56
Joined: Sat Jul 23, 2011 6:15 pm

Re: Teachable Robotic Arm

Post by mlandergan »

Here is my code as of right now. It is able to load without errors but I am not achieving my goal of having all six servos be recorded/playedback. Any advice?

Code: Select all

// Example code for recording and playing back servo motion with a 
// analog feedback servo
// http://www.adafruit.com/products/1404


#include <Servo.h>
#include <EEPROM.h>

#define CALIB_MAX 512
#define CALIB_MIN 100
#define SAMPLE_DELAY 25 // in ms, 50ms seems good

uint8_t recordButtonPin = 6;
uint8_t playButtonPin = 7;
uint8_t servo1Pin = 8;
uint8_t servo2Pin = 9;
uint8_t servo3Pin = 10;
uint8_t servo4Pin = 11;
uint8_t servo5Pin = 12;
uint8_t servo6Pin = 13;
uint8_t servo1FeedbackPin = A0;
uint8_t servo2FeedbackPin = A1;
uint8_t servo3FeedbackPin = A2;
uint8_t servo4FeedbackPin = A3;
uint8_t servo5FeedbackPin = A4;
uint8_t servo6FeedbackPin = A5;
uint8_t ledPin = 3;

Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
Servo servo5;
Servo servo6;
  
void setup() {
  Serial.begin(9600);
  pinMode(recordButtonPin, INPUT);
  digitalWrite(recordButtonPin, HIGH);
  pinMode(playButtonPin, INPUT);
  digitalWrite(playButtonPin, HIGH);
  pinMode(ledPin, OUTPUT);
  
  Serial.println("Servo RecordPlay");
}

void loop() {
 if (! digitalRead(recordButtonPin)) {
   delay(10);
   // wait for released
   while (! digitalRead(recordButtonPin));
   delay(20);
   // OK released!
   recordAllServos(servo1Pin, servo1FeedbackPin, recordButtonPin);
 }
 
  if (! digitalRead(playButtonPin)) {
   delay(10);
   // wait for released
   while (! digitalRead(playButtonPin));
   delay(20);
   // OK released!
   playAllServo(servo1Pin, servo2Pin, servo3Pin, servo4Pin, servo5Pin, servo6Pin, playButtonPin);
 }
}

void playAllServo(uint8_t servo1Pin, uint8_t servo2Pin, uint8_t servo3Pin, uint8_t servo4Pin, uint8_t servo5Pin, uint8_t servo6Pin, uint8_t buttonPin) {
  uint16_t addr = 0;
  Serial.println("Playing");


  servo1.attach(servo1Pin);
  servo2.attach(servo2Pin);
  servo3.attach(servo3Pin);
  servo4.attach(servo4Pin);
  servo5.attach(servo5Pin);
  servo5.attach(servo6Pin);
  while (digitalRead(buttonPin)) {    
    uint8_t x = EEPROM.read(addr);
    Serial.print("Read EE: "); Serial.print(x);
    if (x == 255) break;
    // map to 0-180 degrees
    x = map(x, 0, 254, 0, 180);
    Serial.print(" -> "); Serial.println(x);
    servo1.write(x);
    servo2.write(x);
    servo3.write(x);
    servo4.write(x);
    servo5.write(x);
    servo6.write(x);
    delay(SAMPLE_DELAY);
    addr++;
    if (addr == 512) break;
  }
  Serial.println("Done");
  servo1.detach();
  delay(250);  
}

void recordAllServos(uint8_t servo1Pin, uint8_t servo1FeedbackPin, uint8_t buttonPin) {
  uint16_t addr = 0;
  
  Serial.println("Recording");
  digitalWrite(ledPin, HIGH);
 
  pinMode(servo1FeedbackPin, INPUT); 
  pinMode(servo2FeedbackPin, INPUT); 
  pinMode(servo3FeedbackPin, INPUT); 
  pinMode(servo4FeedbackPin, INPUT); 
  pinMode(servo5FeedbackPin, INPUT); 
  pinMode(servo6FeedbackPin, INPUT); 

  while (digitalRead(buttonPin)) 
  {
     readAllServo(servo1FeedbackPin);
     readAllServo(servo2FeedbackPin);
     readAllServo(servo3FeedbackPin);
     readAllServo(servo4FeedbackPin);
     readAllServo(servo5FeedbackPin);
     readAllServo(servo6FeedbackPin);

     if (addr > 506) break;
     delay(SAMPLE_DELAY);
  }
  if (addr != 1024) EEPROM.write(addr, 255);

  digitalWrite(ledPin, LOW);

  Serial.println("Done");
  delay(250);
}

void readAllServo(uint8_t analogPin)
{
     uint16_t addr = 0;
     uint16_t a = analogRead(analogPin);
     
     Serial.print("Read analog pin "); Serial.print(analogPin); Serial.print(": "); Serial.print(a);
     if (a < CALIB_MIN) a = CALIB_MIN;
     if (a > CALIB_MAX) a = CALIB_MAX;
     a = map(a, CALIB_MIN, CALIB_MAX, 0, 254);
     Serial.print(" -> "); Serial.println(a);
     EEPROM.write(addr, a);
     addr++;
}

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

Re: Teachable Robotic Arm

Post by adafruit_support_bill »

Your "RecordAllServos" function calls ReadAllServos 6 times in the while loop, but never writes anything to EEPROM.

User avatar
mlandergan
 
Posts: 56
Joined: Sat Jul 23, 2011 6:15 pm

Re: Teachable Robotic Arm

Post by mlandergan »

I made the correction. The code is able to save the values to the eeprom, but the playing part still doesn't work. Any more advice? Thank you again.

Here is the code:

Code: Select all

//Code Produced 
//By Mark Edward Landergan
//Science Fair 2013
//Robotic Arm

#include <Servo.h>
#include <EEPROM.h>

#define CALIB_MAX 512
#define CALIB_MIN 100
#define SAMPLE_DELAY 25 // in ms, 50ms seems good

uint8_t recordButtonPin = 6;
uint8_t playButtonPin = 7;
uint8_t servo1Pin = 8;
uint8_t servo2Pin = 9;
uint8_t servo3Pin = 10;
uint8_t servo4Pin = 11;
uint8_t servo5Pin = 12;
uint8_t servo6Pin = 13;
uint8_t servo1FeedbackPin = A0;
uint8_t servo2FeedbackPin = A1;
uint8_t servo3FeedbackPin = A2;
uint8_t servo4FeedbackPin = A3;
uint8_t servo5FeedbackPin = A4;
uint8_t servo6FeedbackPin = A5;
uint8_t ledPin = 3;
int addr1, addr2, addr3, addr4, addr5, addr6 = 0;

Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
Servo servo5;
Servo servo6;
  
void setup() {
  Serial.begin(9600);
  pinMode(recordButtonPin, INPUT);
  digitalWrite(recordButtonPin, HIGH);
  pinMode(playButtonPin, INPUT);
  digitalWrite(playButtonPin, HIGH);
  pinMode(ledPin, OUTPUT);
  
  Serial.println("Servo RecordPlay");
}

void loop() {
 if (! digitalRead(recordButtonPin)) {
   delay(10);
   // wait for released
   while (! digitalRead(recordButtonPin));
   delay(20);
   // OK released!
   recordAllServos(servo1Pin, servo1FeedbackPin, recordButtonPin);
 }
 
  if (! digitalRead(playButtonPin)) {
   delay(10);
   // wait for released
   while (! digitalRead(playButtonPin));
   Serial.print("Play!");
   delay(20);
   // OK released!
   playAllServo(servo1Pin, servo2Pin, servo3Pin, servo4Pin, servo5Pin, servo6Pin, playButtonPin);
 }
}

void playAllServo(uint8_t servo1Pin, uint8_t servo2Pin, uint8_t servo3Pin, uint8_t servo4Pin, uint8_t servo5Pin, uint8_t servo6Pin, uint8_t buttonPin) {
  Serial.println("Playing");


  servo1.attach(servo1Pin);
  servo2.attach(servo2Pin);
  servo3.attach(servo3Pin);
  servo4.attach(servo4Pin);
  servo5.attach(servo5Pin);
  servo5.attach(servo6Pin);
  while (digitalRead(buttonPin)) {    
  uint8_t x1 = EEPROM.read(addr1);
  Serial.print("Read EE Servo 1: "); Serial.print(x1);
    if (x1 == 255) break;
    // map to 0-180 degrees
    x1 = map(x1, 0, 254, 0, 180);
    Serial.print(" -> "); Serial.println(x1);
    servo1.write(x1);
    
  uint8_t x2 = EEPROM.read(addr2);
  Serial.print("Read EE Servo 2: "); Serial.print(x2);
    if (x2 == 255) break;
    // map to 0-180 degrees
    x2 = map(x2, 0, 254, 0, 180);
    Serial.print(" -> "); Serial.println(x2);
    servo2.write(x2);
    
  uint8_t x3 = EEPROM.read(addr3);
  if (x3 == 255) break;
  Serial.print("Read EE Servo 3: "); Serial.print(x3);  
  servo3.write(x3);
  
  uint8_t x4 = EEPROM.read(addr4);
  if (x4 == 255) break;
  Serial.print("Read EE Servo 4: "); Serial.print(x4);
  servo4.write(x4);
  
  uint8_t x5 = EEPROM.read(addr5);
  if (x5 == 255) break;
  Serial.print("Read EE Servo 5: "); Serial.print(x5);
    servo5.write(x5);
    
  uint8_t x6 = EEPROM.read(addr6); 
  if (x6 == 255) break;
  servo6.write(x6);
  delay(SAMPLE_DELAY);
  addr6++;
    if (addr6 == 512) break;
  }
  Serial.println("Done");
  servo1.detach();
  delay(250);  
}

void recordAllServos(uint8_t servo1Pin, uint8_t servo1FeedbackPin, uint8_t buttonPin) {
  uint16_t addr = 0;
  
  Serial.println("Recording");
  digitalWrite(ledPin, HIGH);
 
  pinMode(servo1FeedbackPin, INPUT); 
  pinMode(servo2FeedbackPin, INPUT); 
  pinMode(servo3FeedbackPin, INPUT); 
  pinMode(servo4FeedbackPin, INPUT); 
  pinMode(servo5FeedbackPin, INPUT); 
  pinMode(servo6FeedbackPin, INPUT); 

  while (digitalRead(buttonPin)) 
  {
     readAllServo(servo1FeedbackPin);
     readAllServo(servo2FeedbackPin);
     readAllServo(servo3FeedbackPin);
     readAllServo(servo4FeedbackPin);
     readAllServo(servo5FeedbackPin);
     readAllServo(servo6FeedbackPin);
    if (addr1 == 1024) break;
    delay(SAMPLE_DELAY);
  }
  if (addr1 != 1024) EEPROM.write(addr1, 255);
  if (addr2 != 1024) EEPROM.write(addr2, 255);
  if (addr3 != 1024) EEPROM.write(addr3, 255);
  if (addr4 != 1024) EEPROM.write(addr4, 255);
  if (addr5 != 1024) EEPROM.write(addr5, 255);
  if (addr6 != 1024) EEPROM.write(addr6, 255);
  digitalWrite(ledPin, LOW);
  
  Serial.println("Done");
  delay(250);
}

void readAllServo(uint8_t analogPin)
{
     int addr1 = 0;
     uint16_t a = analogRead(analogPin);
     
     Serial.print("Read analog pin "); Serial.print(servo1FeedbackPin); Serial.print(": "); Serial.print(a);
     if (a < CALIB_MIN) a = CALIB_MIN;
     if (a > CALIB_MAX) a = CALIB_MAX;
     a = map(a, CALIB_MIN, CALIB_MAX, 0, 254);
     Serial.print(" -> "); Serial.println(a);
     EEPROM.write(addr1, a); //save servo 1 values to eeprom
     addr1++;
     
     uint16_t b = analogRead(servo2FeedbackPin);
     Serial.print("Read analog pin2 "); Serial.print(servo2FeedbackPin); Serial.print(": "); Serial.print(b);
     if (b < CALIB_MIN) b = CALIB_MIN;
     if (b > CALIB_MAX) b = CALIB_MAX;
     b = map(b, CALIB_MIN, CALIB_MAX, 0, 254);
     Serial.print(" -> "); Serial.println(b);
     EEPROM.write(addr2, b); //save servo 2 values to eeprom
     addr2++;
     
     uint16_t c = analogRead(servo3FeedbackPin);
     Serial.print("Read analog pin3 "); Serial.print(servo3FeedbackPin); Serial.print(": "); Serial.print(c);
     if (c < CALIB_MIN) c = CALIB_MIN;
     if (c > CALIB_MAX) c = CALIB_MAX;
     c = map(c, CALIB_MIN, CALIB_MAX, 0, 254);
     Serial.print(" -> "); Serial.println(c);
     EEPROM.write(addr3, c); //save servo3 values to eeprom
     addr3++;
     
     uint16_t d = analogRead(servo4FeedbackPin);
     Serial.print("Read analog pin4 "); Serial.print(servo4FeedbackPin); Serial.print(": "); Serial.print(d);
     if (d < CALIB_MIN) d = CALIB_MIN;
     if (d > CALIB_MAX) d = CALIB_MAX;
     d = map(d, CALIB_MIN, CALIB_MAX, 0, 254);
     Serial.print(" -> "); Serial.println(d);
     EEPROM.write(addr4, d); //save servo4 values to eeprom
     addr4++;
     
     uint16_t e = analogRead(servo5FeedbackPin);
     Serial.print("Read analog pin3 "); Serial.print(servo5FeedbackPin); Serial.print(": "); Serial.print(c);
     if (c < CALIB_MIN) c = CALIB_MIN;
     if (c > CALIB_MAX) c = CALIB_MAX;
     b = map(c, CALIB_MIN, CALIB_MAX, 0, 254);
     Serial.print(" -> "); Serial.println(c);
     EEPROM.write(addr5, c); //save servo 5 values to eeprom
     addr3++;
     
     uint16_t f = analogRead(servo6FeedbackPin);
     Serial.print("Read analog pin3 "); Serial.print(servo6FeedbackPin); Serial.print(": "); Serial.print(c);
     if (f < CALIB_MIN) f = CALIB_MIN;
     if (f > CALIB_MAX) f = CALIB_MAX;
     b = map(c, CALIB_MIN, CALIB_MAX, 0, 254);
     Serial.print(" -> "); Serial.println(c);
     EEPROM.write(addr6, f); //save servo 6 values to eeprom
     addr6++;
}

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

Re: Teachable Robotic Arm

Post by adafruit_support_bill »

Not sure what you are trying to do here. ReadAllServos reads all 6 servos. Why do you call it 6 times from recordAllServos?

ReadAllServos won't work because all the addresses are pointing to the same place, all the servo data will be overwritten and you will only get one servo's worth of data.

You do not need 6 addresses. There is only 1 bank of EEPROM and you need to write to it sequentially. You only need one address ("addr") just as in the example code and increment it after every write.

Your playAllServo function has the same problem. Use only one addresss ("addr") and increment it after every read.

User avatar
mlandergan
 
Posts: 56
Joined: Sat Jul 23, 2011 6:15 pm

Re: Teachable Robotic Arm

Post by mlandergan »

ok and also when I am writing each servo their given values from addr, how do I know it is appropriate for each servo.

For example I have this code for void playAllServo

Code: Select all

uint8_t x = EEPROM.read(addr);
  Serial.print("Read EE Servo 1: "); Serial.print(x);
    if (x == 255) break;
    // map to 0-180 degrees
    x = map(x1, 0, 254, 0, 180);
    Serial.print(" -> "); Serial.println(x);
    servo1.write(x);
    addr+++;


So after this line of code should I repeat this code except do servo2.write and so on?

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

Re: Teachable Robotic Arm

Post by adafruit_support_bill »

ok and also when I am writing each servo their given values from addr, how do I know it is appropriate for each servo.
If you always start at zero and read them back in the same order you wrote them, then you will get the right value for each servo.
So after this line of code should I repeat this code except do servo2.write and so on?
Yes.

User avatar
mlandergan
 
Posts: 56
Joined: Sat Jul 23, 2011 6:15 pm

Re: Teachable Robotic Arm

Post by mlandergan »

Do I need to change the variable x for each servo or can I use the same one?

Thanks

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

Re: Teachable Robotic Arm

Post by adafruit_support_bill »

You can use the same one for all the servos.

User avatar
mlandergan
 
Posts: 56
Joined: Sat Jul 23, 2011 6:15 pm

Re: Teachable Robotic Arm

Post by mlandergan »

What am I doing wrong?

Code: Select all

//Code Produced 
//By Mark Edward Landergan
//Science Fair 2013
//Robotic Arm

#include <Servo.h>
#include <EEPROM.h>

#define CALIB_MAX 512
#define CALIB_MIN 100
#define SAMPLE_DELAY 25 // in ms, 50ms seems good

uint8_t recordButtonPin = 6;
uint8_t playButtonPin = 7;
uint8_t servo1Pin = 8;
uint8_t servo2Pin = 9;
uint8_t servo3Pin = 10;
uint8_t servo4Pin = 11;
uint8_t servo5Pin = 12;
uint8_t servo6Pin = 13;
uint8_t servo1FeedbackPin = A0;
uint8_t servo2FeedbackPin = A1;
uint8_t servo3FeedbackPin = A2;
uint8_t servo4FeedbackPin = A3;
uint8_t servo5FeedbackPin = A4;
uint8_t servo6FeedbackPin = A5;
uint8_t ledPin = 3;
int addr1, addr2, addr3, addr4, addr5, addr6 = 0;

Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
Servo servo5;
Servo servo6;
  
void setup() {
  Serial.begin(9600);
  pinMode(recordButtonPin, INPUT);
  digitalWrite(recordButtonPin, HIGH);
  pinMode(playButtonPin, INPUT);
  digitalWrite(playButtonPin, HIGH);
  pinMode(ledPin, OUTPUT);
  
  Serial.println("Servo RecordPlay");
}

void loop() {
 if (! digitalRead(recordButtonPin)) {
   delay(10);
   // wait for released
   while (! digitalRead(recordButtonPin));
   delay(20);
   // OK released!
   recordAllServos(servo1Pin, servo1FeedbackPin, recordButtonPin);
 }
 
  if (! digitalRead(playButtonPin)) {
   delay(10);
   // wait for released
   while (! digitalRead(playButtonPin));
   delay(20);
   // OK released!
   playAllServo(servo1Pin, playButtonPin);
 }
}

void playAllServo(uint8_t servo1Pin, uint8_t buttonPin) {
  Serial.println("Playing");
  servo1.attach(servo1Pin);
  servo2.attach(servo2Pin);
  servo3.attach(servo3Pin);
  servo4.attach(servo4Pin);
  servo5.attach(servo5Pin);
  servo5.attach(servo6Pin);
  while (digitalRead(buttonPin)) {    
  uint8_t x = EEPROM.read(addr);
  Serial.print("Read EE Servo 1: "); Serial.print(x);
    if (x == 255) break;
    // map to 0-180 degrees
    x = map(x1, 0, 254, 0, 180);
    Serial.print(" -> "); Serial.println(x);
    servo1.write(x);
    addr+++;
    
  uint8_t x = EEPROM.read(addr);
  Serial.print("Read EE Servo 2: "); Serial.print(x);
    if (x == 255) break;
    // map to 0-180 degrees
    x = map(x, 0, 254, 0, 180);
    Serial.print(" -> "); Serial.println(x);
  servo2.write(x);
    addr+++;  
    
      uint8_t x = EEPROM.read(addr);
  Serial.print("Read EE Servo 3: "); Serial.print(x);
    if (x == 255) break;
    // map to 0-180 degrees
    x = map(x, 0, 254, 0, 180);
    Serial.print(" -> "); Serial.println(x);
  servo3.write(x);
    addr+++;  
    
  uint8_t x = EEPROM.read(addr);
  Serial.print("Read EE Servo 4: "); Serial.print(x);
    if (x == 255) break;
    // map to 0-180 degrees
    x = map(x, 0, 254, 0, 180);
    Serial.print(" -> "); Serial.println(x);
  servo4.write(x);
    addr+++;  
    
  uint8_t x = EEPROM.read(addr);
  Serial.print("Read EE Servo 5: "); Serial.print(x);
    if (x == 255) break;
    // map to 0-180 degrees
    x = map(x, 0, 254, 0, 180);
    Serial.print(" -> "); Serial.println(x);
  servo5.write(x);
    addr+++;  
    
  uint8_t x = EEPROM.read(addr);
  Serial.print("Read EE Servo 6: "); Serial.print(x);
    if (x == 255) break;
    // map to 0-180 degrees
    x = map(x, 0, 254, 0, 180);
    Serial.print(" -> "); Serial.println(x);
  servo6.write(x);
    addr+++;  
    
  }
  Serial.println("Done");
  servo1.detach();
  delay(250);  
}

void recordAllServos(uint8_t servo1Pin, uint8_t servo1FeedbackPin, uint8_t buttonPin) {
  uint16_t addr = 0;
  
  Serial.println("Recording");
  digitalWrite(ledPin, HIGH);
 
  pinMode(servo1FeedbackPin, INPUT); 
  pinMode(servo2FeedbackPin, INPUT); 
  pinMode(servo3FeedbackPin, INPUT); 
  pinMode(servo4FeedbackPin, INPUT); 
  pinMode(servo5FeedbackPin, INPUT); 
  pinMode(servo6FeedbackPin, INPUT); 

  while (digitalRead(buttonPin)) 
  {
     readAllServo(servo1FeedbackPin);
     readAllServo(servo2FeedbackPin);
     readAllServo(servo3FeedbackPin);
     readAllServo(servo4FeedbackPin);
     readAllServo(servo5FeedbackPin);
     readAllServo(servo6FeedbackPin);
    if (addr == 1024) break;
    delay(SAMPLE_DELAY);
  }
  if (addr != 1024) EEPROM.write(addr, 255);
  digitalWrite(ledPin, LOW);
  
  Serial.println("Done");
  delay(250);
}

void readAllServo(uint8_t servo1FeedbackPin)
{
     
     uint16_t a = analogRead(servo1FeedbackPin);s 
     Serial.print("Read analog pin "); Serial.print(servo1FeedbackPin); Serial.print(": "); Serial.print(a);
     if (a < CALIB_MIN) a = CALIB_MIN;
     if (a > CALIB_MAX) a = CALIB_MAX;
     a = map(a, CALIB_MIN, CALIB_MAX, 0, 254);
     Serial.print(" -> "); Serial.println(a);
     EEPROM.write(addr, a); //save servo 1 values to eeprom
     addr++
}

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

Return to “Arduino”