Conditions with switch case

Looking for some help with creating a persistent condition while using a switch case within a class.
I have to admit that I am over my head.
Right now I have an iOS app that is transmitting over bluetooth, commands to move a robotic arm. All of that is working fine, however, part of the program is that you Arte supposed to flip a switch on the iOS program that tells the arm to delay an incoming commands.
Activating the switch send a hex code of 0x18.

Basically I need a way to recognize that the switch has been flipped, and run a separate piece of code until the switch is flipped again.
Below is my current code and any help is appreciated!


#include <M5Stack.h>
#include <MyCobotProBasic.h>
#include <ParameterProList.h>
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>

#define PERIPHERAL_NAME             "mrRobot Peripheral"
#define SERVICE_NAME                "mrRobot service"
#define SERVICE_UUID                "2220"
#define CHARACTERISTIC_UUID   "2222"
//#define CHARACTERISTIC_OUTPUT_UUID  "2221" //this is to SEND to iPad but doesn't work for whate

MyCobotProBasic myCobotpro;

Angles angles = { -0.0, -0.0, -0.0, -0.0, -0.0, -0.0 };


int moveSpeed = 15;



// Current value of output characteristic persisted here
//static uint8_t outputData[1];
//static uint8_t inputData[1];



// Class defines methods called when a device connects and disconnects from the service
class ServerCallbacks: public BLEServerCallbacks {
    void onConnect(BLEServer* pServer) {
      Serial.println("BLE Client Connected");
      M5.Lcd.fillScreen(BLACK);
      M5.Lcd.setCursor(0, 0);
      M5.Lcd.println("BLE Client Connected");
    }
    void onDisconnect(BLEServer* pServer) {
      BLEDevice::startAdvertising();
      Serial.println("BLE Client Disconnected");
      M5.Lcd.println("BLE Client Disconnected");
    }
};

class InputReceivedCallbacks: public BLECharacteristicCallbacks {
    void onWrite(BLECharacteristic *pCharWriteState) {
      uint8_t *inputValues = pCharWriteState->getData();


      switch (*inputValues) {
        case 0x00: // button UP
          Serial.println("Moving UP");
          M5.Lcd.fillScreen(BLACK);
          M5.Lcd.setCursor(0, 0);
          M5.Lcd.println("Moving UP");
          myCobotpro.jogAngle(3, 1, moveSpeed);
          //joint2 = myCobotpro.getEncoder(2);
          break;
        case 0x10: // button UP STOP
          Serial.println("STOP MOVING UP");
          M5.Lcd.fillScreen(BLACK);
          M5.Lcd.setCursor(0, 0);
          M5.Lcd.println("STOP MOVING UP");
          myCobotpro.jogStop();
          joint2 = myCobotpro.getEncoder(2);
          joint2 = myCobotpro.getEncoder(2);
          Serial.print(joint2);
          Serial.println("   ");
          break;
        case 0x01: // button DOWN
          Serial.println("Moving DOWN");
          M5.Lcd.fillScreen(BLACK);
          M5.Lcd.setCursor(0, 0);
          M5.Lcd.println("Moving DOWN");
          myCobotpro.jogAngle(3, 0, moveSpeed);
          //joint2 = myCobotpro.getEncoder(2);
          break;
        case 0x11: // button DOWN STOP
          Serial.println("STOP MOVING DOWN");
          M5.Lcd.fillScreen(BLACK);
          M5.Lcd.setCursor(0, 0);
          M5.Lcd.println("STOP MOVING DOWN");
          myCobotpro.jogStop();
          joint2 = myCobotpro.getEncoder(2);
          joint2 = myCobotpro.getEncoder(2);
          Serial.print(joint2);
          Serial.println("   ");
          break;
        case 0x02: // button LEFT
          Serial.println("Moving LEFT");
          M5.Lcd.fillScreen(BLACK);
          M5.Lcd.setCursor(0, 0);
          M5.Lcd.println("Moving LEFT");
          myCobotpro.jogAngle(1, 1, moveSpeed);
          //joint1 = myCobotpro.getEncoder(1);
          break;
        case 0x12: // button LEFT STOP
          Serial.println("STOP MOVING LEFT");
          M5.Lcd.fillScreen(BLACK);
          M5.Lcd.setCursor(0, 0);
          M5.Lcd.println("STOP MOVING LEFT");
          myCobotpro.jogStop();
          joint1 = myCobotpro.getEncoder(1);
          joint1 = myCobotpro.getEncoder(1);
          Serial.print(joint1);
          Serial.println("   ");
          break;
        case 0x03: // button RIGHT
          Serial.println("Moving RIGHT");
          M5.Lcd.fillScreen(BLACK);
          M5.Lcd.setCursor(0, 0);
          M5.Lcd.println("Moving RIGHT");
          myCobotpro.jogAngle(1, 0, moveSpeed);
          //joint1 = myCobotpro.getEncoder(1);
          break;
        case 0x13: // button RIGHT STOP
          Serial.println("STOP MOVING RIGHT");
          M5.Lcd.fillScreen(BLACK);
          M5.Lcd.setCursor(0, 0);
          M5.Lcd.println("STOP MOVING RIGHT");
          myCobotpro.jogStop();
          joint1 = myCobotpro.getEncoder(1);
          joint1 = myCobotpro.getEncoder(1);
          Serial.print(joint1);
          Serial.println("   ");
          break;
        case 0x04: // button vertical OUT
          Serial.println("Moving OUT");
          M5.Lcd.fillScreen(BLACK);
          M5.Lcd.setCursor(0, 0);
          M5.Lcd.println("Moving OUT");
          myCobotpro.jogAngle(4 , 1, moveSpeed);
          //myCobotpro.jogAngle(4, 1, moveSpeed);
          //joint3 = myCobotpro.getEncoder(3);
          //joint4 = myCobotpro.getEncoder(4);
          break;
        case 0x14: // button OUT STOP
          Serial.println("STOP MOVING OUT");
          M5.Lcd.fillScreen(BLACK);
          M5.Lcd.setCursor(0, 0);
          M5.Lcd.println("STOP MOVING OUT");
          myCobotpro.jogStop();
          joint4 = myCobotpro.getEncoder(4);
          joint4 = myCobotpro.getEncoder(4);
          Serial.print(joint4);
          Serial.println("   ");
          break;
        case 0x05: // button IN
          Serial.println("Moving IN");
          M5.Lcd.fillScreen(BLACK);
          M5.Lcd.setCursor(0, 0);
          M5.Lcd.println("Moving IN");
          myCobotpro.jogAngle(4, 0, moveSpeed);
          //myCobotpro.jogAngle(4, 0, moveSpeed);
          break;
        case 0x15: // button IN
          Serial.println("STOP MOVING IN");
          M5.Lcd.fillScreen(BLACK);
          M5.Lcd.setCursor(0, 0);
          M5.Lcd.println("STOP MOVING IN");
          myCobotpro.jogStop();
          joint4 = myCobotpro.getEncoder(4);
          Serial.print(joint4);
          Serial.println("   ");
          break;
        case 0x06: // button rotate left
          Serial.println("Rotate Left");
          M5.Lcd.fillScreen(BLACK);
          M5.Lcd.setCursor(0, 0);
          M5.Lcd.println("Rotate Left");
          myCobotpro.jogAngle(6, 1, moveSpeed);
          //joint6 = myCobotpro.getEncoder(6);
          break;
        case 0x16: // button rotate left Stop
          Serial.println("Rotate Left Stop");
          M5.Lcd.fillScreen(BLACK);
          M5.Lcd.setCursor(0, 0);
          M5.Lcd.println("Rotate Left Stop");
          myCobotpro.jogStop();
          //joint6 = myCobotpro.getEncoder(6);
          break;
        case 0x07: // button rotate right
          Serial.println("Rotate Right");
          M5.Lcd.fillScreen(BLACK);
          M5.Lcd.setCursor(0, 0);
          M5.Lcd.println("Rotate Right");
          myCobotpro.jogAngle(6, 0, moveSpeed);
          //joint6 = myCobotpro.getEncoder(6);
          break;
        case 0x17: // button rotate right Stop
          Serial.println("Rotate Right Stop");
          M5.Lcd.fillScreen(BLACK);
          M5.Lcd.setCursor(0, 0);
          M5.Lcd.println("Rotate Right Stop");
          myCobotpro.jogStop();
          //joint6 = myCobotpro.getEncoder(6);
          break;
        case 0x18: // 4g mode
          Serial.println("Starting 4g Mode");
          M5.Lcd.fillScreen(BLACK);
          M5.Lcd.setCursor(0, 0);
          M5.Lcd.println("Starting 4g Mode");
          break;
        case 0x08: // 5g mode
          Serial.println("Starting 5g Mode");
          M5.Lcd.fillScreen(BLACK);
          M5.Lcd.setCursor(0, 0);
          M5.Lcd.println("Starting 5g Mode");
          break;

      }
    }

};

