Skip to content

Instantly share code, notes, and snippets.

@chesterbr
Created December 23, 2017 02:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chesterbr/9d682b11715c092793b848977ebd8dac to your computer and use it in GitHub Desktop.
Save chesterbr/9d682b11715c092793b848977ebd8dac to your computer and use it in GitHub Desktop.
Washing Machine Alarm with Arduino and ADXL-345 acceleration sensor
// washing_machine_alarm.ino
//
// Copyright by Carlos Duarte Do Nascimento, 2017
// Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
// More info: https://chester.me/archives/2017/12/motion-sensing-arduino-based-washing-machine-alarm/
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL345_U.h>
#include <LowPower.h>
#define SPEAKER_PIN 8
#define MOTION_SENSITIVITY 0.15 /* smaller values = more sensitive */
#define MEASUREMENTS_TO_CONSIDER_STOPPED 3
Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345);
sensors_vec_t old_acceleration;
sensors_event_t event;
bool moving() {
accel.getEvent(&event);
float motion =
abs(old_acceleration.x - event.acceleration.x) +
abs(old_acceleration.y - event.acceleration.y) +
abs(old_acceleration.z - event.acceleration.z);
old_acceleration = event.acceleration;
return (motion > MOTION_SENSITIVITY);
}
void play_music() {
while (true) {
tone(SPEAKER_PIN, 1000, 500);
LowPower.powerDown(SLEEP_2S, ADC_OFF, BOD_OFF);
}
}
void setup() {
accel.begin();
pinMode(SPEAKER_PIN, OUTPUT);
}
int no_movement_count = 0;
void loop() {
LowPower.powerDown(SLEEP_2S, ADC_OFF, BOD_OFF);
if (moving()) {
no_movement_count = 0;
} else {
no_movement_count++;
}
if (no_movement_count == MEASUREMENTS_TO_CONSIDER_STOPPED) {
play_music();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment