Go Down

Topic: Make an acceleromer control a servo. (Read 2941 times) previous topic - next topic

roboticfan101

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

http://www.youtube.com/watch?v=flM003C5PIw&feature=related

johnwasser

The ADXL330 has voltage outputs so you connect it to an analog input.
Code: [Select]

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

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


roboticfan101

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: [Select]
myServo.write(angle);

johnwasser


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: [Select]
myServo.write(angle);


'angle' is the name if an integer variable, calculated from the acceleration:
Code: [Select]

    int accel = analogRead(ADXLpin);
    int angle = map(accel, 0, 1023, 0, 180);
    myServo.write(angle);
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

roboticfan101

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! :D

johnwasser

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: [Select]

    int accel = pulseIn(MemsicPin);   
    int angle = map(accel, 3750, 6250, 0, 180);  // map -1G through +1G to 0 through 180
    myServo.write(angle);
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

roboticfan101

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

Go Up