Accelerometer sensors are now in most of our smartphones to give them a wide variety of usage and capabilities that we use daily, without even knowing that the one responsible for it is the accelerometer.

One of these capabilities is the controllability that the accelerometer give to us where you can control your car in racing games or use a certain app to turn your phone into a remote control for your robot or RC car that uses the accelerometer as the control tool.

So, in this Arduino Accelerometer Tutorial, we are going to use this last feature to make use of the embedded accelerometer sensor on our smartphones to control the opening and closing of a ship bridge. Of course, the bridge is just a servo motor that goes on 0 degrees to close and 90 degrees to open.

Let’s talk about the idea behind this Arduino Accelerometer Tutorial ...


Idea:

We are going to use 1Sheeld board with its companion Android/iOS app and use the accelerometer shield where any change in any x-axis or y-axis or z-axis will be sent to the Arduino and hence we can decide whether to change the servo degree to 0 or 90.


Getting started:

If this is your first time to deal with 1Sheeld or you want to learn more about it, I recommend checking this quick and easy getting started tutorial.

And if you haven't tried servo motor before, I recommend checking this quick video.

Now, after you've become a little bit familiar with 1Sheeld, let's start!


Step 1: Hardware components:

-       Arduino Uno.

-       1Sheeld+ board.

-       S90 servo motor.

-       LED.

-       3 * Male to male wires.

-       Arduino USB cable or 9-12v battery.

-       Android/iOS phone with 1Sheeld App installed on it.


Step 2: Software components:


Step 3: Connection and Schematic:

Firstly, you slide the switch towards the “SWITCH” notation which turns the 1Sheeld board into the Uploading mode to let you upload the Arduino code.

Secondly, after you finish uploading the code, slide the switch towards the “UART” notation (or “SERIAL” at 1Sheeld+ board) which turns the 1Sheeld board into the Operating mode to communicate with your smartphone 1Sheeld App.


Code:

I would recommend checking the Arduino Accelerometer Shield documentation to know more about the Arduino Accelerometer Shield functionalities and how to use them.

Now, switch the 1Sheeld board to the Uploading mode, upload this code

/*
  Arduino Accelerometer Project

  This project shows an application on 1Sheeld's Accelerometer shield.

  By using this project, you can wirelessly control
  the ship bridge by using Accelerometer shield from 1Sheeld.
  
  OPTIONAL:
  To reduce the library compiled size and limit its memory usage, you
  can specify which shields you want to include in your sketch by
  defining CUSTOM_SETTINGS and the shields respective INCLUDE_ define.
*/

/* Include required shields */
#define CUSTOM_SETTINGS
#define INCLUDE_TERMINAL_SHIELD
#define INCLUDE_TEXT_TO_SPEECH_SHIELD
#define INCLUDE_ACCELEROMETER_SENSOR_SHIELD

/* Commands which will be converted to speech */
const char command1[] = "the bridge is open now";
const char command2[] = "a ship is crossing over";
const char command3[] = "the ship has just crossed over";
const char command4[] = "the bridge is closed now";

/* Include 1Sheeld library. */
#include <OneSheeld.h>

/* Include servo motor library */
#include <Servo.h>

/* Initiate a servo object */
Servo servo;

/* Define the servo signal pin */
int servoPin = 8;

/* define the LED pin */
int ledPin = 13;

void setup()
{
  /* Start 1Sheeld communication. */
  OneSheeld.begin();
  
  /* make LED pin output */
  pinMode(ledPin , OUTPUT);

  /* Configure Arduino to use this pin as the servo signal pin */
  servo.attach(servoPin);

  /* Start servo at the 0 degree angle */
  servo.write(0);
}

void loop()
{
  /* Check y-axis acceleration. */
  /* Bridge is open once the Y-axis value is greater than 4 */
  if(AccelerometerSensor.getY() > 4)
  {
      /* Move servo to the 90 degree to open the bridge */
      servo.write(90);

      /* Turn the LED on */
      digitalWrite(ledPin,HIGH);

      /* Announce that the bridge is opened */
      TextToSpeech.say(command1);

      /* Delay 3 seconds for the speech to be completed */
      OneSheeld.delay(3000);

      /* Announce that a ship is crossing the bridge right now */
      TextToSpeech.say(command2);

      /* Delay 3 seconds for the speech to be completed */
      OneSheeld.delay(3000);
  }

  /* The bridge is considered closed if the y-axis value is less than -4 */
  else if(AccelerometerSensor.getY() < -4)
  {
      /* Move servo to the 0 degree to close the bridge */
      servo.write(0);

      /* Turn the LED off */
      digitalWrite(ledPin,LOW);

      /* Announce that the ship has crossed the bridge */
      TextToSpeech.say(command3);

      /* Delay 3 seconds for the speech to be completed */
      OneSheeld.delay(3000);

      /* Announce that the bridge is closed */
      TextToSpeech.say(command4);

      /* Delay 3 seconds for the speech to be completed */
      OneSheeld.delay(3000);
  }
}

Then, Switch the 1Sheeld board to the Operating mode then open the 1Sheeld app and connect it to the 1Sheeld board via Bluetooth.


Step 5: Run it:

As you see in the Arduino Accelerometer Tutorial video, I have used a piece of cardboard taped to the servo motor as an indication of the bridge movement.

Then you tend the phone towards you and you will see the bridge has opened with 90 degrees of the servo and the LED is on with a speech comes out from the phone's speaker telling you that the bridge is now opened and a ship is crossing over.

And once you tend the phone on the opposite side, you will see the bridge is closed with 0 degrees of the servo and the LED is off with a speech comes out from the phone's speaker telling you that the bridge is now closed and a ship has crossed over.

That was it guys, I hope you enjoyed this quick Arduino Accelerometer Tutorial and for any questions or even opinions about it please don’t hesitate to leave your comment down below.