Using this code, you can control 12 servos with an Arduino.
For each servo, it's possible to define the position angle with Serial Monitor tool.
Syntax using Serial Monitor:
1.045 = move Servo 1 to position 45 degree
or
2.120 = move Servo 2 to position 120 degree
or
10.073 = move Servo 10 to position 73 degree
It's important prefix the position with 0 when the position angle is smaller then 100 degree!
Code:
#include <Servo.h> // declare servo objects Servo servo00; Servo servo01; Servo servo02; Servo servo03; Servo servo04; Servo servo05; Servo servo06; Servo servo07; Servo servo08; Servo servo09; Servo servo10; Servo servo11; // declare servo angle variables and setting initial values int servo00Angle; int servo01Angle; int servo02Angle; int servo03Angle; int servo04Angle; int servo05Angle; int servo06Angle; int servo07Angle; int servo08Angle; int servo09Angle; int servo10Angle; int servo11Angle; // declare a char array to buffer (5 byte) the inputs from Serial Monitor window char serialMonitorBuffer [5]; float value; int row = 0; // The setup() function is called when a sketch starts. void setup() { // Sets the data rate in bits per second (baud) for serial data transmission. Serial.begin(9600); // Attach the Servo variable to a pin. servo00.attach(2); servo01.attach(3); servo02.attach(4); servo03.attach(5); servo04.attach(6); servo05.attach(7); servo06.attach(8); servo07.attach(9); servo08.attach(10); servo09.attach(11); servo10.attach(12); servo11.attach(13); setInitialPositionValues(); } void loop() { // Get the number of bytes (characters) available for reading from the serial port. // This is data that's already arrived and stored in the serial receive buffe if (Serial.available()) { delay(5); // store serial data into buffer array (5 byte) int i=0; while(i < 5) { serialMonitorBuffer[i] = Serial.read(); i++; } // Flushes the buffer of incoming serial data. // That is, any call to Serial.read() or Serial.available() will return // only data received after all the most recent call to Serial.flush(). Serial.flush(); // convert numeric string to float value = atof(serialMonitorBuffer); row++; // Prints data to the serial port as human-readable ASCII text. Serial.print(row); Serial.print(";"); Serial.print(value); Serial.print(";"); // if 99 is given, set servos to initial position if(value > 99) { setInitialPositionValues(); } else if(value > 11.0) { float angle = (value - 11.0) * 1000; servo11Angle = (int)angle; } else if(value > 10.0) { float angle = (value - 10.0) * 1000; servo10Angle = (int)angle; } else if(value > 9.0) { float angle = (value - 9.0) * 1000; servo09Angle = (int)angle; } else if(value > 8.0) { float angle = (value - 8.0) * 1000; servo08Angle = (int)angle; } else if(value > 7.0) { float angle = (value - 7.0) * 1000; servo07Angle = (int)angle; } else if(value > 6.0) { float angle = (value - 6.0) * 1000; servo06Angle = (int)angle; } else if(value > 5.0) { float angle = (value - 5.0) * 1000; servo05Angle = (int)angle; } else if(value > 4.0) { float angle = (value - 4.0) * 1000; servo04Angle = (int)angle; } else if(value > 3.0) { float angle = (value - 3.0) * 1000; servo03Angle = (int)angle; } else if(value > 2.0) { float angle = (value - 2.0) * 1000; servo02Angle = (int)angle; } else if(value > 1.0) { float angle = (value - 1.0) * 1000; servo01Angle = (int)angle; } else if(value > 0.0) { float angle = (value - 0.0) * 1000; servo00Angle = (int)angle; } } // printOutServoAngles(); if(value > 0.0) { printOutServoAnglesToCsv(); } value = 0; servo00.write(servo00Angle); servo01.write(servo01Angle); servo02.write(servo02Angle); servo03.write(servo03Angle); servo04.write(servo04Angle); servo05.write(servo05Angle); servo06.write(servo06Angle); servo07.write(servo07Angle); servo08.write(servo08Angle); servo09.write(servo09Angle); servo10.write(servo10Angle); servo11.write(servo11Angle); delay(50); } void setInitialPositionValues() { servo00Angle = 84; servo01Angle = 80; servo02Angle = 86; servo03Angle = 79; servo04Angle = 95; servo05Angle = 85; servo06Angle = 85; servo07Angle = 85; servo08Angle = 81; servo09Angle = 88; servo10Angle = 79; servo11Angle = 79; } void printOutServoAnglesToCsv() { Serial.print(servo00Angle); Serial.print(";"); Serial.print(servo01Angle); Serial.print(";"); Serial.print(servo02Angle); Serial.print(";"); Serial.print(servo03Angle); Serial.print(";"); Serial.print(servo04Angle); Serial.print(";"); Serial.print(servo05Angle); Serial.print(";"); Serial.print(servo06Angle); Serial.print(";"); Serial.print(servo07Angle); Serial.print(";"); Serial.print(servo08Angle); Serial.print(";"); Serial.print(servo09Angle); Serial.print(";"); Serial.print(servo10Angle); Serial.print(";"); Serial.println(servo11Angle); }
No comments:
Post a Comment