amrmostaafaa
Published

Arduino Accelerometer Tutorial: Control a Ship Bridge

Controlling a ship bridge using a servo motor and accelerometer from 1Sheeld.

BeginnerFull instructions provided1,391
Arduino Accelerometer Tutorial: Control a Ship Bridge

Things used in this project

Story

Read more

Schematics

circuit

Code

Arduino_Servo_Ship_Bridge.ino

Arduino
/*
  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);
  }
}

Credits

amrmostaafaa

amrmostaafaa

7 projects • 43 followers
Maker, Engineer, dreamer and passionate about technology.

Comments