ATMega Gertboard controlled servo via serial input

18 May 2013

Well I have worked through quite a few of the demos on the gertboard manual so I thought I'd spend some time combining the demos and rather than just have a motor going or a led lighting up I duct taped a nerf gun to the servo and as you can see from the demo it does fire.

Alex eames from raspi tv give some help on wiring it together, as well the sketches in the arduino ide mention pin 9 and using the gertboard you have to use the buffers so you set up buffer 1 as pin 9.

To wire it up you do the following : PB1 to B1 on J3 and servo pulse to BUF1 near the leds. Jumper B1 set to output.
GND lead of servo to Gertboard GND and 5V servo lead to 5V supply. GND of 5V supply also to Gertboard GND.
raspberryorg

You jumper off the output as PB1 will provide the input to Buf 1. You then wire the control cable [orange on the servo] to Buf 1. The ground cable from the servo goes in the ground of PB1 and the positive servo cable to the positive battery. I ground the battery cable to the ground on PB1.It does not seem to matter where it is ground goes

Minicom has been set up on the raspberry pi so GP14 and GP15 had jumpers set across to MCTX and RCTX as can be seen in the video. All that needs done to read the data from minicom (serial comms) is the function call Serial.Read

The code from the arduino ide is listed below

// Serial Servo Nerf

#include <Servo.h>

Servo myservo; // create servo object
byte num = 0; // read in serial input
int pos = 0;
int servo_enabled = 0;

void setup() {
// initialize serial port
Serial.begin(9600);
myservo.attach(9); // attaches the servo on pin 9
}

void loop()
{
// pressing the letter a fires the servo
num = Serial.read();

if (num == 97 && servo_enabled == 0)
{
servo_enabled = 1;
Serial.print("arming nerf");
Serial.print("character received: ");
Serial.println(num, DEC);
delay(1); // required to read serial buffer
}
}

if (servo_enabled ==  1 )
{

for(pos = 0; pos < 100; pos +=1)
{    myservo.write(pos);
delay(15); // waaaaaits 15 ms for the servo to reach position
}

// move back
for(pos =  100; pos >=1 ; pos-=1) // goes aaback to the position
{
myservo.write(pos);
delay(15);

}

Serial.print("servo disengaged");
servo_enabled = 0;
}

}

Published on 18 May 2013 Find me on Twitter!