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.
joeschmaus
 
Posts: 10
Joined: Thu Jan 23, 2014 4:02 pm

Feedback Servo Kit

Post by joeschmaus »

Whoopsy
Last edited by joeschmaus on Fri Feb 07, 2014 12:54 am, edited 1 time in total.

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

Re: Feedback Servo Kit

Post by adafruit_support_bill »

You should be able to do that using the hardware ordered and minimal code changes. It should be possible to do something similar on the Trinket. But you will need to restructure the code a bit drive servos with the Trinket. library: http://learn.adafruit.com/trinket-gemma ... l/overview

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

Re: Feedback Servo Kit

Post by joeschmaus »

:lol:
Last edited by joeschmaus on Fri Feb 07, 2014 12:56 am, edited 1 time in total.

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

Re: Feedback Servo Kit

Post by adafruit_support_bill »

To do what you describe should be a simple change. We can help you with that here in the forums.

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

Re: Feedback Servo Kit

Post by joeschmaus »

Great!!
Last edited by joeschmaus on Fri Feb 07, 2014 12:57 am, edited 1 time in total.

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

Re: Feedback Servo Kit

Post by adafruit_support_bill »

This should do it:

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 = A0;
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 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);
}

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

Re: Feedback Servo Kit

Post by joeschmaus »

Sorry
Last edited by joeschmaus on Fri Feb 07, 2014 12:59 am, edited 1 time in total.

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

Re: Feedback Servo Kit

Post by adafruit_support_bill »

That will start the playback when you turn on the replay switch and continue until you turn the power off or press the reset button. You can add a power switch anywhere between your power supply and the Arduino. We have some in-line switches here. http://www.adafruit.com/products/1125

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

Re: Feedback Servo Kit

Post by joeschmaus »

Okay great!!!
Last edited by joeschmaus on Fri Feb 07, 2014 1:00 am, edited 1 time in total.

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

Re: Feedback Servo Kit

Post by adafruit_support_bill »

No. That code will not work on the Trinket.

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

Re: Feedback Servo Kit

Post by joeschmaus »

Oh, that is too bad :( but regardless, this little project has been a ton of fun for the kids and none of it would be possible without your help. I have 6 children all under the age of 10, five boys and one girl, my goal is to get them interested in inventing and building rather than video gaming. They have thus far shown a ton of interest and are anxious to learn more. Thank you so much for help thus far.

Can you update the code you supplied me for the addition of a 10 kΩ variable resistor (pot) and provide me with a layout print for the wiring of the pot relative to the kit I purchased?? What I am trying to accomplish with this is that when the reset button is pressed, the pot can then be used to control the servo.

Thank again,

Joe

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

Re: Feedback Servo Kit

Post by adafruit_support_bill »

I have 6 children all under the age of 10, five boys and one girl, my goal is to get them interested in inventing and building rather than video gaming. They have thus far shown a ton of interest and are anxious to learn more.
Awesome!
Can you update the code you supplied me for the addition of a 10 kΩ variable resistor (pot) and provide me with a layout print for the wiring of the pot relative to the kit I purchased?? What I am trying to accomplish with this is that when the reset button is pressed, the pot can then be used to control the servo.
There is a tutorial here with a diagram that shows you how to hook up a port for controlling a servo: http://arduino.cc/en/Tutorial/Knob
I can take a look at the code, but I have a couple of tutorials to finish up first.

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

Re: Feedback Servo Kit

Post by joeschmaus »

Thanks for the compliment and thanks for your willingness to help. Please take a look at the code when you have time and get back to me. Would I wire things up as shown in tutorial even though I am using the same feedback servo? Thanks again!!

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

Re: Feedback Servo Kit

Post by adafruit_support_bill »

Both examples are using analog pin A0 for input. You will have to move one of them to a different analog pin such as A1. This is easy to change in the code:

Code: Select all

uint8_t recordButtonPin = 12;
uint8_t playButtonPin = 7;
uint8_t servoPin = 9;
uint8_t feedbackPin = A0;  ///<<------------------ Change this line to match the pin you use.
uint8_t ledPin = 13;

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

Re: Feedback Servo Kit

Post by joeschmaus »

Wow
Last edited by joeschmaus on Fri Feb 07, 2014 1:02 am, edited 1 time in total.

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

Return to “Microcontrollers”