3本ローラーのスマートトレーナー化(その8)

 プログラム(Arduino系のマイコンでは「スケッチ」という。)の処理で行き詰っている。

 つまり、

1)ブルートゥースで電磁石の強度やLEDの点滅速度をコントロールするスケッチはうまく動いた(シリアル通信を使えばいい。)。

2)ADコンバータからの入力により、LEDや電磁石をオンオフしたり、極性を変更することはできた。

 ところが、

3)ブルートゥースからの指示により、ADコンバータからの入力によりオンオフしている電磁石の強度を変更する。

ことができない。具体的には電磁石の強度は変更できるのだが、ADコンバータからの入力が効いていない。

 プログラムの途中で変数を吐き出してやってチェックしたところ、ADコンバータが連続して入力を監視し続けるかと思いきや、最初の1回しか動作していないことがわかった。

 おそらく、シリアル通信とADコンバータ処理を同時並行的に実施できないことが原因。疑似的なスレッド処理というのを行う必要があると思われる。

 たぶん、こういうやつ。
Arduinoでマルチタスクっぽく実行する方法 - Qiita

 備忘録代わりに、動かないプログラムを貼っておく。

/**********************************************************************
  Filename    : Smart_Trainer.ino
  Description : Smartize Roller Trainer for esp32.
  Auther      : EibbleCrMo
  Modification: 2022/12/26
**********************************************************************/
#include "BluetoothSerial.h"
#include "string.h"

#define PIN_LED1 0
#define PIN_LED2 2
#define CHN1 0     //define the pwm channel
#define CHN2 1     //define the pwm channel
#define FRQ 1000   //define the pwm frequency
#define PWM_BIT 6  //define the pwm precision
#define PIN_ANALOG_IN 4

BluetoothSerial SerialBT;
char buffer[20];
static int count = 0;
void setup() {
  Serial.begin(115200);
  ledcSetup(CHN1, FRQ, PWM_BIT);   //setup pwm channel
  ledcSetup(CHN2, FRQ, PWM_BIT);   //setup pwm channe2
  ledcAttachPin(PIN_LED1, CHN1);   //attach the led pin to pwm channel
  ledcAttachPin(PIN_LED2, CHN2);   //attach the led pin to pwm channe2
  SerialBT.begin("SmartTrainer");  //Bluetooth device name
}

void loop() {
  int adcVal;
  while (SerialBT.available()) {
    buffer[count] = SerialBT.read();
    count++;
  }
  if (count > 0) {
    magnet_drive(buffer);
  }

  count = 0;
  memset(buffer, 0, 20);
}

