Soil_Person Posted June 24, 2020 Share Posted June 24, 2020 (edited) Hi everyone, Here is how to control a 180 degree Pulse Width Modulated (PWM) Servo in Arduino with AutoIT. Firstly, I'm re-posting this from a comment I made as I think it is valuable by it's self: Original Thread: I've adapted codes by reaper7 (see above link) to include a GUI to move 1 servo between 0-180 degrees. This GUI is simple, type a number between 0 and 180, then press 'Move Servo'. The servo will then move to the desired location. IMPORTANT: I'm assuming you are using a 180 degree servo here. If you are using a 90 degree servo, this may overextended your servo and damage it. Adjust to code in the Arduino code to suit your servo's rotational limits (3rd last line of Arduino code). For a 180 degree servo: val = map(data, 0,180, 0, 170) For a 90 degree servo: val = map(data, 0,180, 0, 90) On a trouble shooting note: At first I was having trouble getting AutoIT to even recognise the Arduino port. To fix this you have to put your script in the same folder as the commMG.au3 and commdll files. What worked for me was putting it all in the 'include' folder on the AutoIT install location. Put all 3 files in there. Also, make sure your commMG file is the same one we are all talking about here, if these codes don't match, no dice! Here is the link for the commMG.au3 and commdll files (just to make sure) https://www.mosaiccgl.co.uk/AutoItDownloads/confirm.php?get=COMMGvv2.zip eg: C:\Program Files (x86)\AutoIt3\Include AutoIT Code: expand popup #include <CommMG.au3> #include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <EditConstants.au3> #include <StaticConstants.au3> Global $CMPort = 7 Global $CmBoBaud = 9600 Global $sportSetError = '' Global $CmboDataBits = 8 Global $CmBoParity = "none" Global $CmBoStop = 1 Global $setflow = 2 _CommSetPort($CMPort, $sportSetError, $CmBoBaud, $CmboDataBits, $CmBoParity, $CmBoStop, $setflow) If @error Then MsgBox(16,"Error!","Can't connect to Arduino on port - "&$CMPort) Exit EndIf _CommSetRTS(0) _CommSetDTR(0) #Region ### START Koda GUI section ### Form= Global $Form1 = GUICreate("AutoIT -->Arduino Servo Mover", 407, 115, 358, 304) Global $ServoValueInputBox = GUICtrlCreateInput("90", 232, 16, 137, 32);Defualt value is 90 (ie: 90- degrees) GUICtrlSetFont(-1, 14, 400, 0, "MS Sans Serif") Global $ServoLabel = GUICtrlCreateLabel("Servo Value (0-180)",40, 16, 170, 28) GUICtrlSetFont(-1, 14, 400, 0, "MS Sans Serif") Global $MoveServoButton = GUICtrlCreateButton("Move Servo", 40, 56, 331, 49) GUICtrlSetFont(-1, 14, 400, 0, "MS Sans Serif") GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### While 1 $nMsg = GUIGetMsg() $Servo1 = GUICtrlRead($ServoValueInputBox) Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $MoveServoButton GUICtrlRead($ServoValueInputBox) _CommSendString(stringformat("A%03d",int($Servo1))) EndSwitch WEnd Arduino Code: #include <Servo.h> Servo Servo1; // create servo called Servo1 Servo Servo2; // create servo called Servo2 char Address; //Variable to store Incoming Packets ID int val = 0; int data = 0; void setup() { Serial.begin(9600); Servo1.attach(8); // attaches the servo1 on pin 8 to the servo object } void loop() { while (Serial.available() > 0) { Address = Serial.read(); if (Address == 'A') Read_Servo1(); // If ID = A goto Servo1 } } void Read_Servo1() { delay(2); int data100 = Serial.read()- '0'; // Read in first bit od data delay(2); int data10 = Serial.read()- '0'; // Read in second bit od data delay(2); int data1 = Serial.read()- '0'; // Read in third bit od data data = 100*data100 + 10*data10 + data1; // Left shift each byte, add add together to get our value 000 val = map(data, 0,180, 0, 170); // Asign our range of 0 - 180 to the servos range of 0 - 170 Servo1.write(val); //Write the value to Serial delay(10); // waits 10ms for the servo to reach the position } Enjoy, Soil_Person Edited June 24, 2020 by Soil_Person argumentum 1 Link to comment Share on other sites More sharing options...
Soil_Person Posted June 26, 2020 Author Share Posted June 26, 2020 I've updated it a bit, Here is the code if you prefer a slider input for AutoIT (makes it more fun). Note the Arduino code doesn't change here, use the same code as in the above post. AutoIT code: expandcollapse popup#include <CommMG.au3> #include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <EditConstants.au3> #include <StaticConstants.au3> Global $CMPort = 7 Global $CmBoBaud = 9600 Global $sportSetError = '' Global $CmboDataBits = 8 Global $CmBoParity = "none" Global $CmBoStop = 1 Global $setflow = 2 _CommSetPort($CMPort, $sportSetError, $CmBoBaud, $CmboDataBits, $CmBoParity, $CmBoStop, $setflow) If @error Then MsgBox(16,"Error!","Can't connect to Arduino on port - "&$CMPort) Exit EndIf _CommSetRTS(0) _CommSetDTR(0) #Region ### START Koda GUI section ### Form= Global $Form1_1 = GUICreate("AutoIT -->Arduino Servo Mover", 796, 116, 204, 185) Global $ServoLabel = GUICtrlCreateLabel("Move slider to move servo", 280, 16, 226, 28) GUICtrlSetFont(-1, 14, 400, 0, "MS Sans Serif") Global $Slider1 = GUICtrlCreateSlider(130, 56, 526, 45) GUICtrlSetLimit($Slider1, 180, 0) GUICtrlSetData($Slider1, 90) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### While 1 $nMsg = GUIGetMsg() $ServoSliderOutput = GUICtrlRead($Slider1) _CommSendString(stringformat("A%03d",int($ServoSliderOutput))) Sleep(20) Switch $nMsg Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd argumentum 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now