I'm working on a quadruped robot and I'm trying to use the serial console to control the legs. I'm using a switch command to determine the joint to move and I want to use a second command to determine what the motor at that joint does. However, I'm finding the pwm.setPWM(...); functions aren't working in each switch case. I added some cases without other arguments and no motion. Can this function work in a switch? Are there other ways to read commands through serial like cin>>var?
Below is my code, in case it helps
Code: Select all
/* Serial Servo Controller for Quadruped Robot
* Use the Serial.availible() argument to control legs
* individually.
*
*/
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
int Knee1state[] = {4, 0, 0};
int Knee2state[] = {3, 0, 0};
int Knee3state[] = {0, 0, 0};
int Knee4state[] = {7, 0, 0};
int Hip1state[] = {5, 0, 0};
int Hip2state[] = {2, 0, 0};
int Hip3state[] = {1, 0, 0};
int Hip4state[] = {8, 0, 0};
char Joint = 0;
char action = 0;
void setup() {
pwm.begin();
pwm.setPWMFreq(60);
delay(10);
Serial.begin(9600);
Serial.println("Booting up");
delay(500);
Serial.println("BANNED live");
delay(500);
Serial.println("Lol, jk");
}
void loop() {
if(Serial.available()>0){
Joint = Serial.read();
Serial.flush();
switch(Joint) {
case 'q': //hip1
Serial.println("hip1");
while(Serial.available()>0){
action = Serial.read();
Serial.flush();
switch(action) {
case 'i'://rise
pwm.setPWM(5, 0, 570);
case 'k'://center
pwm.setPWM(5, 0, 365);
case 'm'://lower
pwm.setPWM(5, 0, 160);
case 'l'://kill
pwm.setPWM(0, 0, 0);
}
}
break;
case 'w': //hip2
Serial.println("hip2");
while(Serial.available()>0){
action = Serial.read();
Serial.flush();
switch(action) {
case 'i'://rise
pwm.setPWM(2, 0, 600);
case 'k'://center
pwm.setPWM(2, 0, 360);
case 'm'://lower
pwm.setPWM(2, 0, 120);
case 'l'://kill
pwm.setPWM(2, 0, 0);
}
}
break;
case 'e': //hip3
Serial.println("hip3");
while(Serial.available()>0){
action = Serial.read();
Serial.flush();
switch(action) {
case 'i'://rise
pwm.setPWM(1, 0, 520);
case 'k'://center
pwm.setPWM(1, 0, 323);
case 'm'://lower
pwm.setPWM(1, 0, 125);
case 'l'://kill
pwm.setPWM(1, 0, 0);
}
}
break;
case 'r': //hip4
Serial.println("hip4");
while(Serial.available()>0){
action = Serial.read();
Serial.flush();
switch(action) {
case 'i'://rise
pwm.setPWM(8, 0, 510);
case 'k'://center
pwm.setPWM(8, 0, 318);
case 'm'://lower
pwm.setPWM(8, 0, 125);
case 'l'://kill
pwm.setPWM(8, 0, 0);
}
}
break;
case 'a': //knee1
Serial.println("knee1");
while(Serial.available()>0){
action = Serial.read();
Serial.flush();
switch(action) {
case 'i'://rise
pwm.setPWM(4, 0, 440);
case 'k'://center
pwm.setPWM(4, 0, 360);
case 'm'://lower
pwm.setPWM(4, 0, 280);
case 'l'://kill
pwm.setPWM(4, 0, 0);
}
}
break;
case 's': //knee2
Serial.println("knee2");
while(Serial.available()>0){
action = Serial.read();
Serial.flush();
switch(action) {
case 'i'://rise
pwm.setPWM(3, 0, 460);
case 'k'://center
pwm.setPWM(3, 0, 370);
case 'm'://lower
pwm.setPWM(3, 0, 280);
case 'l'://kill
pwm.setPWM(3, 0, 0);
}
}
break;
case 'd': //knee3
Serial.println("knee3");
while(Serial.available()>0){
action = Serial.read();
Serial.flush();
switch(action) {
case 'i'://rise
pwm.setPWM(0, 0, 416);
case 'k'://center
pwm.setPWM(0, 0, 208);
case 'm'://lower
pwm.setPWM(0, 0, 1);
case 'l'://kill
pwm.setPWM(0, 0, 0);
}
}
break;
case 'f': //knee4
Serial.println("knee4");
while(Serial.available()>0){
action = Serial.read();
Serial.flush();
switch(action) {
case 'i'://rise
pwm.setPWM(7, 0, 380);
case 'k'://center
pwm.setPWM(7, 0, 280);
case 'm'://lower
pwm.setPWM(7, 0, 180);
case 'l'://kill
pwm.setPWM(7, 0, 0);
}
}
break;
case 'z': //fall
Serial.println("Rise");
pwm.setPWM(4, 0, 0);
pwm.setPWM(3, 0, 0);
pwm.setPWM(0, 0, 0);
pwm.setPWM(7, 0, 0);
break;
case 'x': //rise
Serial.println("Fall");
delay(100);
pwm.setPWM(4, 0, 440);
pwm.setPWM(3, 0, 460);
pwm.setPWM(0, 0, 416);
pwm.setPWM(7, 0, 380);
break;
}
}
}