Alex Hess - Code

The Code sent to the Arduino

 

 

// Electric Wooden Labyrinth

// Alex Hess

// ECE 387 - Miami University

// Dr. Jamieson

// This program is run on an Arduino Uno microprocessor

// It is used to take input from a joystick and control two servos

// The servos were mounted to control a Wooden Labyrinth Maze

 

#include <Servo.h>

 

//  Variables for each servo

Servo xServo; 

Servo yServo;

 

//  Variables for the analog inputs

const byte X_ANALOG = 0;

const byte Y_ANALOG = 1;

 

//  Variables to remember servo position

int xPosition;

int yPosition;

 

//  Variables to call buttons

const byte PIN_BUTTON_SELECT = 2;

const byte PIN_BUTTON_RIGHT = 3;

const byte PIN_BUTTON_UP = 4;

const byte PIN_BUTTON_DOWN = 5;

const byte PIN_BUTTON_LEFT = 6;

 

void setup()

{

  //  Setup pins used for servo communication

  xServo.attach(7);

  yServo.attach(8);

}

 

void loop()

{

  //  Convert the analog signals to digital number

  xPosition = analogRead(X_ANALOG);

  yPosition = analogRead(Y_ANALOG);

 

  //  Convert the position to a number the servo can read

  xPosition = map(xPosition, 0, 1023, 60, 130);

  yPosition = map(yPosition, 0, 1023, 145, 70);  //  Notice the output range (last 2 #'s for servo) is backwards here

                                                 //  The map function can convert to any range( forward or backward)

                                                 //  Another useful function is the constrain function.  It can't go past range unlike map function

 

  //  Tell the servo what position to move to

  xServo.write(xPosition);

  yServo.write(yPosition);

 

}