Monday, January 18, 2010

IT LIVESSSSS!

After many hours of toiling away (and by many hours, I really mean about one or so), I wrote the necessary Arduino code needed to run the servos. It looks like this (I wish this blog site had actual code quoting):
#include

Servo pan, tilt;

void setup()
{
  Serial.begin(9600);
  pan.attach(3, 1000, 2000);
  tilt.attach(10, 1000, 2000);
  pinMode(2, OUTPUT);
  pinMode(4, OUTPUT);
  delay(15);
  pan.write(100);
  tilt.write(100);
}

void loop()
{
  digitalWrite(2, HIGH);
  digitalWrite(4, LOW);
  while(Serial.available() > 2)
  {
    int command = Serial.read();
    if(command == 255)
    {
      int command = Serial.read();
      int value = Serial.read();

      switch (command)
      {
      case 0:
        pan.write(value);
        break;

      case 1:
        tilt.write(value);
        break;
      }
    }
  }
}

All the code does is set up the servos and the on board serial port using the nice included libraries. It then instructs the Arduino to sit around and wait for serial data. When it gets something on its serial port, it checks first for a start byte (255), then for a servo ID (either 0 or 1), then a value to move the servo. This value should range from 0 to 180 and it (theoretically) corresponds to the degree the servo rotates to.

Here's a quick video of it moving:

There are still a number of issues to be worked out, mainly, it seems that sometimes when I issue a command to control one of the motors, it inadvertently moves the other motor as well. I am not completely sure what causes this, but I think I may try rewriting a large portion of the Arduino code to better deal with the serial communication.

Although I can (mostly) control the pan-tilt rig via a computer, there is still one last hardware issue that needs to be addressed before I can start distracting kittens and that is I need to create a method by which the computer can actually turn the laser pointer on and off. This shouldn't be too difficult of a challenge seeing as it should only consist of a few resistors and a transistor. I'll post that update sometime in the future.

1 comment:

  1. just started following this matt, sounds promising.

    a very useful way to spend extra time, haha

    ReplyDelete