Pages: [1]   Go Down
Author Topic: Make an acceleromer control a servo.  (Read 2787 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 1
Posts: 51
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8856
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The ADXL330 has voltage outputs so you connect it to an analog input.
Code:
#include <Servo.h>
const int servoPin = 9;
const in ADXLpin = A0;

Servo myServo;

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

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

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

Offline Offline
Jr. Member
**
Karma: 1
Posts: 51
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

THANK YOU SO MUCH!!!!
Logged

Offline Offline
Jr. Member
**
Karma: 1
Posts: 51
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Tesla Member
***
Karma: 208
Posts: 8856
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 accel = analogRead(ADXLpin);
    int angle = map(accel, 0, 1023, 0, 180);
    myServo.write(angle);
Logged

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

Offline Offline
Jr. Member
**
Karma: 1
Posts: 51
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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! smiley-grin
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8856
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Jr. Member
**
Karma: 1
Posts: 51
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: