// Basic demo for accelerometer readings from Adafruit LIS3DH #include #include #include #include #include #define MOTOR 10 #if __cplusplus >= 201402L void operator delete(void* ptr, size_t size) noexcept { operator delete(ptr); } void operator delete[](void * ptr, size_t size) noexcept { operator delete[](ptr); } #endif // I2C Adafruit_LIS3DH lis = Adafruit_LIS3DH(); void setup(void) { pinMode(MOTOR, OUTPUT); Serial.begin(115200); while (!Serial) delay(10); // will pause Zero, Leonardo, etc until serial console opens Serial.println("LIS3DH test!"); if (! lis.begin(0x18)) { // change this to 0x19 for alternative i2c address Serial.println("Couldnt start"); while (1) yield(); } Serial.println("LIS3DH found!"); // lis.setRange(LIS3DH_RANGE_4_G); // 2, 4, 8 or 16 G! Serial.print("Range = "); Serial.print(2 << lis.getRange()); Serial.println("G"); // lis.setDataRate(LIS3DH_DATARATE_50_HZ); Serial.print("Data rate set to: "); switch (lis.getDataRate()) { case LIS3DH_DATARATE_1_HZ: Serial.println("1 Hz"); break; case LIS3DH_DATARATE_10_HZ: Serial.println("10 Hz"); break; case LIS3DH_DATARATE_25_HZ: Serial.println("25 Hz"); break; case LIS3DH_DATARATE_50_HZ: Serial.println("50 Hz"); break; case LIS3DH_DATARATE_100_HZ: Serial.println("100 Hz"); break; case LIS3DH_DATARATE_200_HZ: Serial.println("200 Hz"); break; case LIS3DH_DATARATE_400_HZ: Serial.println("400 Hz"); break; case LIS3DH_DATARATE_POWERDOWN: Serial.println("Powered Down"); break; case LIS3DH_DATARATE_LOWPOWER_5KHZ: Serial.println("5 Khz Low Power"); break; case LIS3DH_DATARATE_LOWPOWER_1K6HZ: Serial.println("16 Khz Low Power"); break; } } void loop() { lis.read(); // get X Y and Z data at once /* Or....get a new sensor event, normalized */ sensors_event_t event; lis.getEvent(&event); /* Display the results */ float movement = abs(event.acceleration.x) + abs(event.acceleration.y) + abs(event.acceleration.z); Serial.print(" \ttot_movement: "); Serial.print(movement); Serial.println(); if(movement >= 25){ digitalWrite(MOTOR, HIGH); } else { digitalWrite(MOTOR, LOW); } delay(200); }