Hello. I hope someone can help.
I have an ESP32 connected to an IR sensor. It is coded to connect via bluetooth, which it does using the Serial Bluetooth Terminal app. It is also coded to count items that go by the IR sensor on a conveyor belt, which it does. The data (item count) shows up on the Arduino IDE program on the laptop when the ESP32 is connected via USB. And I can communicate text between the Arduino IDE and the Serial Bluetooth Terminal using the ESP32's bluetooth connection, so I know the 2 way communication via bluetooth is working. But I cannot get the IR sensor count to show up on the Serial Bluetooth Terminal.
Any help would be appreciated. TIA
Here is the code:
#include "BluetoothSerial.h"
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
BluetoothSerial SerialBT;
// Pin configuration
const int sensorPin = 2; // Connect the infrared sensor's output pin to Arduino pin 2
// Variables
int objectCount = 0; // Counter to keep track of the number of objects
int sensorState = 0; // Current state of the sensor (HIGH or LOW)
int lastSensorState = 0; // Previous state of the sensor
void setup() {
Serial.begin(115200);
SerialBT.begin("ESP32test"); //Bluetooth device name
Serial.println("The device started, now you can pair it with bluetooth!");
pinMode(sensorPin, INPUT); // Set sensor pin as input
Serial.begin(9600); // Initialize serial communication for debugging
}
void loop() {
sensorState = digitalRead(sensorPin); // Read the state of the sensor
// Check if the sensor state has changed (object detected)
if (sensorState != lastSensorState) {
if (sensorState == HIGH) {
objectCount++; // Increment the object count when an object is detected
Serial.print("Object detected! Count: ");
Serial.println(objectCount);
}
// Delay to debounce the sensor (avoid multiple counts for a single object)
delay(50);
}
lastSensorState = sensorState; // Update the last sensor state for the next iteration
if (Serial.available()) {
SerialBT.write(Serial.read());
}
if (SerialBT.available()) {
Serial.write(SerialBT.read());
}
delay(20);
}
When I edited the post, it made a new post. I'll try again here.
#include "BluetoothSerial.h"
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
BluetoothSerial SerialBT;
// Pin configuration
const int sensorPin = 2; // Connect the infrared sensor's output pin to Arduino pin 2
// Variables
int objectCount = 0; // Counter to keep track of the number of objects
int sensorState = 0; // Current state of the sensor (HIGH or LOW)
int lastSensorState = 0; // Previous state of the sensor
void setup() {
Serial.begin(115200);
SerialBT.begin("ESP32test"); //Bluetooth device name
Serial.println("The device started, now you can pair it with bluetooth!");
pinMode(sensorPin, INPUT); // Set sensor pin as input
Serial.begin(9600); // Initialize serial communication for debugging
}
void loop() {
sensorState = digitalRead(sensorPin); // Read the state of the sensor
// Check if the sensor state has changed (object detected)
if (sensorState != lastSensorState) {
if (sensorState == HIGH) {
objectCount++; // Increment the object count when an object is detected
Serial.print("Object detected! Count: ");
Serial.println(objectCount);
}
// Delay to debounce the sensor (avoid multiple counts for a single object)
delay(50);
}
lastSensorState = sensorState; // Update the last sensor state for the next iteration
if (Serial.available()) {
SerialBT.write(Serial.read());
}
if (SerialBT.available()) {
Serial.write(SerialBT.read());
}
delay(20);
}
I'm guessing that I don't? I do not know how to code. I have to do this for a final project in an online class, but they never covered how to write code, so I'm a little lost.
I used 2 existing codes from the library and merged them through a bunch of trial and error. If something is missing or in the wrong order, I don't know what, or where it should go. Any guidance would be greatly appreciated. Thank you.
This is a hint. Just like Serial has print() and println() methods, SerialBT also has them. So where you print()/println() the count to Serial, you can also add print()/println() statements to send the count to SerialBT.