void setup() {
  // Use the Arduino serial monitor set to this baud rate to view BLE peripheral logs
  Serial.begin(115200);
  Serial.println("Begin Setup BLE Service and Characteristics");

  // Configure thes server

  BLEDevice::init(PERIPHERAL_NAME);
  BLEServer *pServer = BLEDevice::createServer();

  // Create the service
  BLEService *pService = pServer->createService(SERVICE_UUID);

  // Create a characteristic for the service
  BLECharacteristic *pInputChar = pService->createCharacteristic(
                                    CHARACTERISTIC_UUID,
                                    BLECharacteristic::PROPERTY_WRITE_NR | BLECharacteristic::PROPERTY_WRITE);





  // Hook callback to report server events
  pServer->setCallbacks(new ServerCallbacks());
  pInputChar->setCallbacks(new InputReceivedCallbacks());


  // Start the service
  pService->start();

  // Advertise the service
  BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  pAdvertising->addServiceUUID(SERVICE_UUID);
  pAdvertising->setScanResponse(true);
  pAdvertising->setMinPreferred(0x06);
  pAdvertising->setMinPreferred(0x12);
  BLEDevice::startAdvertising();

  Serial.println("BLE Service is advertising");

  M5.begin();
  myCobotpro.setup();

  myCobotpro.powerOn();




//set servos to home position
  myCobotpro.writeAngle(2, 0, 50); //Set to home position
  delay(2000);
  myCobotpro.writeAngle(3, 0, 50); //Set to home position
  delay(2000);
  myCobotpro.writeAngle(4, 0, 50); //Set to home position
  delay(2000);
  myCobotpro.writeAngle(6, 0, 50); //Set to home position
  delay(2000);
  myCobotpro.writeAngle(5, 0, 50); //Set to home position
  delay(2000);
  myCobotpro.writeAngle(1, 0, 50); //Set to home position
  delay(2000);

}


void loop() {


}


So you can write all that code, but you are not able to create a simple program pause? Looks like the code for 0x18 is already used to start a 4g mode, right?

Found most of the code and modified.
I've tried several ways to get it to work but I think the problem I am having is reading that specific variable and storing it's state.
I've tried creating variables within the class as well as in the loop, I've use if/else within and outside the class.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.