YourDuinoRobotArm-CODE
Jump to navigation
Jump to search
CODE FOR YOURDUINO ROBOT ARM
INITIAL CONSTRUCTION
SERVO ANGLE SET
We have an Arduino sketch than makes it easy to set each servo to the proper angle before attaching it's connecting piece and assembling it into the arm. You can type in an angle (like 45 or 90 etc.) and it will go to that angle. It can also run a "servo-Sweep" to test your servo. Copy the arduino sketch below and VERIFY it and SAVE IT with a name like "servoset". Upload it to your arduino and connect your servo on port "A0" (Reusing one of the 'analog' ports. This is the first (top) 3-pin connector on the Motor Driver shield.
NOTE! Make SURE the BLACK servo wire goes to the rightmost pin. Notice the "S V G" marking at the bottom of the set of 3-pin connectors. Those are the Signal, Voltage and Ground connections.
CODE
/* YourDuino Example: Set Servo angle and Servo Sweep - Moves a Servomotor through a range of positions - SEE the comments after "//" on each line below - CONNECTIONS: - Servo connector plugged on YourDuinoRobo1 port A0 - If separate wires: - Servo Black or Brown to Gnd. - Servo Red or Orange (Center wire) to +5V - Servo White or Yellow to Signal (Pin 9) - V1.02 01/02/2021 Questions: terry@yourduino.com */ /*-----( Import needed libraries )-----*/ #include <Servo.h> // Comes with Arduino IDE /*-----( Declare Constants and Pin Numbers )-----*/ #define ServoPIN A0 // Can be changed 3,5,6,9,10,11 #define ServoMIN 0 // Can set limits to servo travel #define ServoMAX 180 // which may be 0 to 180. #define numChars 32 // Used for keyboard input /*-----( Declare objects )-----*/ Servo myservo; // create servo object to control a servo // a maximum of eight servo objects can be created char receivedChars[numChars]; // an array to store the received data /*-----( Declare Variables )-----*/ int pos = 0; // variable to store the servo position boolean newData = false; int dataNumber = 0; // new for this version void setup() { myservo.attach(ServoPIN); // attaches the servo on pin you set to the servo object Serial.begin(115200); delay(100); Serial.println("Servo Set position (for Keyes Arm) NOTE: Set 'newline' below in Serial Monitor"); delay(100); Serial.println("Type in window above: 0 to 180 or 555 to ServoSweep once"); delay(100); }// END SETUP void loop() { recvWithEndMarker(); showNewNumber(); myservo.write(pos); }// END Loop //----------( USer written functions )------ void recvWithEndMarker() { static byte ndx = 0; char endMarker = '\n'; char rc; if (Serial.available() > 0) { rc = Serial.read(); if (rc != endMarker) { receivedChars[ndx] = rc; ndx++; if (ndx >= numChars) { ndx = numChars - 1; } } else { receivedChars[ndx] = '\0'; // terminate the string ndx = 0; newData = true; } } } //------------------------------------------------------------------------- void showNewNumber() { if (newData == true) { dataNumber = 0; // new for this version dataNumber = atoi(receivedChars); // new for this version if ((dataNumber >= 0) & (dataNumber <= 180)) { pos = dataNumber; } else { if (dataNumber != 555) { Serial.print("Number "); Serial.print(dataNumber); Serial.println(" is out of range 0..180"); } } Serial.print("SERVO Set to: "); Serial.println(pos); if (dataNumber == 555) { Serial.println("Number = 555 so doing one ServoSweep"); ServoSweep(); } // Serial.print("This just in ... "); // Serial.println(receivedChars); // Serial.print("Data as Number ... "); // new for this version // Serial.println(dataNumber); // new for this version newData = false; } } //------------------------------------------------------------------------- void ServoSweep() { for (pos = ServoMIN; pos < ServoMAX; pos += 1) // goes from 0 degrees to 180 degrees { // in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable 'pos' delay(10); // waits 15ms for the servo to reach the position } delay(1000); for (pos = ServoMAX; pos >= (ServoMIN + 1); pos -= 1) // goes from 180 degrees to 0 degrees { myservo.write(pos); // tell servo to go to position in variable 'pos' delay(10); // waits 15ms for the servo to reach the position } delay(1000); } //THE END