Go Down

Topic: Using an accelerometer to orient a model rocket in light via control surfaces (Read 2171 times) previous topic - next topic


I have attempted to recreate guiding a model rocket
Based on the linked project shown in Hack a day
I am not able to recreate their results using another accelerometer (mma 7361)

link to their code download

The more I examine the changes I made the more confused I get.

What I'm looking for is:
Any tips to problem areas in my code.
In addition I can display the values from the MMA 7361 successfully however I question if I'm getting accurate data.

MY modified code, uploads fine but does not zero the rockets position nor does it compensate,
Code: [Select]
#include <Servo.h>

int xaccPin = 1;
int yaccPin = 2;
long xacc = 0;
long yacc = 0;
int anglex;
int angley;
int servoposx, servoposy;
int duration, inches;
long xacc1 = 0;
long yacc1 = 0;
long xacc2 = 0;
long yacc2 = 0;
long xacc3 = 0;
long yacc3 = 0;
long xaccs = 0;
long yaccs = 0;
long xaccd = 0;
long yaccd = 0;
int counter = 0;
Servo x1servo;
Servo x2servo;
Servo y1servo;
Servo y2servo;

void setup()
  pinMode(xaccPin, INPUT);
  pinMode(yaccPin, INPUT);
  if (counter <= 1)
      // starting values
      xacc1 = readAcceleration(analogRead(xaccPin));
      yacc1 = readAcceleration(analogRead(yaccPin));
      xacc2 = readAcceleration(analogRead(xaccPin));
      yacc2 = readAcceleration(analogRead(yaccPin));
      xacc3 = readAcceleration(analogRead(xaccPin));
      yacc3 = readAcceleration(analogRead(yaccPin)); 
      // stable position?
      xaccs = (xacc1 + xacc2 + xacc3)/3;
      yaccs = (yacc1 + yacc2 + yacc3)/3;

void loop()
  xacc = readAcceleration(analogRead(xaccPin));
  yacc = readAcceleration(analogRead(yaccPin));
  xaccd = xacc - xaccs;
  yaccd = yacc - yaccs;
  anglex = map(xaccd, 280, -280, -90, 90);
  angley = map(yaccd, 280, -280, -90, 90);
  servoposx = map(anglex, -90, 90, 20, 160);
  servoposy = map(angley, -90, 90, 20, 160);
  x2servo.write(160 - servoposx);
  y2servo.write(160 - servoposy);

// Uncomment for testing purposes
  Serial.print("X1: ");
  Serial.print("xacc:  ");
  //Serial.print("  X2: ");
  //Serial.print(analogRead(160 - xaccPin));
  Serial.print("Y1: ");
  //Serial.print("  Y2: ");
  //Serial.print(analogRead(160 - yaccPin));


long readAcceleration(int axe)
  int count = 0;
  long accel = 0;
  int value = 0;
  value = analogRead(axe);
  while(value == HIGH)
     value = analogRead(axe);
  while(value == LOW)
    value = analogRead(axe);
  while(value == HIGH)
    value = analogRead(axe);
    count = count + 1;
  //accel = abs(8 * (count * 18 / 10 - 500)); //this was the original function
  accel = count;
  return accel;

This is the code I use to view the accelerometer values

Code: [Select]
#include <LiquidCrystal.h>

LiquidCrystal lcd(8,9,4,5,6,7);

int backLight = 13;

const int xPin = 1;                   // x-axis
const int yPin = 2;                   // y-axis
const int zPin = 3;                   // z-axis

void setup()
  pinMode(backLight, OUTPUT);
  digitalWrite(backLight, HIGH);

void loop()

  //Uncomment the lines below for Serial output
  //Comment if not using a LCD.
  lcd.print("X= ");
  lcd.print("Y= ");
  lcd.print("Z= ");

at rest correctly oriented I get x 303-310 y 315-320 z 336

the accelerometer is connected to 5v


I have attempted to recreate guiding a model rocket

You may want to be very, very careful with what you are doing and saying in a public forum. I don't know where you live, but in many cases the government does not look kindly on people building their own guided missiles.

Model rocketry (even high power model rocketry) has many limitations on it before you run into things needing licenses, permits, certifications, etc - and one of those tends to be "adding active control surfaces".

Depending on your luck and your location - nothing may happen, or you may face big fines - if you're lucky.

I'm just putting this out there to warn you and others; some governments consider adding active control surfaces (especially "intelligent" ones) to a rocket to be a step towards weapon systems creation. You may not think so; you may think such rules as restrictive - but your government may not care one little bit. Should they decide to pursue it, they will grind you into dust. Fair warning, ok...?

I will not respond to Arduino help PM's from random forum users; if you have such a question, start a new topic thread.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131