PCA9685: SetPWM switch case

Breakout boards, sensors, other Adafruit kits, etc.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
celt1201
 
Posts: 14
Joined: Fri Jun 30, 2017 7:46 pm

PCA9685: SetPWM switch case

Post by celt1201 »

Hello,
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;
    }
    
  }
}

User avatar
adafruit_support_bill
 
Posts: 88155
Joined: Sat Feb 07, 2009 10:11 am

Re: PCA9685: SetPWM switch case

Post by adafruit_support_bill »

Can this function work in a switch?
Yes. There is nothing to prevent it from working within a switch statement.

If you are driving it from the Serial Monitor, this bit of code will always get the last byte sent - which will be a line feed. Your command byte will be ignored.

Code: Select all

        while(Serial.available()>0){
          action = Serial.read();
          Serial.flush();

User avatar
celt1201
 
Posts: 14
Joined: Fri Jun 30, 2017 7:46 pm

Re: PCA9685: SetPWM switch case

Post by celt1201 »

Ideally, I'd like a command like cin>>var, from c++ that waits for input. Do you know anything like that?
Also, the z and x cases also didn't work, but don't wait for a secondary serial input. Why might that be? I don't have a cap and the PCA9685 board, but I've also edited the code to have a delay between each command to try and alleviate that. Still didn't get motion.

User avatar
adafruit_support_bill
 
Posts: 88155
Joined: Sat Feb 07, 2009 10:11 am

Re: PCA9685: SetPWM switch case

Post by adafruit_support_bill »

Ideally, I'd like a command like cin>>var, from c++ that waits for input. Do you know anything like that?

Code: Select all

while(!Serial.available()) // wait for input
char c = Serial.read();
Also, the z and x cases also didn't work, but don't wait for a secondary serial input. Why might that be?
The code is doing exactly what you are telling it to do. It is reading all the characters that are available - but only storing the last one read. If you are entering characters from Serial Monitor, the last character sent will always be a line feed.

Locked
Please be positive and constructive with your questions and comments.

Return to “Other Products from Adafruit”