arduino + accelerometer + dc motor.

Hi all.

im using an accelerometer (memsic 2125) connected to the arduino and connected to a dc motor by a syren motor driver.

heres my code:

#include <Servo.h>
#define OUTPUT_PIN 9
#define PULSE_MIN 1000
#define PULSE_MAX 2000

Servo m_controller;
int xaccPin = 2;
int yaccPin = 3;

void setup()
{
   Serial.begin(9600);
  pinMode (xaccPin, INPUT);
  pinMode (yaccPin, INPUT);
  m_controller.attach(OUTPUT_PIN, PULSE_MIN, PULSE_MAX);
}


void loop()
{
  int pulseX, pulseY;
  int accelerationX, accelerationY;
  pulseX = pulseIn(xaccPin, HIGH);
  pulseY = pulseIn(yaccPin, HIGH);
  accelerationX = ((pulseX/10)-500)*8;
  accelerationY = ((pulseY/10)-500)*8;
  Serial.print(accelerationX);
  Serial.print("\t");
  Serial.print(accelerationY);
  Serial.println();
  delay(100);
  if(accelerationY > -700 && accelerationY < 0){
    while (accelerationY < 10){ 
        m_controller.write(180);
        int  pulseY;
        int  accelerationY;

        pulseY = pulseIn(yaccPin, HIGH);

        accelerationY = ((pulseY/10)-500)*8;
 
        Serial.print(accelerationY);
        Serial.println();
        delay(500);
      if(accelerationY >= 0){
      m_controller.write (90);}
    }
  if(accelerationY >= 0 && accelerationY < 200){        
    while (accelerationY < 200){
      m_controller.write(90);
      int pulseX, pulseY;
      int accelerationX, accelerationY;
      pulseX = pulseIn(xaccPin, HIGH);
      pulseY = pulseIn(yaccPin, HIGH);
      accelerationX = ((pulseX/10)-500)*8;
      accelerationY = ((pulseY/10)-500)*8;
      Serial.print(accelerationX);
      Serial.print("\t");
      Serial.print(accelerationY);
      Serial.println();
      delay(500);}
     
    while (accelerationY < 500){
      m_controller.write(180); 
      int pulseX, pulseY;
      int accelerationX, accelerationY;
      pulseX = pulseIn(xaccPin, HIGH);
      pulseY = pulseIn(yaccPin, HIGH);
      accelerationX = ((pulseX/10)-500)*8;
      accelerationY = ((pulseY/10)-500)*8;
      Serial.print(accelerationX);
      Serial.print("\t");
      Serial.print(accelerationY);
      Serial.println();
      delay(500);}
      
    if(accelerationY > 400){
      while (accelerationY > - 700){
            m_controller.write(0); 
            int pulseX, pulseY;
            int accelerationX, accelerationY;
            pulseX = pulseIn(xaccPin, HIGH);
            pulseY = pulseIn(yaccPin, HIGH);
            accelerationX = ((pulseX/10)-500)*8;
            accelerationY = ((pulseY/10)-500)*8;
            Serial.print(accelerationX);
            Serial.print("\t");
            Serial.print(accelerationY);
            Serial.println();
            delay(500);}
    }
  }
  }
}

it results in the motor running in the forward direction forever until i off the battery.

i want the motor to stop at a certain point. (when accelerationY >= 0)

help!

sorry its a bit messy and redundant, but i need all the help i can get…

You appear to keep redefining things:- int pulseX, pulseY; int accelerationX, accelerationY; several times in the loop().

You should only define a variable once at the start of the loop, or if you want to keep the value from one time round the loop to the next then define the variables outside the loop and make them global.

What do you get from the print out?

gotcha mike.

the print out reads the tilt acceleration of the accelerometer. at different tilt positions there are different readings.

i have the motor running at the 1st while loop but it doesnt seem to come out of the loop after i've tilt the sensor past 0.

it doesnt seem to come out of the loop after i’ve tilt the sensor past 0.

In the code, you have this:

void loop()
{
  int pulseX, pulseY;
  int accelerationX, [glow]accelerationY[/glow];
  pulseX = pulseIn(xaccPin, HIGH);
  pulseY = pulseIn(yaccPin, HIGH);
  accelerationX = ((pulseX/10)-500)*8;
  accelerationY = ((pulseY/10)-500)*8;
  Serial.print(accelerationX);
  Serial.print("\t");
  Serial.print(accelerationY);
  Serial.println();
  delay(100);
  if(accelerationY > -700 && accelerationY < 0){
    while ([glow]accelerationY [/glow]< 10){
        m_controller.write(180);
        int  pulseY;
        int  [glow]accelerationY[/glow];

        pulseY = pulseIn(yaccPin, HIGH);

        [glow]accelerationY [/glow]= ((pulseY/10)-500)*8;

The first two highlighted accelerationY occurrences refer to one location in memory.

The second two occurrences refer to another location in memory.

Inside the while loop, you are not changing the memory location that the while loop is monitoring, so the while loop keeps running.

hi thanks for the replies.

how do i make them refer to the same memory? :frowning:

how do i make them refer to the same memory

Just declare them once.