Feedback Servo Kit

For Adafruit customers who seek help with microcontrollers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
adafruit_support_bill
 
Posts: 88087
Joined: Sat Feb 07, 2009 10:11 am

Re: Feedback Servo Kit

Post by adafruit_support_bill »

No, that just allows you to run the record/playback sketch using a different analog pin. Combining the two functions is a little more involved.

joeschmaus
 
Posts: 10
Joined: Thu Jan 23, 2014 4:02 pm

Re: Feedback Servo Kit

Post by joeschmaus »

:?:

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

Re: Feedback Servo Kit

Post by adafruit_support_bill »

Did you have a specific question? As I said earlier. I have a couple other tasks to complete before I can get to the code. Writing code for people is not really part of our job here.

joeschmaus
 
Posts: 10
Joined: Thu Jan 23, 2014 4:02 pm

Re: Feedback Servo Kit

Post by joeschmaus »

Sorry, for some reason the chat did not refresh. Please take a look into this when you have time.

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

Re: Feedback Servo Kit

Post by adafruit_support_bill »

Here you go. This should have the servo follow the pot at startup - until you hit one of the buttons.

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 servoPin = 9;
uint8_t feedbackPin = A1;  // Connect servo feedback to this pin
int potpin = A0;  // connect pot to this pin
uint8_t ledPin = 13;

Servo myServo;  
  
void setup() {
  Serial.begin(9600);
  pinMode(recordButtonPin, INPUT);
  digitalWrite(recordButtonPin, HIGH);
  pinMode(playButtonPin, INPUT);
  digitalWrite(playButtonPin, HIGH);
  pinMode(ledPin, OUTPUT);
  
  Serial.println("Servo Pot-Control");
  myServo.attach(servoPin);
  
  while ((digitalRead(recordButtonPin) && digitalRead(playButtonPin)))
  {
     int val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023) 
     val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180) 
     myServo.write(val);                  // sets the servo position according to the scaled value 
     delay(15);  
  }
  
  myServo.detach();
  Serial.println("Servo RecordPlay");
}

void loop() {
 if (! digitalRead(recordButtonPin)) {
   delay(10);
   // wait for released
   while (! digitalRead(recordButtonPin));
   delay(20);
   // OK released!
   recordServo(servoPin, feedbackPin, 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");


  myServo.attach(servoPin);
  while (digitalRead(buttonPin)) {    
    uint8_t x = EEPROM.read(addr);
    Serial.print("Read EE: "); Serial.print(x);
    if (x == 255) addr = 0; // this line resets to the beginning aqt the end of the recorded data
   else
   {
      // 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) addr = 0;  // this line resets to the beginning at the end of EEPROM
   }
  }
  Serial.println("Done");
  myServo.detach();
  delay(250);  
}

void recordServo(uint8_t servoPin, uint8_t analogPin, uint8_t buttonPin) {
  uint16_t addr = 0;
  
  Serial.println("Recording");
  digitalWrite(ledPin, HIGH);
  


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

  digitalWrite(ledPin, LOW);

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

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

Return to “Microcontrollers”