Sunday, May 22, 2011

Arduino Servo Catapult

Decided to put my cardboard gluing skills to a new use and tried to build a catapult.
Did the whole cutting and gluing the tower and the arm,  Mounted the servo and did the coding.

Everything worked - except the servo is too slow so the projectile (the bead) just kind of falls out.

The code is very simple.
Read a button.
Move the servo and the arm
Pause
Reset the servo and the arm


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.

// Catapult
// Read a button press on pin 4
// and Sweep the servo on pin 10

#include <Servo.h>

int button4 = 4;  // Used to start servo10
Servo myservo10;  // create servo object to control a servo on pin10

void setup()
{
myservo10.attach(10);  // attaches the servo on pin 10 to the servo object
pinMode(button4,INPUT);  // button4 used to start myservo10
myservo10.write(180); // Put servo all the say back
}

void loop()
{
  // Check if button2 has been pressed
  if (digitalRead(button4) == HIGH){

    // Fire catapult - fully
  myservo10.write(80); // Put servo all the say back

  // Pause after firing
  delay(500);

  // Reset to all the way back
  myservo10.write(180); // Put servo all the say back
  }
}