Motion activated lights flicker; don't want them to

I’m trying to make motion activated lights for my longboard, and I’ve got it most of the way there. I don’t know much about arduino or coding so this is mostly copy and paste from a few other projects I’ve seen to make it work for mine. The problem I’m having is every time the accelerometer checks for motion, the lights flash off if there is still continous motion. I want them to be always on if there is motion and only turn off if the board has been still for a few seconds. Any thoughts on what I should do?

Here is my code:

#include <Adafruit_NeoPixel.h>
#include <Wire.h>

#define PIN 2
#define NUMPIXELS 62

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_RGB + NEO_KHZ800);
int delayval = 5000;
int stillTime = 5000;
bool blinkState = 0;
bool running = false;
bool myOutput = false;
unsigned long loopTime = 0;          // get the time since program started
unsigned long interruptsTime = 0;    // get the time when motion event is detected
const int MPU_ADDR = 0x68; // I2C address of the MPU-6050. If AD0 pin is set to HIGH, the I2C address will be 0x69.
int16_t accelerometer_x, accelerometer_y, accelerometer_z; // variables for accelerometer raw data
int16_t gyro_x, gyro_y, gyro_z; // variables for gyro raw data
int16_t temperature; // variables for temperature data
char tmp_str[7]; // temporary variable used in convert function
char* convert_int16_to_str(int16_t i) { // converts int16 to string. Moreover, resulting strings will have the same length in the debug monitor.
  sprintf(tmp_str, "%6d", i);
  return tmp_str;

void setup() {


  pinMode(PIN, OUTPUT);
  digitalWrite(PIN, HIGH);
  digitalWrite(PIN, LOW);
  Wire.beginTransmission(MPU_ADDR); // Begins a transmission to the I2C slave (GY-521 board)
  Wire.write(0x6B); // PWR_MGMT_1 register
  Wire.write(0); // set to zero (wakes up the MPU-6050)

void loop() {
   for (int i = 0; i < NUMPIXELS; i++) {
      pixels.setPixelColor(i, pixels.Color(0, 0, 0));;
    // This one is breadboard facing left (color should be green)
  if (accelerometer_x > -9000 && accelerometer_x < 9000) {
    for (int i = 0; i < 13; i++) {
      pixels.setPixelColor(i, pixels.Color(0, 150, 0));;
    for (int i = 13; i < 31; i++) {
      pixels.setPixelColor(i, pixels.Color(150, 0, 150));;
    for (int i = 31; i < 45; i++) {
      pixels.setPixelColor(i, pixels.Color(85, 85, 86));;
    for (int i = 45; i < 62; i++) {
      pixels.setPixelColor(i, pixels.Color(150, 0, 150));;


  Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H) [MPU-6000 and MPU-6050 Register Map and Descriptions Revision 4.2, p.40]
  Wire.endTransmission(false); // the parameter indicates that the Arduino will send a restart. As a result, the connection is kept active.
  Wire.requestFrom(MPU_ADDR, 7 * 2, true); // request a total of 7*2=14 registers

  // "<<8 |;" means two registers are read and stored in the same variable
  accelerometer_x = << 8 |; // reading registers: 0x3B (ACCEL_XOUT_H) and 0x3C (ACCEL_XOUT_L)
  accelerometer_y = << 8 |; // reading registers: 0x3D (ACCEL_YOUT_H) and 0x3E (ACCEL_YOUT_L)
  accelerometer_z = << 8 |; // reading registers: 0x3F (ACCEL_ZOUT_H) and 0x40 (ACCEL_ZOUT_L)
  temperature = << 8 |; // reading registers: 0x41 (TEMP_OUT_H) and 0x42 (TEMP_OUT_L)
  gyro_x = << 8 |; // reading registers: 0x43 (GYRO_XOUT_H) and 0x44 (GYRO_XOUT_L)
  gyro_y = << 8 |; // reading registers: 0x45 (GYRO_YOUT_H) and 0x46 (GYRO_YOUT_L)
  gyro_z = << 8 |; // reading registers: 0x47 (GYRO_ZOUT_H) and 0x48 (GYRO_ZOUT_L)

  // print out data
  Serial.print("aX = "); Serial.print(convert_int16_to_str(accelerometer_x));
  Serial.print(" | aY = "); Serial.print(convert_int16_to_str(accelerometer_y));
  Serial.print(" | aZ = "); Serial.print(convert_int16_to_str(accelerometer_z));
  // the following equation was taken from the documentation [MPU-6000/MPU-6050 Register Map and Description, p.30]
  Serial.print(" | tmp = "); Serial.print(temperature / 340.00 + 36.53);
  Serial.print(" | gX = "); Serial.print(convert_int16_to_str(gyro_x));
  Serial.print(" | gY = "); Serial.print(convert_int16_to_str(gyro_y));
  Serial.print(" | gZ = "); Serial.print(convert_int16_to_str(gyro_z));


The problem I'm having is every time the accelerometer checks for motion, the lights flash off if there is still continous motion.

Every time through the loop function the LEDs have data written to them to turn off all the LEDs. This is bad as you only want to send data when anything changes. What makes this even worse is that every time you change one LED you bang out the data to set them all.

So you need an if statement to make sure you only refresh when there is new data, and move the show method call to after the for loop has complected. This point applies to every time you try and update the LEDs.


should only be used once in the setup function, and as you are having the Arduino as a master and not a slave I2C device it should be:-


so this is mostly copy and paste from a few other projects

Do not copy and paste anything from a project that

  1. You don't understand what it does.
  2. Is a crap project full of mistakes anyway. I know this is hard because there is more crap out there than there is quality. Which is why 1) is so important.