I'm trying to use an Adafruit MMA8451 accelerometer to create a pedometer-type device that counts steps. I'd like to initiate a counter that says "if the x-axis readout is > some threshold, add 1 to the counter" so that once the counter reaches a certain point, I'll be able to trigger a separate event (like an LED turning on). I'm an Arduino beginner, and would appreciate any pointers in the right direction as I work through this.

I'm using the Sparkfun Redboard and the Adafruit MMA8451, and wrote a loop to check whether the x-axis readout was over 5. If it's over 5, I want it to add 1 to the accumulator. However, it's accumulating 1 each readout regardless of what the 'x' reading actually is. Can anyone tell what I'm doing wrong? A nudge in the right direction would be much appreciated.

Code is here:

int lastStepCount = 0;
int accum = 0;

#include <Wire.h>
#include <Adafruit_MMA8451.h>
#include <Adafruit_Sensor.h>

Adafruit_MMA8451 mma = Adafruit_MMA8451();

void setup(void) {

  Serial.println("Adafruit MMA8451 test!");

  if (! mma.begin()) {
    Serial.println("Couldnt start");
    while (1);
  Serial.println("MMA8451 found!");


  Serial.print("Range = "); Serial.print(2 << mma.getRange());


void loop() {
  // Read the 'raw' data in 14-bit counts
  Serial.print("X:\t"); Serial.print(mma.x);
  Serial.print("\tY:\t"); Serial.print(mma.y);
  Serial.print("\tZ:\t"); Serial.print(mma.z);

  /* Get a new sensor event */
  sensors_event_t event;

  /* Display the results (acceleration is measured in m/s^2) */
  Serial.print("X: \t"); Serial.print(event.acceleration.x); Serial.print("\t");
  Serial.print("Y: \t"); Serial.print(event.acceleration.y); Serial.print("\t");
  Serial.print("Z: \t"); Serial.print(event.acceleration.z); Serial.print("\t");
  Serial.println("m/s^2 ");

  /* initiate for loop to count movement— this is the piece I'm having trouble with */
  if (event.acceleration.x >= 5);
    accum = accum + 1;
  Serial.print("X Counter: "); Serial.print(accum);

  /* Get the orientation of the sensor */
  uint8_t o = mma.getOrientation();

  switch (o) {
    case MMA8451_PL_PUF:
      Serial.println("Portrait Up Front");
    case MMA8451_PL_PUB:
      Serial.println("Portrait Up Back");
    case MMA8451_PL_PDF:
      Serial.println("Portrait Down Front");
    case MMA8451_PL_PDB:
      Serial.println("Portrait Down Back");
    case MMA8451_PL_LRF:
      Serial.println("Landscape Right Front");
    case MMA8451_PL_LRB:
      Serial.println("Landscape Right Back");
    case MMA8451_PL_LLF:
      Serial.println("Landscape Left Front");
    case MMA8451_PL_LLB:
      Serial.println("Landscape Left Back");


**Update: the output I'm getting is as follows. The "X Counter" accumulates even when the x-reading is well below 5...not sure what's going on:

X:  1Adafruit MMA8451 test!
MMA8451 found!
Range = 2G
X:  144 Y:  50  Z:  4052
X:  0.34    Y:  0.11    Z:  9.66    m/s^2 
X Counter: 1Portrait Up Front

X:  138 Y:  62  Z:  4052
X:  0.34    Y:  0.11    Z:  9.70    m/s^2 
X Counter: 2Portrait Up Front

X:  0   Y:  48  Z:  4040
X:  -0.02   Y:  0.11    Z:  9.67    m/s^2 
X Counter: 3Portrait Up Front

X:  -780    Y:  106 Z:  3814
X:  -1.80   Y:  0.25    Z:  9.11    m/s^2 
X Counter: 4Portrait Up Front

X:  -510    Y:  106 Z:  4008
X:  -1.46   Y:  0.20    Z:  9.68    m/s^2 
  • What is your output? – Sniper Dec 10 '16 at 17:13
  • Voting to close because it's a typing mistake / syntax error, and user has not returned since asking the question. – VE7JRO Feb 14 at 17:57

Change your if statement - remove ; (semicolon) from the end.

Use this:

if (event.acceleration.x >= 5)
accum = accum + 1;

Notice there is no semicolon after if statement

  • 1
    Mark as answer if it helped you. – Sniper Dec 10 '16 at 17:22
  • The OP has not returned since asking the question, so I doubt it will ever be accepted. +1 for your answer. – VE7JRO Feb 14 at 18:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.