Hello, i have a problem with communication between processing and arduino.
I can control five servos from processing and this works always correctly (in the
arduino code case 1 to case 5).
The intention is that I can put them on (attachServos()) and off (detachServos()) from
processing (in the arduino code case 6 and case 7).
The processing code sent everything correctly but the Arduino does not always
respond correctly when I want to turn off (detach) the servos.
Turning on (attach) the servos is never a problem.
Can anyone find a mistake in my programming code?
Thanks.
A part of the arduino code:
void loop(){
if (Serial.available() > 3){
startbyte = Serial.read();
if (startbyte == 255){
for (t = 0; t<2; t++){
serialInput[t] = Serial.read();
}
servo = serialInput[0];
pos = serialInput[1];
if (pos == 255){
servo = 255;
}
switch(servo){
case 1:
servo_1.write(pos);
break;
case 2:
servo_2.write(pos);
break;
case 3:
servo_3.write(pos);
break;
case 4:
servo_rotatie.write(pos);
break;
case 5:
servo_hand.write(pos);
break;
case 6:
if (pos == 180){
attachServos();
}
break;
case 7:
if (pos == 1){
detachServos();
}
break;
}
}
}
}
void attachServos(){
servo_1.attach(Servo_1_Pin, minPulse, maxPulse);
servo_2.attach(Servo_2_Pin, minPulse, maxPulse);
servo_3.attach(Servo_3_Pin, minPulse, maxPulse);
servo_rotatie.attach(Servo_4_Pin, minPulse, maxPulse);
servo_hand.attach(Servo_5_Pin, minPulse, maxPulse);
}
void detachServos(){
servo_1.detach();
servo_2.detach();
servo_3.detach();
servo_rotatie.detach();
servo_hand.detach();
}
A part of the processing code:
void ON_OFF(boolean btnEnabled) {
if ( btnEnabled==true ) {
sendMove(6, 180);
servosEnabled = true;
println("Servos enabled");
}
else {
sendMove(7, 1);
servosEnabled = false;
println("Servos not enabled");
}
}
void sendMove(int servo, int val) {
myPort.write(255);
myPort.write(servo);
myPort.write(val);
}