Stap 5: Gewijzigde code
De test loopt in de perfectie! Maar er een probleem is: de klauw opent wanneer ik de knop loslaat. Als ik wil houden de klauw gesloten, dan heb ik te houden op de knop te drukken. Dientengevolge, ik plaats van een waarde voor PWM voor elke servo en voeg / min 100 wanneer ik druk op de knop.
int A_button = 2;int B_button = 3;int C_button = 4;int D_button = 5;int E_button = 6;int F_button = 7;int Analog = 8;int x_axis = A0;int y_axis = A1;int value6 = 1000;int value5 = 1000;int value4 = 1000;int value3 = 1000;int value2 = 1000;int value1 = 1000;void setup() { Serial.begin(9600); pinMode(A_button, INPUT); pinMode(B_button, INPUT); pinMode(C_button, INPUT); pinMode(D_button, INPUT); pinMode(E_button, INPUT); pinMode(F_button, INPUT); pinMode(Analog, INPUT);}void move(int servo, int position, int time) { Serial.print("#"); Serial.print(servo); Serial.print("P"); Serial.print(position); Serial.print("T"); Serial.println(time); delay(time);}void loop() { int A_button_state = digitalRead(A_button); int B_button_state = digitalRead(B_button); int C_button_state = digitalRead(C_button); int D_button_state = digitalRead(D_button); int E_button_state = digitalRead(E_button); int F_button_state = digitalRead(F_button); int Analog_state = digitalRead(Analog); int x_axis_state = analogRead(x_axis); int y_axis_state = analogRead(y_axis); x_axis_state = map(x_axis_state, 0, 1000, -1, 1); y_axis_state = map(y_axis_state, 0, 1000, -1, 1); if (A_button_state == LOW) { if (value6 < 1700) { value6 = value6 + 100; move(6, value6, 100); } else { move(6, value6, 100); } } if (B_button_state == LOW) { if (value6 > 900) { value6 = value6 - 100; move(6, value6, 100); } } if (C_button_state == LOW) { if (value5 < 2500) { value5 = value5 + 100; move(5, value5, 100); } else { move(5, value5, 100); } } if (D_button_state == LOW) { if (value5 > 500) { value5 = value5 - 100; move(5, value5, 100); } } if (E_button_state == LOW) { if (value4 < 2500) { value4 = value4 + 100; move(4, value4, 100); } else { move(4, value4, 100); } } if (F_button_state == LOW) { if (value4 > 500) { value4 = value4 - 100; move(4, value4, 100); } } if (Analog_state == LOW) { if (value3 < 2500) { value3 = value3 + 100; move(3, value3, 100); } else { move(3, value3, 100); } } if (y_axis_state == -1) { if (value2 < 2500) { value2 = value2 + 100; move(2, value2, 100); } else { move(2, value2, 100); } } if (y_axis_state == 1) { if (value2 > 500) { value2 = value2 - 100; move(2, value2, 100); } } if (x_axis_state == -1) { if (value1 < 2500) { value1 = value1 + 100; move(1, value1, 100); } else { move(1, value1, 100); } } if (x_axis_state == 1) { if (value1 > 500) { value1 = value1 - 100; move(1, value1, 100); } }}