Feedback Servo Kit
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
-
- Posts: 10
- Joined: Thu Jan 23, 2014 4:02 pm
Feedback Servo Kit
Whoopsy
Last edited by joeschmaus on Fri Feb 07, 2014 12:54 am, edited 1 time in total.
- adafruit_support_bill
- Posts: 88154
- Joined: Sat Feb 07, 2009 10:11 am
Re: Feedback Servo Kit
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
-
- Posts: 10
- Joined: Thu Jan 23, 2014 4:02 pm
Re: Feedback Servo Kit
Last edited by joeschmaus on Fri Feb 07, 2014 12:56 am, edited 1 time in total.
- adafruit_support_bill
- Posts: 88154
- Joined: Sat Feb 07, 2009 10:11 am
Re: Feedback Servo Kit
To do what you describe should be a simple change. We can help you with that here in the forums.
-
- Posts: 10
- Joined: Thu Jan 23, 2014 4:02 pm
Re: Feedback Servo Kit
Great!!
Last edited by joeschmaus on Fri Feb 07, 2014 12:57 am, edited 1 time in total.
- adafruit_support_bill
- Posts: 88154
- Joined: Sat Feb 07, 2009 10:11 am
Re: Feedback Servo Kit
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);
}
-
- Posts: 10
- Joined: Thu Jan 23, 2014 4:02 pm
Re: Feedback Servo Kit
Sorry
Last edited by joeschmaus on Fri Feb 07, 2014 12:59 am, edited 1 time in total.
- adafruit_support_bill
- Posts: 88154
- Joined: Sat Feb 07, 2009 10:11 am
Re: Feedback Servo Kit
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
-
- Posts: 10
- Joined: Thu Jan 23, 2014 4:02 pm
Re: Feedback Servo Kit
Okay great!!!
Last edited by joeschmaus on Fri Feb 07, 2014 1:00 am, edited 1 time in total.
- adafruit_support_bill
- Posts: 88154
- Joined: Sat Feb 07, 2009 10:11 am
Re: Feedback Servo Kit
No. That code will not work on the Trinket.
-
- Posts: 10
- Joined: Thu Jan 23, 2014 4:02 pm
Re: Feedback Servo Kit
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
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
- adafruit_support_bill
- Posts: 88154
- Joined: Sat Feb 07, 2009 10:11 am
Re: Feedback Servo Kit
Awesome!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.
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/KnobCan 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.
I can take a look at the code, but I have a couple of tutorials to finish up first.
-
- Posts: 10
- Joined: Thu Jan 23, 2014 4:02 pm
Re: Feedback Servo Kit
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!!
- adafruit_support_bill
- Posts: 88154
- Joined: Sat Feb 07, 2009 10:11 am
Re: Feedback Servo Kit
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;
-
- Posts: 10
- Joined: Thu Jan 23, 2014 4:02 pm
Re: Feedback Servo Kit
Wow
Last edited by joeschmaus on Fri Feb 07, 2014 1:02 am, edited 1 time in total.
Please be positive and constructive with your questions and comments.