//This example code is in the Public Domain (or CC0 licensed, at your option.)
//By Evandro Copercini - 2018
//
//This example creates a bridge between Serial and Classical Bluetooth (SPP)
//and also demonstrate that SerialBT have the same functionalities of a normal Serial
// source: https://randomnerdtutorials.com/esp32-bluetooth-classic-arduino-ide/
#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;
String a;
const int LEFT_BLINKER_LED_PIN = 12;
const int TAIL_LIGHTS_PIN = 13;
const int HEAD_LIGHT_PIN = 27;
const int RIGHT_BLINKER_LED_PIN = 14;
const int interval = 600; //750;
const int TAIL_LIGHT_BRIGHTNESS_LIGHT = 155;
const int TAIL_LIGHT_BRIGHTNESS_BRAKE = 255;
unsigned long previousMillis = 0;
int ledState = LOW;
bool blinker_left_state = false;
bool blinker_right_state = false;
bool light_state = false;
void setup()
{
Serial.begin(115200);
Serial.println("Hello, ESP32-S2!");
pinMode(TAIL_LIGHTS_PIN, OUTPUT);
pinMode(HEAD_LIGHT_PIN, OUTPUT);
pinMode(LEFT_BLINKER_LED_PIN, OUTPUT);
pinMode(RIGHT_BLINKER_LED_PIN, OUTPUT);
}
void loop() {
a = what_to_do();
// a = Serial.readStringUntil('\n');// read the incoming data as string
if (a.equalsIgnoreCase("h"))
{
if (light_state)
{
}
else{
analogWrite(TAIL_LIGHTS_PIN, TAIL_LIGHT_BRIGHTNESS_LIGHT);
digitalWrite(HEAD_LIGHT_PIN, HIGH);
}
}
else if (a == "off" || a == "OFF")
{
Serial.println("LED OFF");
digitalWrite(TAIL_LIGHTS_PIN, LOW);
analogWrite(HEAD_LIGHT_PIN, TAIL_LIGHT_BRIGHTNESS_BRAKE);
}
// Trigger left blinker
else if (a.equalsIgnoreCase("l"))
{
blinker_left_state = !blinker_left_state;
Serial.print("blinker_left_state is: ");
Serial.println(blinker_left_state);
blinker_right_state = false;
digitalWrite(RIGHT_BLINKER_LED_PIN, 0);
if (blinker_left_state == false)
{
digitalWrite(LEFT_BLINKER_LED_PIN, 0);
}
}
// Trigger right blinker
else if (a.equalsIgnoreCase("r"))
{
blinker_right_state = !blinker_right_state;
Serial.print("blinker_right_state is: ");
Serial.println(blinker_right_state);
blinker_left_state = false;
digitalWrite(LEFT_BLINKER_LED_PIN, 0);
if (blinker_right_state == false)
{
digitalWrite(RIGHT_BLINKER_LED_PIN, 0);
}
}
// trigger blinkers on/off
if (blinker_left_state)
{
blinker(LEFT_BLINKER_LED_PIN);
}
if (blinker_right_state)
{
blinker(RIGHT_BLINKER_LED_PIN);
}
}
String what_to_do()
{
if (Serial.available() > 0)
{
String inChar = Serial.readStringUntil('\n');
if (inChar != NULL)
{
Serial.print(" > Received: ");
Serial.println(inChar);
return inChar;
}
}
}
void blinker(int blinker_led)
{
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval)
{
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
{
ledState = HIGH;
}
else
{
ledState = LOW;
}
}
// set the LED with the ledState of the variable:
digitalWrite(blinker_led, ledState);
}