|
| 1 | +#include <Servo.h> |
| 2 | + |
| 3 | +Servo s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12; |
| 4 | + |
| 5 | +double ang2pulse(uint8_t angle) { |
| 6 | + double pulse; |
| 7 | + pulse = (0.5 + angle/90.0) * 1000; |
| 8 | + return pulse; |
| 9 | +} |
| 10 | + |
| 11 | +void setup_servos() { |
| 12 | + Serial.println("Setup Servos"); |
| 13 | + // limb1 |
| 14 | + s1.attach(3, 500, 2500); |
| 15 | + s2.attach(4, 500, 2500); |
| 16 | + s3.attach(5, 500, 2500); |
| 17 | + // limb2 |
| 18 | + s4.attach(6, 500, 2500); |
| 19 | + s5.attach(7, 500, 2500); |
| 20 | + s6.attach(8, 500, 2500); |
| 21 | + // limb3 |
| 22 | + s7.attach(9, 500, 2500); |
| 23 | + s8.attach(10, 500, 2500); |
| 24 | + s9.attach(11, 500, 2500); |
| 25 | + // limb4 |
| 26 | + s10.attach(12, 500, 2500); |
| 27 | + s11.attach(13, 500, 2500); |
| 28 | + s12.attach(14, 500, 2500); |
| 29 | +} |
| 30 | + |
| 31 | +void shut_servos() { |
| 32 | + Serial.println("Shut Servos"); |
| 33 | + s1.detach(); |
| 34 | + s2.detach(); |
| 35 | + s3.detach(); |
| 36 | + s4.detach(); |
| 37 | + s5.detach(); |
| 38 | + s6.detach(); |
| 39 | + s7.detach(); |
| 40 | + s8.detach(); |
| 41 | + s9.detach(); |
| 42 | + s10.detach(); |
| 43 | + s11.detach(); |
| 44 | + s12.detach(); |
| 45 | +} |
| 46 | + |
| 47 | +void run_servos() { |
| 48 | + Serial.println("Run Servos"); |
| 49 | + s1.writeMicroseconds(ang2pulse(45)); |
| 50 | + s2.writeMicroseconds(ang2pulse(90)); |
| 51 | + s3.writeMicroseconds(ang2pulse(180)); |
| 52 | + |
| 53 | + s4.writeMicroseconds(ang2pulse(130)); |
| 54 | + s5.writeMicroseconds(ang2pulse(83)); |
| 55 | + s6.writeMicroseconds(ang2pulse(0)); |
| 56 | + |
| 57 | + s7.writeMicroseconds(ang2pulse(130)); |
| 58 | + s8.writeMicroseconds(ang2pulse(83)); |
| 59 | + s9.writeMicroseconds(ang2pulse(0)); |
| 60 | + |
| 61 | + s10.writeMicroseconds(ang2pulse(45)); |
| 62 | + s11.writeMicroseconds(ang2pulse(90)); |
| 63 | + s12.writeMicroseconds(ang2pulse(180)); |
| 64 | + |
| 65 | + delay(1000); |
| 66 | +} |
| 67 | + |
| 68 | +void setup() { |
| 69 | + Serial.begin(9600); |
| 70 | + Serial.setTimeout(10); |
| 71 | + while (!Serial); |
| 72 | + // wait for ready |
| 73 | + while (Serial.available() && Serial.read()); // empty buffer |
| 74 | + |
| 75 | + Serial.println("Lab robot servo test!"); |
| 76 | + Serial.println("Please select code:"); |
| 77 | + Serial.println("1. Setup Servo"); |
| 78 | + Serial.println("2. Run"); |
| 79 | + Serial.println("3. Shut Servo"); |
| 80 | +} |
| 81 | + |
| 82 | +String inString = ""; |
| 83 | +void loop() { |
| 84 | + if (Serial.available() > 0) { |
| 85 | + if (Serial.peek() != '\n') { |
| 86 | + inString += (char)Serial.read(); |
| 87 | + } else { |
| 88 | + Serial.read(); |
| 89 | + Serial.print("Instruction received: "); |
| 90 | + |
| 91 | + int code = inString.toInt(); |
| 92 | + Serial.println(code); |
| 93 | + switch(code) { |
| 94 | + case 1: |
| 95 | + setup_servos(); |
| 96 | + break; |
| 97 | + case 2: |
| 98 | + run_servos(); |
| 99 | + break; |
| 100 | + case 3: |
| 101 | + shut_servos(); |
| 102 | + break; |
| 103 | + } |
| 104 | +// run_servo(); |
| 105 | + inString = ""; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | +} |
0 commit comments