SLAVE.ino (3.6 KB)
Yep, your code completely lacks any work to send sensor readings, so there's no point working on the AI2 side.
Search this board or the Web for sample code.
Maybe kio4 has some sample code you can use.
Hi, sorry for the late reply. Attached is the sketch with the sensor readings. Thank you
gg.ino (5.5 KB)
I'm posting your code here for others to see without the need for downloading:
The ino
#include <Arduino.h>
#include <BluetoothSerial.h>
#include <esp_now.h>
#include <WiFi.h>
BluetoothSerial Serial_BT;
// Motor 3
int r_en3 = 15; // +
int l_en3 = 0; // -
int r_pwm3 = 2; // +
int l_pwm3 = 4; // -
// Motor 4
int r_en4 = 16; // +
int l_en4 = 18; // -
int r_pwm4 = 17; // +
int l_pwm4 = 19; // -
//Ultrasonic Sensor Pinouts
int trigPin = 13;
int echoPin = 12;
int distance;
long duration;
int incoming;
uint8_t slaveAddress[] = {0xC8, 0x2E, 0x18, 0x67, 0x36, 0x6C}; // Replace with your slave ESP32 MAC address
void onDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
Serial.print("\r\nLast Packet Send Status:\t");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
if (status != ESP_NOW_SEND_SUCCESS) {
Serial.println("Failed to send data");
}
}
void setup() {
//motor driver;
pinMode(r_en3, OUTPUT);
pinMode(l_en3, OUTPUT);
pinMode(r_pwm3, OUTPUT);
pinMode(l_pwm3, OUTPUT);
pinMode(r_en4, OUTPUT);
pinMode(l_en4, OUTPUT);
pinMode(r_pwm4, OUTPUT);
pinMode(l_pwm4, OUTPUT);
Serial.begin(115200);
Serial_BT.begin("ESP32Test");
Serial.println("The device has started, now you can pair it with Bluetooth");
// Initialize WiFi and ESP-NOW
WiFi.mode(WIFI_STA);
WiFi.disconnect(); // Ensure no connections interfere with ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
esp_now_register_send_cb(onDataSent);
//Register Peer
esp_now_peer_info_t peerInfo;
memcpy(peerInfo.peer_addr, slaveAddress, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
Serial.println("Failed to add peer");
return;
} else {
Serial.println("Peer added successfully");
}
}
void loop() {
while (Serial_BT.available()) {
String Direction = Serial_BT.readStringUntil('\n');
Serial.print("Received Direction: ");
Serial.println(Direction);
esp_err_t result = esp_now_send(slaveAddress, (uint8_t *)Direction.c_str(), Direction.length());
if (result == ESP_OK) {
Serial.println("ESP-NOW message sent successfully");
} else {
Serial.print("Error sending ESP-NOW message: ");
Serial.println(result);
}
if (Direction == "F") {
// Forward
moveForward();
} else if (Direction == "B") {
// Backward
moveBackward();
} else if (Direction == "L") {
// Left
moveLeft();
} else if (Direction == "R") {
// Right
moveRight();
} else if (Direction == "FL") {
// Forward Left
moveForwardLeft();
} else if (Direction == "FR") {
// Forward Right
moveForwardRight();
} else if (Direction == "BL") {
// Backward Left
moveBackwardLeft();
} else if (Direction == "BR") {
// Backward Right
moveBackwardRight();
} else if (Direction == "S") {
// STOP
moveStop();
}
delay(1000);
}
//Ultrasonic Sensor Reading
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
if (distance < 20) { // Distance threshold in centimeters
Serial_BT.write('O'); // 'O' for obstacle detected
delay(500); // Add a delay to avoid constant triggering
}
}
void moveForward() {
digitalWrite(r_en3, HIGH);
digitalWrite(l_en3, HIGH);
analogWrite(r_pwm3, 255);
analogWrite(l_pwm3, 0);
digitalWrite(r_en4, HIGH);
digitalWrite(l_en4, HIGH);
analogWrite(r_pwm4, 255);
analogWrite(l_pwm4, 0);
}
void moveBackward() {
digitalWrite(r_en3, HIGH);
digitalWrite(l_en3, HIGH);
analogWrite(r_pwm3, 0);
analogWrite(l_pwm3, 255);
digitalWrite(r_en4, HIGH);
digitalWrite(l_en4, HIGH);
analogWrite(r_pwm4, 0);
analogWrite(l_pwm4, 255);
}
void moveLeft() {
digitalWrite(r_en3, HIGH);
digitalWrite(l_en3, HIGH);
analogWrite(r_pwm3, 255);
analogWrite(l_pwm3, 0);
digitalWrite(r_en4, HIGH);
digitalWrite(l_en4, HIGH);
analogWrite(r_pwm4, 0);
analogWrite(l_pwm4, 255);
}
void moveRight() {
digitalWrite(r_en3, HIGH);
digitalWrite(l_en3, HIGH);
analogWrite(r_pwm3, 0);
analogWrite(l_pwm3, 255);
digitalWrite(r_en4, HIGH);
digitalWrite(l_en4, HIGH);
analogWrite(r_pwm4, 255);
analogWrite(l_pwm4, 0);
}
void moveForwardLeft() {
digitalWrite(r_en3, HIGH);
digitalWrite(l_en3, HIGH);
analogWrite(r_pwm3, 255);
analogWrite(l_pwm3, 0);
digitalWrite(r_en4, HIGH);
digitalWrite(l_en4, HIGH);
analogWrite(r_pwm4, 0);
analogWrite(l_pwm4, 0);
}
void moveForwardRight() {
digitalWrite(r_en3, HIGH);
digitalWrite(l_en3, HIGH);
analogWrite(r_pwm3, 0);
analogWrite(l_pwm3, 0);
digitalWrite(r_en4, HIGH);
digitalWrite(l_en4, HIGH);
analogWrite(r_pwm4, 255);
analogWrite(l_pwm4, 0);
}
void moveBackwardLeft() {
digitalWrite(r_en3, HIGH);
digitalWrite(l_en3, HIGH);
analogWrite(r_pwm3, 0);
analogWrite(l_pwm3, 0);
digitalWrite(r_en4, HIGH);
digitalWrite(l_en4, HIGH);
analogWrite(r_pwm4, 0);
analogWrite(l_pwm4, 255);
}
void moveBackwardRight() {
digitalWrite(r_en3, HIGH);
digitalWrite(l_en3, HIGH);
analogWrite(r_pwm3, 0);
analogWrite(l_pwm3, 255);
digitalWrite(r_en4, HIGH);
digitalWrite(l_en4, HIGH);
analogWrite(r_pwm4, 0);
analogWrite(l_pwm4, 0);
}
void moveStop() {
digitalWrite(r_en3, LOW);
digitalWrite(l_en3, LOW);
analogWrite(r_pwm3, 0);
analogWrite(l_pwm3, 0);
digitalWrite(r_en4, LOW);
digitalWrite(l_en4, LOW);
analogWrite(r_pwm4, 0);
analogWrite(l_pwm4, 0);
}
So you're sending an 'O' (letter) when nearing an obstacle, and waiting 500 ms before sending the next 'O'. (A wait loop in your sketch is not as good as keeping millisecond time stamps of last alarm, because you want your sketch loop to continue doing whatever it was doing instead of stopping dead for a wait.)
Your AI2 Clock should run twice as fast (250 ms) to keep its input buffer close to empty.
Also, the AI2 Sound component should not linger long, to avoid locking up the app's running thread.
Your AI2 message handling should ask for only 1 byte of text, and test if it equals 'O', and act accordingly (buzz or complain)
Hi, here is the blocks I'm trying on right now with an updated sketch, will this suffice?
#include <Arduino.h>
#include <BluetoothSerial.h>
#include <esp_now.h>
#include <WiFi.h>
BluetoothSerial Serial_BT;
// Motor 3
int r_en3 = 15; // +
int l_en3 = 0; // -
int r_pwm3 = 2; // +
int l_pwm3 = 4; // -
// Motor 4
int r_en4 = 16; // +
int l_en4 = 18; // -
int r_pwm4 = 17; // +
int l_pwm4 = 19; // -
//Ultrasonic Sensor Pinouts
int trigPin = 13;
int echoPin = 12;
int distance;
long duration;
unsigned long previousMillis = 0;
const long interval = 500; // 500ms interval
int incoming;
uint8_t slaveAddress[] = {0xC8, 0x2E, 0x18, 0x67, 0x36, 0x6C}; // Replace with your slave ESP32 MAC address
void onDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
Serial.print("\r\nLast Packet Send Status:\t");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
if (status != ESP_NOW_SEND_SUCCESS) {
Serial.println("Failed to send data");
}
}
void setup() {
//motor driver;
pinMode(r_en3, OUTPUT);
pinMode(l_en3, OUTPUT);
pinMode(r_pwm3, OUTPUT);
pinMode(l_pwm3, OUTPUT);
pinMode(r_en4, OUTPUT);
pinMode(l_en4, OUTPUT);
pinMode(r_pwm4, OUTPUT);
pinMode(l_pwm4, OUTPUT);
Serial.begin(115200);
Serial_BT.begin("ESP32Test");
Serial.println("The device has started, now you can pair it with Bluetooth");
// Initialize WiFi and ESP-NOW
WiFi.mode(WIFI_STA);
WiFi.disconnect(); // Ensure no connections interfere with ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
esp_now_register_send_cb(onDataSent);
//Register Peer
esp_now_peer_info_t peerInfo;
memcpy(peerInfo.peer_addr, slaveAddress, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
Serial.println("Failed to add peer");
return;
} else {
Serial.println("Peer added successfully");
}
}
void loop() {
while (Serial_BT.available()) {
String Direction = Serial_BT.readStringUntil('\n');
Serial.print("Received Direction: ");
Serial.println(Direction);
esp_err_t result = esp_now_send(slaveAddress, (uint8_t *)Direction.c_str(), Direction.length());
if (result == ESP_OK) {
Serial.println("ESP-NOW message sent successfully");
} else {
Serial.print("Error sending ESP-NOW message: ");
Serial.println(result);
}
if (Direction == "F") {
// Forward
moveForward();
} else if (Direction == "B") {
// Backward
moveBackward();
} else if (Direction == "L") {
// Left
moveLeft();
} else if (Direction == "R") {
// Right
moveRight();
} else if (Direction == "FL") {
// Forward Left
moveForwardLeft();
} else if (Direction == "FR") {
// Forward Right
moveForwardRight();
} else if (Direction == "BL") {
// Backward Left
moveBackwardLeft();
} else if (Direction == "BR") {
// Backward Right
moveBackwardRight();
} else if (Direction == "S") {
// STOP
moveStop();
}
delay(1000);
}
unsigned long currentMillis = millis();
//Ultrasonic Sensor Reading
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
if (distance < 20 && currentMillis - previousMillis >= interval) {
Serial_BT.write('O');
previousMillis = currentMillis; // Update time stamp
}
}
void moveForward() {
digitalWrite(r_en3, HIGH);
digitalWrite(l_en3, HIGH);
analogWrite(r_pwm3, 255);
analogWrite(l_pwm3, 0);
digitalWrite(r_en4, HIGH);
digitalWrite(l_en4, HIGH);
analogWrite(r_pwm4, 255);
analogWrite(l_pwm4, 0);
}
void moveBackward() {
digitalWrite(r_en3, HIGH);
digitalWrite(l_en3, HIGH);
analogWrite(r_pwm3, 0);
analogWrite(l_pwm3, 255);
digitalWrite(r_en4, HIGH);
digitalWrite(l_en4, HIGH);
analogWrite(r_pwm4, 0);
analogWrite(l_pwm4, 255);
}
void moveLeft() {
digitalWrite(r_en3, HIGH);
digitalWrite(l_en3, HIGH);
analogWrite(r_pwm3, 255);
analogWrite(l_pwm3, 0);
digitalWrite(r_en4, HIGH);
digitalWrite(l_en4, HIGH);
analogWrite(r_pwm4, 0);
analogWrite(l_pwm4, 255);
}
void moveRight() {
digitalWrite(r_en3, HIGH);
digitalWrite(l_en3, HIGH);
analogWrite(r_pwm3, 0);
analogWrite(l_pwm3, 255);
digitalWrite(r_en4, HIGH);
digitalWrite(l_en4, HIGH);
analogWrite(r_pwm4, 255);
analogWrite(l_pwm4, 0);
}
void moveForwardLeft() {
digitalWrite(r_en3, HIGH);
digitalWrite(l_en3, HIGH);
analogWrite(r_pwm3, 255);
analogWrite(l_pwm3, 0);
digitalWrite(r_en4, HIGH);
digitalWrite(l_en4, HIGH);
analogWrite(r_pwm4, 0);
analogWrite(l_pwm4, 0);
}
void moveForwardRight() {
digitalWrite(r_en3, HIGH);
digitalWrite(l_en3, HIGH);
analogWrite(r_pwm3, 0);
analogWrite(l_pwm3, 0);
digitalWrite(r_en4, HIGH);
digitalWrite(l_en4, HIGH);
analogWrite(r_pwm4, 255);
analogWrite(l_pwm4, 0);
}
void moveBackwardLeft() {
digitalWrite(r_en3, HIGH);
digitalWrite(l_en3, HIGH);
analogWrite(r_pwm3, 0);
analogWrite(l_pwm3, 0);
digitalWrite(r_en4, HIGH);
digitalWrite(l_en4, HIGH);
analogWrite(r_pwm4, 0);
analogWrite(l_pwm4, 255);
}
void moveBackwardRight() {
digitalWrite(r_en3, HIGH);
digitalWrite(l_en3, HIGH);
analogWrite(r_pwm3, 0);
analogWrite(l_pwm3, 255);
digitalWrite(r_en4, HIGH);
digitalWrite(l_en4, HIGH);
analogWrite(r_pwm4, 0);
analogWrite(l_pwm4, 0);
}
void moveStop() {
digitalWrite(r_en3, LOW);
digitalWrite(l_en3, LOW);
analogWrite(r_pwm3, 0);
analogWrite(l_pwm3, 0);
digitalWrite(r_en4, LOW);
digitalWrite(l_en4, LOW);
analogWrite(r_pwm4, 0);
analogWrite(l_pwm4, 0);
}
I see several problems.
- You initialize the received message variable to 'O', so your app is going to buzz immediately. Better to initialize it to blank.
- Read the tool tip for Clock1.TimerInterval. It is in milliseconds, not seconds, so you are setting the clock to run 1000 times too fast. Just use 250.
- You don't need to reset the Interval every time it fires. You can do it once in Screen1.Initialize or the Designer's Clock1 Attributes.
- You have to wrap that ReceiveText sequence with a test if BytesAvailable > 0. Don't attempt a ReceiveText unless you are sure there is text waiting in the BlueTooth buffer.
- That vibrate for a full second might be trouble. Test and let us know how that worked out for you.
Hi, question about your 3rd point, is it from my sketch or I need to remove something in the blocks? Thank you.
Hi, thank you for the clarification, I will check the vibrate function later, and will get back to you soon.
Hi, the vibration now works but the app lags as the sensor readings are constantly being read by the mcu, because of this, I can't input a direction to the remote. Thank you
You expect a \n after each command, but you don't send any from AI2.
Minor confusion sorry, it does give an input but it lags as the sensors are constantly giving the readings to the mcu. Attached is the sketch and the output.
#include <Arduino.h>
#include <BluetoothSerial.h>
#include <esp_now.h>
#include <WiFi.h>
BluetoothSerial Serial_BT;
// Motor 3
int r_en3 = 15; // +
int l_en3 = 0; // -
int r_pwm3 = 2; // +
int l_pwm3 = 4; // -
// Motor 4
int r_en4 = 16; // +
int l_en4 = 18; // -
int r_pwm4 = 17; // +
int l_pwm4 = 19; // -
//Ultrasonic Sensor Pinouts
int trigPin = 13;
int echoPin = 12;
int distance;
long duration;
unsigned long currentMillis;
unsigned long previousMillis = 0;
const long interval = 900; // Interval for sensor reading in milliseconds
int incoming;
uint8_t slaveAddress[] = {0xC8, 0x2E, 0x18, 0x67, 0x36, 0x6C}; // Replace with your slave ESP32 MAC address
void onDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
Serial.print("\r\nLast Packet Send Status:\t");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
if (status != ESP_NOW_SEND_SUCCESS) {
Serial.println("Failed to send data");
}
}
void setup() {
//motor driver;
pinMode(r_en3, OUTPUT);
pinMode(l_en3, OUTPUT);
pinMode(r_pwm3, OUTPUT);
pinMode(l_pwm3, OUTPUT);
pinMode(r_en4, OUTPUT);
pinMode(l_en4, OUTPUT);
pinMode(r_pwm4, OUTPUT);
pinMode(l_pwm4, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(115200);
Serial_BT.begin("ESP32Test");
Serial.println("The device has started, now you can pair it with Bluetooth");
// Initialize WiFi and ESP-NOW
WiFi.mode(WIFI_STA);
WiFi.disconnect(); // Ensure no connections interfere with ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
esp_now_register_send_cb(onDataSent);
//Register Peer
esp_now_peer_info_t peerInfo;
memcpy(peerInfo.peer_addr, slaveAddress, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
Serial.println("Failed to add peer");
return;
} else {
Serial.println("Peer added successfully");
}
}
void loop() {
while (Serial_BT.available()) {
String Direction = Serial_BT.readStringUntil('\n');
Serial.print("Received Direction: ");
Serial.println(Direction);
esp_err_t result = esp_now_send(slaveAddress, (uint8_t *)Direction.c_str(), Direction.length());
if (result == ESP_OK) {
Serial.println("ESP-NOW message sent successfully");
} else {
Serial.print("Error sending ESP-NOW message: ");
Serial.println(result);
}
if (Direction == "F") {
// Forward
moveForward();
} else if (Direction == "B") {
// Backward
moveBackward();
} else if (Direction == "L") {
// Left
moveLeft();
} else if (Direction == "R") {
// Right
moveRight();
} else if (Direction == "FL") {
// Forward Left
moveForwardLeft();
} else if (Direction == "FR") {
// Forward Right
moveForwardRight();
} else if (Direction == "BL") {
// Backward Left
moveBackwardLeft();
} else if (Direction == "BR") {
// Backward Right
moveBackwardRight();
} else if (Direction == "S") {
// STOP
moveStop();
}
}
unsigned long currentMillis = millis();
//Ultrasonic Sensor Reading
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2; // Calculate the distance
// Debug: Print the calculated distance
Serial.print("Distance: ");
Serial.println(distance);
if (currentMillis - previousMillis >= interval) { // Ensure the interval has passed
previousMillis = currentMillis; // Update the timestamp
if (distance < 20) { // Object detected within 200 cm
Serial_BT.write('O');
Serial.println('O');
} else { // No object within 200 cm
Serial_BT.write('1');
Serial.println('1');
}
}
}
void moveForward() {
digitalWrite(r_en3, HIGH);
digitalWrite(l_en3, HIGH);
analogWrite(r_pwm3, 255);
analogWrite(l_pwm3, 0);
digitalWrite(r_en4, HIGH);
digitalWrite(l_en4, HIGH);
analogWrite(r_pwm4, 255);
analogWrite(l_pwm4, 0);
}
void moveBackward() {
digitalWrite(r_en3, HIGH);
digitalWrite(l_en3, HIGH);
analogWrite(r_pwm3, 0);
analogWrite(l_pwm3, 255);
digitalWrite(r_en4, HIGH);
digitalWrite(l_en4, HIGH);
analogWrite(r_pwm4, 0);
analogWrite(l_pwm4, 255);
}
void moveLeft() {
digitalWrite(r_en3, HIGH);
digitalWrite(l_en3, HIGH);
analogWrite(r_pwm3, 255);
analogWrite(l_pwm3, 0);
digitalWrite(r_en4, HIGH);
digitalWrite(l_en4, HIGH);
analogWrite(r_pwm4, 0);
analogWrite(l_pwm4, 255);
}
void moveRight() {
digitalWrite(r_en3, HIGH);
digitalWrite(l_en3, HIGH);
analogWrite(r_pwm3, 0);
analogWrite(l_pwm3, 255);
digitalWrite(r_en4, HIGH);
digitalWrite(l_en4, HIGH);
analogWrite(r_pwm4, 255);
analogWrite(l_pwm4, 0);
}
void moveForwardLeft() {
digitalWrite(r_en3, HIGH);
digitalWrite(l_en3, HIGH);
analogWrite(r_pwm3, 255);
analogWrite(l_pwm3, 0);
digitalWrite(r_en4, HIGH);
digitalWrite(l_en4, HIGH);
analogWrite(r_pwm4, 0);
analogWrite(l_pwm4, 0);
}
void moveForwardRight() {
digitalWrite(r_en3, HIGH);
digitalWrite(l_en3, HIGH);
analogWrite(r_pwm3, 0);
analogWrite(l_pwm3, 0);
digitalWrite(r_en4, HIGH);
digitalWrite(l_en4, HIGH);
analogWrite(r_pwm4, 255);
analogWrite(l_pwm4, 0);
}
void moveBackwardLeft() {
digitalWrite(r_en3, HIGH);
digitalWrite(l_en3, HIGH);
analogWrite(r_pwm3, 0);
analogWrite(l_pwm3, 0);
digitalWrite(r_en4, HIGH);
digitalWrite(l_en4, HIGH);
analogWrite(r_pwm4, 0);
analogWrite(l_pwm4, 255);
}
void moveBackwardRight() {
digitalWrite(r_en3, HIGH);
digitalWrite(l_en3, HIGH);
analogWrite(r_pwm3, 0);
analogWrite(l_pwm3, 255);
digitalWrite(r_en4, HIGH);
digitalWrite(l_en4, HIGH);
analogWrite(r_pwm4, 0);
analogWrite(l_pwm4, 0);
}
void moveStop() {
digitalWrite(r_en3, LOW);
digitalWrite(l_en3, LOW);
analogWrite(r_pwm3, 0);
analogWrite(l_pwm3, 0);
digitalWrite(r_en4, LOW);
digitalWrite(l_en4, LOW);
analogWrite(r_pwm4, 0);
analogWrite(l_pwm4, 0);
}
dont mind the delivery fail, I dont have the slave esp right now.
show your corrected blocks and new clock timer settings.
You missed point 4
I dont know if this is right
Like this, but receive just 1 byte:
Do I need the label1 to be hidden or not?
Keep it visible until everything works