How to take 1 bit Input from Bluetooth using ESP32

Hey every one ,
i am trying to program my ESP32 in the way i can send 1 bit input (a number) from Bluetooth Serial to play some effects on my NeoPixel strip but it seems that i am getting a wrong input and don't know what is exactly the problem .
maybe someone can help me
thanx in advance

P.S. : that is my try till now

#include "BluetoothSerial.h"

#include <Adafruit_NeoPixel.h>
BluetoothSerial BT;

#define PIXEL_PIN1 22
const int LEDpin = 23;

#define PIXEL_COUNT 420

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN1, NEO_GRB + NEO_KHZ800);
int showType = 0;
int a;
uint32_t c;
int b[8][3] = {(0, 0, 0), (255, 0, 0),(0, 255, 0), (0, 0, 255), (255, 255, 255), (209, 181, 255), (217, 160, 255), (140, 231, 120)};

void setup() {
Serial.begin(115200);
BT.begin("ESP32test"); //Bluetooth device name
strip.begin();
color(strip.Color(209, 181, 255));
delay(100);
}

void loop() {
// might be time consuming
if (BT.available())
{
a = (int)BT.read();
for (int i = 0 ; i < 3; i++)
Serial.println(b[a][i]);
color(strip.Color(b[a][0], b[a][1], b[a][2]));
if (a == 8)
{
rainbowCycle();
}
}
}

void color(uint32_t c) {
for (uint16_t i = 0; i < strip.numPixels(); i++) {
strip.fill(c, i);
strip.show();
}
}
void rainbowCycle() {
uint16_t i, j;

for (j = 0; j < 256 * 5; j++) { // 5 cycles of all colors on wheel
for (i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
}
strip.show();
}
}
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if (WheelPos < 85) {
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
if (WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}

Hello, do yourself a favour and please read How to get the best out of this forum and post your code accordingly (including code tags and necessary documentation for your ask).

So you either have the value 0 or the value 1 ?

I suggest that you put some more serial prints in ypur code; e.g.

  if (BT.available())
  {
    a = (int)BT.read();
    Serial.print("Received: 0x");
    Serial.println(a, HEX);

What does it print? 0x8 or 0x38? In the first case, you're receiving the number 8, in the second case your receiving the character 8.

This is a two-dimensional array; the range of a in b[a][0] is 0 .. 7.

don't know if sterretje made is clear that read() returns an ASCII character. the "(int)" simply insures that it is treated as a 2 byte integer, it doesn't convert it to a binary value. one approach is

a = BT.read() - '0';

but the code should also verify that the value is between 0-2 before attempting to print the values from b[][]

this is useless (but harmless). read() returns an int and a is an int... so no casting is necessary...

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