Pages: [1]   Go Down
 Author Topic: Make an acceleromer control a servo.  (Read 2720 times) 0 Members and 1 Guest are viewing this topic.
Offline
Jr. Member
Karma: 1
Posts: 51
 « on: October 02, 2012, 08:00:20 pm » Bigger Smaller Reset

Can any one write the code that this guy used to make his servo react the way it does. Thank you!

 Logged

Massachusetts, USA
Offline
Tesla Member
Karma: 201
Posts: 8667
 « Reply #1 on: October 02, 2012, 08:34:25 pm » Bigger Smaller Reset

Code:
#include <Servo.h>
const int servoPin = 9;

Servo myServo;

void setup() {myServo.attach(servoPin);}

void loop()
{
int angle = map(accel, 0, 1023, 0, 180);
myServo.write(angle);
}
 Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline
Jr. Member
Karma: 1
Posts: 51
 « Reply #2 on: October 05, 2012, 07:02:18 pm » Bigger Smaller Reset

THANK YOU SO MUCH!!!!
 Logged

Offline
Jr. Member
Karma: 1
Posts: 51
 « Reply #3 on: October 05, 2012, 07:09:37 pm » Bigger Smaller Reset

Does anything have to go in the parenthesis with the word angle in it? I have an odd feeling like there should be an angle measure there.
Code:
myServo.write(angle);
 Logged

Massachusetts, USA
Offline
Tesla Member
Karma: 201
Posts: 8667
 « Reply #4 on: October 05, 2012, 11:53:16 pm » Bigger Smaller Reset

Does anything have to go in the parenthesis with the word angle in it? I have an odd feeling like there should be an angle measure there.
Code:
myServo.write(angle);

'angle' is the name if an integer variable, calculated from the acceleration:
Code:
int angle = map(accel, 0, 1023, 0, 180);
myServo.write(angle);
 Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline
Jr. Member
Karma: 1
Posts: 51
 « Reply #5 on: November 12, 2012, 10:02:39 pm » Bigger Smaller Reset

Hi again! I have a new question. Would this code be compatible with a Mesmic 2125 accelerometer? Can you explain if any the difference? Thanks a lot! I've learned a lot from asking questions of this forum. You guys are great!
 Logged

Massachusetts, USA
Offline
Tesla Member
Karma: 201
Posts: 8667
 « Reply #6 on: November 13, 2012, 08:34:53 am » Bigger Smaller Reset

The Mesmic 2125 accelerometer (http://www.parallax.com/Portals/0/Downloads/docs/prod/acc/memsickit.pdf) outputs a variable-width pulse rather than an analog voltage.  At 0G the output pulse is 5000 microseconds high and each 1G would be a change of 1250 microseconds. A pulse of 3750 microseconds would be -1G and 6250 microseconds would be +1G. The code for that would be something like this:

Code:
int accel = pulseIn(MemsicPin);
int angle = map(accel, 3750, 6250, 0, 180);  // map -1G through +1G to 0 through 180
myServo.write(angle);
 Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline
Jr. Member
Karma: 1
Posts: 51
 « Reply #7 on: November 14, 2012, 12:33:12 am » Bigger Smaller Reset

Oh, I see! That's so cool! Thanks again, you rock!
 Logged

 Pages: [1]   Go Up