void magnet_drive(char *buffer) {
  int adcVal = analogRead(PIN_ANALOG_IN);
  Serial.print(adcVal);
  if (strncmp(buffer, "0", 1) == 0) {
    if (adcVal > 2900) {
      ledcWrite(CHN1, 0);
      ledcWrite(CHN2, 0);
    } else if (adcVal < 2860) {
      ledcWrite(CHN1, 0);
      ledcWrite(CHN2, 0);
    } else {
      ledcWrite(CHN1, 0);
      ledcWrite(CHN2, 0);
    }
  }

  if (strncmp(buffer, "1", 1) == 0) {
    if (adcVal > 2900) {
      ledcWrite(CHN1, 7);
      ledcWrite(CHN2, 0);
    } else if (adcVal < 2860) {
      ledcWrite(CHN1, 0);
      ledcWrite(CHN2, 7);
    } else {
      ledcWrite(CHN1, 0);
      ledcWrite(CHN2, 0);
    }
  }

  if (strncmp(buffer, "2", 1) == 0) {
    if (adcVal > 2900) {
      ledcWrite(CHN1, 14);
      ledcWrite(CHN2, 0);
    } else if (adcVal < 2860) {
      ledcWrite(CHN1, 0);
      ledcWrite(CHN2, 14);
    } else {
      ledcWrite(CHN1, 0);
      ledcWrite(CHN2, 0);
    }
  }

  if (strncmp(buffer, "3", 1) == 0) {
    if (adcVal > 2900) {
      ledcWrite(CHN1, 21);
      ledcWrite(CHN2, 0);
    } else if (adcVal < 2860) {
      ledcWrite(CHN1, 0);
      ledcWrite(CHN2, 21);
    } else {
      ledcWrite(CHN1, 0);
      ledcWrite(CHN2, 0);
    }
  }

  if (strncmp(buffer, "4", 1) == 0) {
    if (adcVal > 2900) {
      ledcWrite(CHN1, 28);
      ledcWrite(CHN2, 0);
    } else if (adcVal < 2860) {
      ledcWrite(CHN1, 0);
      ledcWrite(CHN2, 28);
    } else {
      ledcWrite(CHN1, 0);
      ledcWrite(CHN2, 0);
    }
  }

  if (strncmp(buffer, "5", 1) == 0) {
    if (adcVal > 2900) {
      ledcWrite(CHN1, 35);
      ledcWrite(CHN2, 0);
    } else if (adcVal < 2860) {
      ledcWrite(CHN1, 0);
      ledcWrite(CHN2, 35);
    } else {
      ledcWrite(CHN1, 0);
      ledcWrite(CHN2, 0);
    }
  }

  if (strncmp(buffer, "6", 1) == 0) {
    if (adcVal > 2900) {
      ledcWrite(CHN1, 42);
      ledcWrite(CHN2, 0);
    } else if (adcVal < 2860) {
      ledcWrite(CHN1, 0);
      ledcWrite(CHN2, 42);
    } else {
      ledcWrite(CHN1, 0);
      ledcWrite(CHN2, 0);
    }
  }

  if (strncmp(buffer, "7", 1) == 0) {
    if (adcVal > 2900) {
      ledcWrite(CHN1, 49);
      ledcWrite(CHN2, 0);
    } else if (adcVal < 2860) {
      ledcWrite(CHN1, 0);
      ledcWrite(CHN2, 49);
    } else {
      ledcWrite(CHN1, 0);
      ledcWrite(CHN2, 0);
    }
  }

  if (strncmp(buffer, "8", 1) == 0) {
    if (adcVal > 2900) {
      ledcWrite(CHN1, 56);
      ledcWrite(CHN2, 0);
    } else if (adcVal < 2860) {
      ledcWrite(CHN1, 0);
      ledcWrite(CHN2, 56);
    } else {
      ledcWrite(CHN1, 0);
      ledcWrite(CHN2, 0);
    }
  }

  if (strncmp(buffer, "9", 1) == 0) {
    if (adcVal > 2900) {
      ledcWrite(CHN1, 63);
      ledcWrite(CHN2, 0);
    } else if (adcVal < 2860) {
      ledcWrite(CHN1, 0);
      ledcWrite(CHN2, 63);
    } else {
      ledcWrite(CHN1, 0);
      ledcWrite(CHN2, 0);
    }
  }
  if (strncmp(buffer, "-1", 2) == 0) {
    if (adcVal > 2900) {
      ledcWrite(CHN1, 0);
      ledcWrite(CHN2, 7);
    } else if (adcVal < 2860) {
      ledcWrite(CHN1, 7);
      ledcWrite(CHN2, 0);
    } else {
      ledcWrite(CHN1, 0);
      ledcWrite(CHN2, 0);
    }
  }

  if (strncmp(buffer, "-2", 2) == 0) {
    if (adcVal > 2900) {
      ledcWrite(CHN1, 0);
      ledcWrite(CHN2, 14);
    } else if (adcVal < 2860) {
      ledcWrite(CHN1, 14);
      ledcWrite(CHN2, 0);
    } else {
      ledcWrite(CHN1, 0);
      ledcWrite(CHN2, 0);
    }
  }

  if (strncmp(buffer, "-3", 2) == 0) {
    if (adcVal > 2900) {
      ledcWrite(CHN1, 0);
      ledcWrite(CHN2, 21);
    } else if (adcVal < 2860) {
      ledcWrite(CHN1, 21);
      ledcWrite(CHN2, 0);
    } else {
      ledcWrite(CHN1, 0);
      ledcWrite(CHN2, 0);
    }
  }

  if (strncmp(buffer, "-4", 2) == 0) {
    if (adcVal > 2900) {
      ledcWrite(CHN1, 0);
      ledcWrite(CHN2, 28);
    } else if (adcVal < 2860) {
      ledcWrite(CHN1, 28);
      ledcWrite(CHN2, 0);
    } else {
      ledcWrite(CHN1, 0);
      ledcWrite(CHN2, 0);
    }
  }

  if (strncmp(buffer, "-5", 2) == 0) {
    if (adcVal > 2900) {
      ledcWrite(CHN1, 0);
      ledcWrite(CHN2, 35);
    } else if (adcVal < 2860) {
      ledcWrite(CHN1, 35);
      ledcWrite(CHN2, 0);
    } else {
      ledcWrite(CHN1, 0);
      ledcWrite(CHN2, 0);
    }
  }

  if (strncmp(buffer, "-6", 2) == 0) {
    if (adcVal > 2900) {
      ledcWrite(CHN1, 0);
      ledcWrite(CHN2, 42);
    } else if (adcVal < 2860) {
      ledcWrite(CHN1, 42);
      ledcWrite(CHN2, 0);
    } else {
      ledcWrite(CHN1, 0);
      ledcWrite(CHN2, 0);
    }
  }

  if (strncmp(buffer, "-7", 2) == 0) {
    if (adcVal > 2900) {
      ledcWrite(CHN1, 0);
      ledcWrite(CHN2, 49);
    } else if (adcVal < 2860) {
      ledcWrite(CHN1, 49);
      ledcWrite(CHN2, 0);
    } else {
      ledcWrite(CHN1, 0);
      ledcWrite(CHN2, 0);
    }
  }

  if (strncmp(buffer, "-8", 2) == 0) {
    if (adcVal > 2900) {
      ledcWrite(CHN1, 0);
      ledcWrite(CHN2, 56);
    } else if (adcVal < 2860) {
      ledcWrite(CHN1, 56);
      ledcWrite(CHN2, 0);
    } else {
      ledcWrite(CHN1, 0);
      ledcWrite(CHN2, 0);
    }
  }

  if (strncmp(buffer, "-9", 2) == 0) {
    if (adcVal > 2900) {
      ledcWrite(CHN1, 0);
      ledcWrite(CHN2, 63);
    } else if (adcVal < 2860) {
      ledcWrite(CHN1, 63);
      ledcWrite(CHN2, 0);
    } else {
      ledcWrite(CHN1, 0);
      ledcWrite(CHN2, 0);
    }
  }
}