![]() |
|
Arduino C - Printable Version +- Sinisterly (https://sinister.li) +-- Forum: Coding (https://sinister.li/Forum-Coding) +--- Forum: C, C++, & Obj-C (https://sinister.li/Forum-C-C-Obj-C) +--- Thread: Arduino C (/Thread-Arduino-C) |
Arduino C - Megan - 11-07-2015 This is some Arduino C code I wrote to find the stop value of some modified servos, for anyone interested in Arduino's I figured I would share this here, feel free to ask questions or critique my code if you know what you're talking about. Code: /******************************
* *
* Servo Calibration *
* VERSION: 0.01 *
* *
******************************/
// adds the Servo library to the sketch!
#include <Servo.h>
// assigns the servos names to be used accordingly
Servo leftServo;
Servo righServo;
// Setting some variables, I like to assign them numbers regardless
int servoSpeed = 0;
int servoSet = 0;
int servoChoice = 0;
int leftServoStop = 0;
int righServoStop = 0;
// Function time!
void leftServoCal(){
// A Potentially endless for loop because I like to do it this rudimentary way
for(int x = 0; x < 999;){
Serial.println("How fast do you want the servo to run at?");
while (!Serial.available()){;} // Waits for a user to answer the question.
servoSpeed = Serial.parseInt();
Serial.print("Servo speed set at ");
Serial.println(servoSpeed);
leftServo.write(servoSpeed); // Sets the servo speed based on the users input & tells the user just for clarification
// Checks to see if the servo has stopped, this gives a delay until the user is ready to continue, I included instructions because it is impossible for the user to know how to work it without knowledge.
Serial.println();
Serial.println("Has the servo stopped?");
Serial.println("0 = No | 1 = Yes");
while (!Serial.available()){;}
servoSet = Serial.parseInt();
if(servoSet == 0){ // No
Serial.println("No!");
Serial.println();
}
else if (servoSet == 1){ // Yes
Serial.println("Yes!");
Serial.println();
x = 999; // If the servo has stopped then yeah, it'll end the for loop.
}
else{
Serial.println("Unacceptable value, try again!"); // error handling
}
}
// Just tells the user what the servo numbers are set to.
Serial.println("Servo calibration complete!");
Serial.print("Left servo stop speed is ");
Serial.print(servoSpeed);
Serial.println();
Serial.println();
leftServoStop = servoSpeed;
servoCal(); // Takes them to the servo calibration UI again.
}
// I'm not going to comment on this function as it is identical to the above except for a few names related to which servo is operating.
void righServoCal(){
for(int x = 0; x < 999;){
Serial.println("How fast do you want the servo to run at?");
while (!Serial.available()){;}
servoSpeed = Serial.parseInt();
Serial.print("Servo speed set at ");
Serial.println(servoSpeed);
righServo.write(servoSpeed);
Serial.println();
Serial.println("Has the servo stopped?");
Serial.println("0 = No | 1 = Yes");
while (!Serial.available()){;}
servoSet = Serial.parseInt();
if(servoSet == 0){ // No
Serial.println("No!");
Serial.println();
}
else if (servoSet == 1){ // Yes
Serial.println("Yes!");
Serial.println();
x = 999;
}
else{
Serial.println("Unacceptable value, try again!");
}
}
Serial.println("Servo calibration complete!");
Serial.print("Left servo stop speed is ");
Serial.print(servoSpeed);
Serial.println();
Serial.println();
righServoStop = servoSpeed;
servoCal();
}
// A simple function to stop both servos when they are finally calibrated.
void servoStop(){
Serial.println("Both Servo Stop values have been calibrated!");
leftServo.write(leftServoStop);
righServo.write(righServoStop);
}
// Servo calibration UI
void servoCal(){
Serial.println("Which servo do you wish to calibrate?");
Serial.println("0 = left | 1 = Right | 2 = Both complete");
Serial.println();
while (!Serial.available()){;}
servoChoice = Serial.parseInt();
if(servoChoice == 0){
leftServoCal();
}
else if(servoChoice == 1){
righServoCal();
}
else if(servoChoice == 2){
servoStop();
}
else{
Serial.println("Unacceptable value, try again!"); // Error handling
servoCal();
}
}
void setup() {
Serial.begin(9600); // Connects to the Serial Console
leftServo.attach(6); // Attaches to the left servo aka number 6
righServo.attach(5); // Attaches to the right servo aka number 5
servoCal(); // and it runs the servo calibration UI
}
void loop() {
} |