I am using an XIAO esp32c3 board with a battery and a small coin motor attached to the 3.3v pin and pin 7. I am relatively new and am still learning. My project goal is to sync led lights to vibration pulses. I am trying to modify an example I found to get my desired result but I don't think I fully understand how BLE communication works. Right now my app will send numbers to my arduino. I am trying to convert those strings to ints and then have the arduino react based on what those numbers are. But no matter where I put my if statements, they do not seem to execute. I have tried several different methods for reading and acting on the input from the app with no success. In my following code, the entire void loop seems to not execute. I tried using Serial.println as an indicator for if my code executes or not. I have read some on how BLE communication works but maybe I haven't stumbled across the proper material yet.
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#define motorPin 7
#define PWM1_Ch 0
#define PWM1_Res 4
#define PWM1_Freq 100
int vibeSpeed = 5;
int vibeStrength = 15;
int vibePause = 25;
String vibeState = "";
int numVibe = 1;
// See the following for generating UUIDs:
// https://www.uuidgenerator.net/
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
class MyCallbacks: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
std::string value = pCharacteristic->getValue();
if (value.length() > 0) {
//Serial.println("*********");
// Serial.print("New value: ");
for (int i = 0; i < value.length(); i++)
Serial.print(value[i]);
}
}
};
void setup() {
Serial.begin(9600);
ledcAttachPin(motorPin, 0);
ledcSetup(0, 100, 4);
pinMode(7, OUTPUT);
BLEDevice::init("Tapper V1.2");
BLEServer *pServer = BLEDevice::createServer();
BLEService *pService = pServer->createService(SERVICE_UUID);
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);
pCharacteristic->setCallbacks(new MyCallbacks());
pCharacteristic->setValue("Hello World");
pService->start();
BLEAdvertising *pAdvertising = pServer->getAdvertising();
pAdvertising->start();
}
void loop() {
while (Serial.available()==0) {}
vibeState = Serial.readString();
numVibe = vibeState.toInt();
Serial.println(numVibe);
if (numVibe = 1) {
ledcWrite(0, 0);
}
else if (numVibe > 1 && numVibe <= 10) {
vibeSpeed = numVibe*100;
Serial.println(vibeSpeed);
}
else if (numVibe >= 11 && numVibe <=20) {
vibeStrength = numVibe*10;
Serial.println(vibeStrength);
}
else if (numVibe >=21 && numVibe <= 30) {
vibePause = numVibe*50;
Serial.println(vibePause);
}
ledcWrite(0, vibeStrength);
delay(vibeSpeed);
ledcWrite(0, 0);
delay(vibePause);
delay(vibeSpeed);
}
if (numVibe = 1)
Did you lose an equals sign somewhere ?
Nope, out of stock at his local keyboard... Happy Saturday, Everyone!
I suppose so, thanks for finding it for me. I went ahead and added that but the entire void loop still doesn't execute.
amitf
November 26, 2022, 3:51pm
5
because of this i think your code isn't running
while (Serial.available()==0) {}
amitf
November 26, 2022, 4:02pm
6
while (Serial.available()==0) {}
vibeState = Serial.readString();
instead of the above code you should use
if (Serial.available()==1) {
vibeState = Serial.readString();
}
Okay I made that change, still nothing. my entire void loop is just not working. When I plug my arduino in and upload the sketch the light just comes on at full brightness and remains that way.
Please post your sketch as it is now
I have tried both of the variations below, both of which will print only the value[i] repeatedly to the Serial monitor.
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#define motorPin 7
#define PWM1_Ch 0
#define PWM1_Res 4
#define PWM1_Freq 100
int vibeSpeed = 5;
int vibeStrength = 15;
int vibePause = 25;
String vibeState = "";
int numVibe = 1;
// See the following for generating UUIDs:
// https://www.uuidgenerator.net/
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
class MyCallbacks: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
std::string value = pCharacteristic->getValue();
if (value.length() > 0) {
//Serial.println("*********");
// Serial.print("New value: ");
for (int i = 0; i < value.length(); i++)
Serial.print(value[i]);
vibeState = value.c_str();
}
numVibe = vibeState.toInt();
Serial.println(numVibe);
if (numVibe == 1) {
ledcWrite(0, 0);
}
else if (numVibe > 1 && numVibe <= 10) {
vibeSpeed = numVibe*100;
Serial.println(vibeSpeed);
}
else if (numVibe >= 11 && numVibe <=20) {
vibeStrength = numVibe*10;
Serial.println(vibeStrength);
}
else if (numVibe >=21 && numVibe <= 30) {
vibePause = numVibe*50;
Serial.println(vibePause);
}
ledcWrite(0, vibeStrength);
delay(vibeSpeed);
ledcWrite(0, 0);
delay(vibePause);
delay(vibeSpeed);
}
};
void setup() {
Serial.begin(9600);
ledcAttachPin(7, 0);
ledcSetup(0, 100, 4);
pinMode(7, OUTPUT);
ledcWrite(0, 0);
BLEDevice::init("Tapper V1.2");
BLEServer *pServer = BLEDevice::createServer();
BLEService *pService = pServer->createService(SERVICE_UUID);
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);
pCharacteristic->setCallbacks(new MyCallbacks());
pCharacteristic->setValue("Hello World");
pService->start();
BLEAdvertising *pAdvertising = pServer->getAdvertising();
pAdvertising->start();
}
void loop() {
}
and
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#define motorPin 7
#define PWM1_Ch 0
#define PWM1_Res 4
#define PWM1_Freq 100
int vibeSpeed = 5;
int vibeStrength = 15;
int vibePause = 25;
String vibeState = "";
int numVibe = 1;
// See the following for generating UUIDs:
// https://www.uuidgenerator.net/
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
class MyCallbacks: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
std::string value = pCharacteristic->getValue();
if (value.length() > 0) {
//Serial.println("*********");
// Serial.print("New value: ");
for (int i = 0; i < value.length(); i++)
Serial.print(value[i]);
//vibeState = value.c_str();
}
}
};
void setup() {
Serial.begin(9600);
ledcAttachPin(7, 0);
ledcSetup(0, 100, 4);
pinMode(7, OUTPUT);
ledcWrite(0, 0);
BLEDevice::init("Tapper V1.2");
BLEServer *pServer = BLEDevice::createServer();
BLEService *pService = pServer->createService(SERVICE_UUID);
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);
pCharacteristic->setCallbacks(new MyCallbacks());
pCharacteristic->setValue("Hello World");
pService->start();
BLEAdvertising *pAdvertising = pServer->getAdvertising();
pAdvertising->start();
}
void loop() {
if (Serial.available()==1) {
vibeState = Serial.readString();
numVibe = vibeState.toInt();
Serial.println(numVibe);
}
if (numVibe == 1) {
ledcWrite(0, 0);
}
else if (numVibe > 1 && numVibe <= 10) {
vibeSpeed = numVibe*100;
Serial.println(vibeSpeed);
}
else if (numVibe >= 11 && numVibe <=20) {
vibeStrength = numVibe*10;
Serial.println(vibeStrength);
}
else if (numVibe >=21 && numVibe <= 30) {
vibePause = numVibe*50;
Serial.println(vibePause);
}
ledcWrite(0, vibeStrength);
delay(vibeSpeed);
ledcWrite(0, 0);
delay(vibePause);
delay(vibeSpeed);
}```
amitf
November 27, 2022, 11:23am
10
Can you share the link from where you have copied the code
And also please upload your circuit diagram