Nano accelerometer project stops working

A few years back I wired an acelerometer to a buzzer making different tones depending on movement and speed.

I recently wired this project to a nano. It works and beeps to the movement for a minute or so then stops and has to be rebooted.

It is powered by 9v. The lights are still on the nano and accelerometer when it stops functioning.

I tried using the serial monitor to see if it was throwing any errors when it stops working but none.

How could I go about troubleshooting this?

I suggest to use a multimeter to measure voltages at various points and see if they are as expected.

Don't expect a 9V block battery to last long powering a nano, even if fresh.

It's actually a 9v adapter that plugs into a 12v auto cigarette lighter plug. I've also had a laptop and the 9v hooked up at the same time and doing the same thing.

could you share the code?
That would help a lot...
...mayby you are counting the millis in a variable, which isn´t declared an an unsigned long

Sounds very much like brown out.

I have a new voltage meter delivering tomorrow. For now, here is my code:

/*
    MPU6050 Triple Axis Gyroscope & Accelerometer. Simple Accelerometer Example.
    Read more: http://www.jarzebski.pl/arduino/czujniki-i-sensory/3-osiowy-zyroskop-i-akcelerometr-mpu6050.html
    GIT: https://github.com/jarzebski/Arduino-MPU6050
    Web: http://www.jarzebski.pl
    (c) 2014 by Korneliusz Jarzebski
*/

#include <Wire.h>
#include <MPU6050.h>

MPU6050 mpu;
#include "pitches.h"
 

void setup() 
{
  Serial.begin(9600);

  Serial.println("Initialize MPU6050");

  while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G))
  {
    Serial.println("Could not find a valid MPU6050 sensor, check wiring!");
    delay(500);
  }

  // If you want, you can set accelerometer offsets
  // mpu.setAccelOffsetX();
  // mpu.setAccelOffsetY();
  // mpu.setAccelOffsetZ();
  
  checkSettings();
}

void checkSettings()
{
  Serial.println();
  
  Serial.print(" * Sleep Mode:            ");
  Serial.println(mpu.getSleepEnabled() ? "Enabled" : "Disabled");
  
  Serial.print(" * Clock Source:          ");
  switch(mpu.getClockSource())
  {
    case MPU6050_CLOCK_KEEP_RESET:     Serial.println("Stops the clock and keeps the timing generator in reset"); break;
    case MPU6050_CLOCK_EXTERNAL_19MHZ: Serial.println("PLL with external 19.2MHz reference"); break;
    case MPU6050_CLOCK_EXTERNAL_32KHZ: Serial.println("PLL with external 32.768kHz reference"); break;
    case MPU6050_CLOCK_PLL_ZGYRO:      Serial.println("PLL with Z axis gyroscope reference"); break;
    case MPU6050_CLOCK_PLL_YGYRO:      Serial.println("PLL with Y axis gyroscope reference"); break;
    case MPU6050_CLOCK_PLL_XGYRO:      Serial.println("PLL with X axis gyroscope reference"); break;
    case MPU6050_CLOCK_INTERNAL_8MHZ:  Serial.println("Internal 8MHz oscillator"); break;
  }
  
   Serial.print(" * Accelerometer:         ");
  switch(mpu.getRange())
  {
    case MPU6050_RANGE_16G:            Serial.println("+/- 16 g"); break;
    case MPU6050_RANGE_8G:             Serial.println("+/- 8 g"); break;
    case MPU6050_RANGE_4G:             Serial.println("+/- 4 g"); break;
    case MPU6050_RANGE_2G:             Serial.println("+/- 2 g"); break;
  }  
 
  Serial.print(" * Accelerometer offsets: ");
  Serial.print(mpu.getAccelOffsetX());
  Serial.print(" / ");
  Serial.print(mpu.getAccelOffsetY());
  Serial.print(" / ");
  Serial.println(mpu.getAccelOffsetZ());
 
  Serial.println();
}

int State;
int State2;
unsigned long mark;


void loop()
{
  Vector rawAccel = mpu.readRawAccel();
  Vector normAccel = mpu.readNormalizeAccel();

 // Serial.print(" Xraw = ");

 // Serial.print(" Yraw = ");

 // Serial.print(" Zraw = ");

 //Serial.print(rawAccel.XAxis);

switch(State){
  case 0:
  if (rawAccel.XAxis <= -2300)
  { State = 1;
  mark = millis();
  }
  break;

  case 1:
  if(millis()-mark>500) State = 2;
  if(rawAccel.XAxis >= -2299) State = 0;
  break;

  case 2:
  tone(8, NOTE_A7, 500);
  delay(50);
  State = 0;
  
}


switch(State2){
  case 0:
  if (rawAccel.XAxis >= 2900)
  { State2 = 1;
  mark = millis();
  }
  break;

  case 1:
  if(millis()-mark>500) State2 = 2;
  if(rawAccel.XAxis <= 2899) State2 = 0;
  break;

  case 2:
  tone(8, NOTE_AS7, 500);
  delay(50);
  State2 = 0;
   
} 

 
}

The voltage coming in from the 9v adapter plugged into a 12v cigarette lighter plug is 9.18v.
The voltage from the nano to the accelerometer is 5.08v.

Anyone know what’s causing my program to stop working and have to reset power?

Should each state switch have it's own copy of mark?

Good point. I didn’t catch that. That needs to be corrected anyway but hopefully it will also keep things from locking up.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.