Servo Command!

“Servo Command!” is a computer program written in the Processing computer language to control a continuous rotation servo via an Arduino interface.

I got a lot of the information out of books on Processing and the Arduino, then modified it to make this user interface.

The jumpers don’t match the servo colors because the jumper wire kit didn’t have the right colors in the right sizes.

The servo will continue turning on the Stop command unless an adjustment is made with the potentiometer. That’s inside the little hole on the top. You’ll need a small four-ply screwdriver.

I used the 11 pin on the Arduino for no reason other than it’s a PWM pin.

Here are the source codes for the Arduino and Processing programs. Note that 90 is stop and 0 and 180 are full speed. I chose half-speed but it would be easy enough to modify for full speed by replacing 45 with 0 and 135 with 180.

Arduino program “servo_test”:

// servo stuff
#include <Servo.h>
Servo myservo;
int pos = 0;
int angle = 0;

// serial stuff
int incomingByte = 0;	// for incoming serial 

void setup(){
  Serial.begin(9600);	// opens serial port, sets data rate to 9600 bps
  myservo.attach(11);
  myservo.write(90);  
}

void loop()
{
  
	// act on data only when you receive data:
	if (Serial.available() < 1) {
          return;
          }
          
	// read the incoming byte:
	incomingByte = Serial.read();

        // operate the servo
	if (incomingByte == 65){      // forward
            myservo.write(45);
          }   
	if (incomingByte == 66){      // stop
            myservo.write(90);
          }             
	if (incomingByte == 67){      // backward
            myservo.write(135);
          }    
  }

Here’s the Processing program “servo_command”:

// servo_command

// uses serial connection to tell servo what to do

import processing.serial.*;

Serial myPort;  // Create object from Serial class
int val;        // Data sent to the serial port

  
int clickflag = 0;
    
void setup() {
  size(480, 500);
  smooth(); 
  background(0);  
  
  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);
  
  stroke(255);
  noFill();  
  rect(100, 100, 50, 50);
  rect(100,200, 50,50); 
  rect(100,300,50,50);
  
  fill(255);
  textSize(20);
  text("SERVO COMMAND!",100,50);
  textSize(14);
  int x = 160;
  text("FORWARD",x,125);
  text("STOP",x,225); 
  text("REVERSE",x,325);
}

void draw() {

if (mousePressed) {
     clickflag = clickflag + 1;
}
     
if (clickflag == 1){
     // check mouse range
     if ((mouseX > 100) && (mouseX < 150)){
          // forward
          if ((mouseY > 100) && (mouseY < 150)){
               myPort.write(65);
                 fill(0);  
                 rect(100, 100, 50, 50);
                 rect(100,200, 50,50); 
                 rect(100,300,50,50);
                 fill(255);
                 rect(100, 100, 50, 50);                 
          }
          // stop
          if ((mouseY > 200) && (mouseY < 250)){
                 myPort.write(66); 
                 fill(0);  
                 rect(100, 100, 50, 50);
                 rect(100,200, 50,50); 
                 rect(100,300,50,50);
                 fill(255);
                 rect(100, 200, 50, 50);                
          } 
          // reverse
          if ((mouseY > 300) && (mouseY < 350)){
               myPort.write(67); 
                 fill(0);  
                 rect(100, 100, 50, 50);
                 rect(100,200, 50,50); 
                 rect(100,300,50,50);
                 fill(255);
                 rect(100, 300, 50, 50);                
          }         
     }    
     
} 
}

void mouseReleased(){
     clickflag = 0;
}


Advertisements

About engineerzero

Once and future engineer.
This entry was posted in Uncategorized and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s