School Project in Need of Assistance

Hello all,

We are trying to write code to make a vibrating motor vibrate when the data from an accelerometer is within certain parameters. We have beginner code to get the data from the accelerometer, but are having some trouble figuring out how to use this data to send a charge to a specific pin, and how to tell the Arduino when exactly to send the charge. We are using an Arduino Uno along with a MPU9250 accelerometer. Any and all help is appreciated.

Pasted here is our current code:

#include "MPU9250.h"

// an MPU9250 object with the MPU-9250 sensor on I2C bus 0 with address 0x68
MPU9250 IMU(Wire,0x68);
int status;

void setup() {
// serial to display data
Serial.begin(115200);
while(!Serial) {}

// start communication with IMU
status = IMU.begin();
if (status < 0) {
Serial.println("IMU initialization unsuccessful");
Serial.println("Check IMU wiring or try cycling power");
Serial.print("Status: ");
Serial.println(status);
while(1) {}
}
}

void loop() {
// read the sensor
IMU.readSensor();
// display the data
Serial.print("AccelX: ");
Serial.print(IMU.getAccelX_mss(),6);
Serial.print("\t");
Serial.print("AccelY: ");
Serial.print(IMU.getAccelY_mss(),6);
Serial.print("\t");
Serial.print("AccelZ: ");
Serial.print(IMU.getAccelZ_mss(),6);
Serial.print("\t");
Serial.print("GyroX: ");
Serial.print(IMU.getGyroX_rads(),6);
Serial.print("\t");
Serial.print("GyroY: ");
Serial.print(IMU.getGyroY_rads(),6);
Serial.print("\t");
Serial.print("GyroZ: ");
Serial.print(IMU.getGyroZ_rads(),6);
Serial.print("\t");
Serial.print("MagX: ");
Serial.print(IMU.getMagX_uT(),6);
Serial.print("\t");
Serial.print("MagY: ");
Serial.print(IMU.getMagY_uT(),6);
Serial.print("\t");
Serial.print("MaZ: ");
Serial.print(IMU.getMagZ_uT(),6);
Serial.print("\t");
Serial.print("Temperature in C: ");
Serial.println(IMU.getTemperature_C(),6);
delay(100);
}

You will probably need to use if statements together with && (and) statements. That way you can say:

if (condition1 && condition2 && conditionx....)
{
do something, like writing a value to a pin which controls the motor
}

You're only one who know what the conditions should be and what motor you have and where/how it is connected so you need to make at least some attempt at a program, post it here, and then we can help you sort it out.

Steve

To send "charge" to a specific pin you first need to set that pin as an output. See pinMode()

Then you need to set the pin to HIGH or LOW. See digitalWrite()

To determine when this happens, as slipstick said you will need to use and if statement. See if()