In this example we use MegunoLink’s Interface Panel visualiser to create a user interface (UI) which controls a servo. The UI uses a trackbar set to send a message to the Arduino each time the value is changed. The Arduino, using our Command Handler, processes the message and extracts out the value sent which it uses to set the position of the servo.
Downloads
This example is available as part of our Arduino Library but you can also download the MegunoLink interface and Arduino program here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
/* ************************************************************************ Servo Control This program demonstrates how MegunoLink's Interface Panel and our command handler Arduino library can be used to control a servo. The example folder also contains a MegunoLink project, with an Interface Panel to control the parameters. Visit: * http://www.MegunoLink.com to download MegunoLink. ************************************************************************ */ #include #include "CommandHandler.h" #define ServoPin 9 Servo MyServo; // create servo object to control a servo CommandHandler<> SerialCommandHandler; // create command handler object void Cmd_SetServoPosition(CommandParameter &Parameters) { int ReceivedPosition = Parameters.NextParameterAsInteger(); // grab sent number as integer Serial.print("Position Received: "); Serial.println(ReceivedPosition); MyServo.write(ReceivedPosition); // sets the servo position according to received value } void setup() { Serial.begin(9600); delay(1000); Serial.print("Setup started.."); MyServo.attach(ServoPin); SerialCommandHandler.AddCommand(F("SetServoPos"), Cmd_SetServoPosition); // Attach command Serial.println("done."); } void loop() { SerialCommandHandler.Process(); // Process serial commands } |