Servo Control

From Microduino Wiki
Revision as of 08:45, 6 June 2014 by Pkj (talk) (Debugging)
Jump to: navigation, search

Objective

The course will show you how to control the turning of a steering engine by using Processing.

Equipment

  • Other Hardware Equipment
    • A USB cable
    • A steering engine

Schematic

ProcessingServoControlSchematics.jpg

Program

Referring to processingServoControl


Debugging

Step 1: Build the hardware environment according to the schematic, as follows:

ProcessingServoControlConnectionDiagram.jpg


Step 2:Here the code needed:

The code of the two ends (Processing and Microduino)

Microduino:

Using firmata StandardFirmata’s program since it achieves the method of how to control a steering engine


Processing:

//Define serial communication in “Setup” and set “pinMode” arduino = new Arduino(this, Arduino.list()[0], 57600); //your offset may vary

 arduino.pinMode(9, 5);

//Read the value of the mouse on the x-coordinate to set the angle of the steering engine

 void draw()
 {
   // read mouseX coordinate
   int newPos = constrain(mouseX/2, 0, 180); // update bg & servo position if mouseX changed
   if (newPos != pos)
   {
     background(0);
     stroke(255);  
     line(mouseX, 0, mouseX, 160);   
     text (newPos, mouseX, 180); 
     arduino.analogWrite(9, newPos);
     println (" newPos = " + newPos );
     pos=newPos; //update servo position storage variable
   }
 } 


Step 3:Download the code and get it compiled successfully.

Step 4:Move the mouse around to see how the steering changes after the system goes well.

Result

In Processing, an indicator will be displayed. Along with the change of the mouse on the x-coordinate, the steering engine will also rotate:

ProcessingServoControlResult.jpg


Video