Hey im having an issue with my servo motor code. im trying to make a robotic arm so i need the motor to move in increments and not a sweeping motion. My code currently does increments for the motor in one direction and a sweep in the other direction.
void loop()
{
int i;
unsigned char data, addr;
// Command to send to the gamecube
// The last bit is rumble, flip it to rumble
// yes this does need to be inside the loop, the
// array gets mutilated when it goes through N64_send
unsigned char command[] = {0x01};
// don't want interrupts getting in the way
noInterrupts();
// send those 3 bytes
N64_send(command, 1);
// read in data and dump it to N64_raw_dump
N64_get();
// end of time sensitive code
interrupts();
// translate the data in N64_raw_dump to something useful
translate_raw_data();
for (i=0; i<16; i++) {
Serial.print(N64_raw_dump[i], BIN);
}
Serial.print(' ');
Serial.print(N64_status.stick_x, BIN);
Serial.print(' ');
Serial.print(N64_status.stick_y, BIN);
Serial.print(" \n");
// Serial.print(" Stick X:");
// Serial.print(N64_status.stick_x, BIN);
// Serial.print(" Stick Y:");
//Serial.println(N64_status.stick_y, BIN);
if (N64_status.data1 == 0) {
controlServoy();
controlServox();
} else {
controlServoy2();
controlServox2();
//If any of the fcllowing buttons A,B,Z,Start are pushed then it unlocks control for servo 3 and 4
}
if (N64_status.stick_x >0)
N64_status.stick_y =0
;if (N64_status.stick_y >0)
{
N64_status.stick_x =0
;}
// DEBUG: print it
//print_N64_status();
delay(25);
}
void controlServox() {
if (N64_status.stick_x > 10 && currentServoPosition >=0)
{
currentServoPosition = currentServoPosition - 45; //can change the number to make it move quicker
}
else if (N64_status.stick_x > 90 && currentServoPosition >= 0){
//Do nothing the servo is already at its max
}
else if (N64_status.stick_x < -10 && currentServoPosition < 180){
currentServoPosition = currentServoPosition + 10; //can change the number to make it move quicker
}
else if (N64_status.stick_x < -10 && currentServoPosition >= 180){
//Do nothing the servo is already at its max
}
else{
//Do nothing Stick is roughly centre
}
myservo.write(currentServoPosition);
}
void controlServoy() {
if (N64_status.stick_y > 10 && currentServoPosition1 >=0)
{
currentServoPosition1 = currentServoPosition1 - 90; //can change the number to make it move quicker
}
else if (N64_status.stick_y > 90 && currentServoPosition1 >= 0){
//Do nothing the servo is already at its max
}
else if (N64_status.stick_y < -10 && currentServoPosition1 < 180){
currentServoPosition1 = currentServoPosition1 + 10; //can change the number to make it move quicker
}
else if (N64_status.stick_y < -10 && currentServoPosition1 >= 180){
//Do nothing the servo is already at its max
}
else{
//Do nothing Stick is roughly centre
}
myservo1.write(currentServoPosition1);
}
void controlServox2() {
if (N64_status.stick_x > 10 && currentServoPosition2 >=0)
{
currentServoPosition2 = currentServoPosition2 - 180; //can change the number to make it move quicker
}
else if (N64_status.stick_x > 90 && currentServoPosition2 >= 0){
//Do nothing the servo is already at its max
}
else if (N64_status.stick_x < -10 && currentServoPosition2 < 180){
currentServoPosition2 = currentServoPosition2 + 10; //can change the number to make it move quicker
}
else if (N64_status.stick_x < -10 && currentServoPosition2 >= 180){
//Do nothing the servo is already at its max
}
else{
//Do nothing Stick is roughly centre
}
myservo2.write(currentServoPosition2);
}
void controlServoy2() {
if (N64_status.stick_y > 10 && currentServoPosition3 >=0)
{
currentServoPosition3 = currentServoPosition3 - 90; //can change the number to make it move quicker
}
else if (N64_status.stick_y > 90 && currentServoPosition3 >= 0){
//Do nothing the servo is already at its max
}
else if (N64_status.stick_y < -10 && currentServoPosition3 < 180){
currentServoPosition3 = currentServoPosition3 + 10; //can change the number to make it move quicker
}
else if (N64_status.stick_y < -10 && currentServoPosition3 >= 180){
//Do nothing the servo is already at its max
}
else{
//Do nothing Stick is roughly centre
}
myservo3.write(currentServoPosition3);
}