Wednesday, June 8, 2011

Arduino Servo mounted maze controlled by 4 buttons

OK, this one is a bit less impressive than I wanted it to be.  Then I found out that really cheap servos cannot take the weight of a decent sized maze.  So, plan B was to use a small maze as a 'proof of concept'.

Using 4 buttons for up, down, left and right to move the maze to get the bead from the start to the finish.

The code (which is below) is actually quite straight forward.  Most of my time was spend gluing bits of cardbaord together to make the various bits that are needed to mount the servos and the maze itself..

In case you need recommends on cardbaord the box that the bumper packs of pampers nappies come in are ideal.  (yes I have small kids)


Below is the code.  Be aware I'm more about getting the Arduino to do something and so the code will be a bit rough around the edges.  It works but I definitely do not consider it best practise or even efficent code.
Use at your own risk.


// Balance Table for Maze
// Winkleink
// 2011

// Pin 7 Left
// Pin 6 Right

//Pin 5 Up
//Pin 4 Down

// Pin 10 Servo - Up/Down
// Pin 9 Servo - Left/Right

// Library that makes working with Servos easier
#include <Servo.h>

// Set Button variables
int bLeft = 5;  // Left Button
int bRight = 4;  // Right Button
int bUp = 7;  // Up Button
int bDown = 6;  // Down Button

// Set Servo variables
Servo servoUD;  // Servo Up/Down on Pin 10 
Servo servoLR;  // Servo Left/Right on Pin 9

//Set Servo position variables
int posUD = 100;    // variable to store the Up/Down servo position
int posLR = 100;    // variable to store the Left/Right servo position

void setup()
{
  servoUD.attach(10);    // attaches the servo on pin 10 to the servo object
  servoLR.attach(9);    // attaches the servo on pin 9 to the servo object

  pinMode(bLeft,INPUT);  // bLeft - Left Button
  pinMode(bRight,INPUT);  // bRight - Right Button
  pinMode(bUp,INPUT);  // bUp - Up Button
  pinMode(bDown,INPUT);  // bDown - Down Button


void loop()
{
// Position the Serrvo
// As the initial variable are center this also centers the table when it starts
servoUD.write(posUD);
servoLR.write(posLR);
 
if (digitalRead(bLeft) == HIGH){
posLR--;
}

if (digitalRead(bRight) == HIGH){
posLR++;
}

if (digitalRead(bUp) == HIGH){
posUD--;
}

if (digitalRead(bDown) == HIGH){
posUD++;
}

delay(50);
}

No comments:

Post a Comment