I am new to this and I have purchased the Lilygo T-Wristband to prototype a watch that will vibrate upon receiving a 1-bit bluetooth signal from an android app. The goal is to allow the user to set the intensity of vibration (hence i am using the Real Time Playback, RTP, mode) that varies from 0-255. My device seems to not be working whenever i have the Wire1 code and I really am stuck at this point trying to make the motor work. I have attached the code below
#include <pcf8563.h>
#include <TFT_eSPI.h> // Graphics and font library for ST7735 driver chip
#include <SPI.h>
#include <Time.h>
#include <Wire.h>
#include <WiFi.h>
#include "sensor.h"
#include "esp_adc_cal.h"
#include "avas.h.c"
#include "charge.h"
#include "BluetoothSerial.h"
#include <Adafruit_DRV2605.h>
// init Class:
BluetoothSerial ESP_BT;
Adafruit_DRV2605 drv;
#define TP_PIN_PIN 33
#define I2C_SDA_PIN 21
#define I2C_SCL_PIN 22
#define RTC_INT_PIN 34
#define BATT_ADC_PIN 35
#define VBUS_PIN 36
#define TP_PWR_PIN 25
#define LED_PIN 4
#define CHARGE_PIN 32
#define DRV_SDA 15
#define DRV_SCL 13
TFT_eSPI tft = TFT_eSPI(); // Invoke library, pins defined in User_Setup.h
PCF8563_Class rtc;
char buff[256];
bool rtcIrq = false;
bool initial = 1;
bool otaStart = false;
RTC_DATA_ATTR int boots = 0;
uint8_t func_select = 0;
uint8_t omm = 99;
uint8_t xcolon = 0;
uint32_t targetTime = 0; // for next 1 second timeout
uint32_t colour = 0;
int vref = 1100;
bool pressed = false;
uint32_t pressedTime = 0;
bool charge_indication = false;
uint8_t hh, mm, ss ;
void setupWire() {
tft.init();
tft.setRotation(1);
tft.setSwapBytes(true);
tft.pushImage(0, 0, 160, 80, avas);
delay(2000);
Wire.begin(I2C_SDA_PIN, I2C_SCL_PIN);
Wire.setClock(400000);
}
void setupPins() {
pinMode(TP_PIN_PIN, INPUT);
pinMode(TP_PWR_PIN, PULLUP);
//! Must be set to pull-up output mode in order to wake up in deep sleep mode
digitalWrite(TP_PWR_PIN, HIGH);
pinMode(LED_PIN, OUTPUT);
pinMode(CHARGE_PIN, INPUT_PULLUP);
attachInterrupt(CHARGE_PIN, [] {
charge_indication = true;
}, CHANGE);
if (digitalRead(CHARGE_PIN) == LOW) {
charge_indication = true;
}
}
void setupTFT() { //set-up fonts
tft.init();
tft.setRotation(1);
tft.setSwapBytes(true);
tft.fillScreen(TFT_WHITE);
tft.setTextColor(TFT_YELLOW, TFT_WHITE); // Note: the new fonts do not draw the background color
}
void setupVolt() { //set-up battery monitoring
esp_adc_cal_characteristics_t adc_chars;
esp_adc_cal_value_t val_type = esp_adc_cal_characterize((adc_unit_t)ADC_UNIT_1, (adc_atten_t)ADC1_CHANNEL_6, (adc_bits_width_t)ADC_WIDTH_BIT_12, 1100, &adc_chars);
//Check type of calibration value used to characterize ADC
if (val_type == ESP_ADC_CAL_VAL_EFUSE_VREF) {
// Serial.printf("eFuse Vref:%u mV", adc_chars.vref);
vref = adc_chars.vref;
} else if (val_type == ESP_ADC_CAL_VAL_EFUSE_TP) {
// Serial.printf("Two Point --> coeff_a:%umV coeff_b:%umV\n", adc_chars.coeff_a, adc_chars.coeff_b);
} else {
// Serial.println("Default Vref: 1100mV");
}
}
void setupRTC() { //set-up RTC for time
rtc.begin(Wire);
//Check if the RTC clock matches, if not, use compile time
rtc.check();
RTC_Date datetime = rtc.getDateTime();
hh = datetime.hour;
mm = datetime.minute;
ss = datetime.second;
}
void setupDRV(){
Wire1.begin(DRV_SDA, DRV_SCL);
Wire1.beginTransmission(DRV2605_ADDR);
if (Wire1.endTransmission() != 0) {
tft.setTextColor(TFT_RED, TFT_WHITE);
tft.fillScreen(TFT_WHITE);
tft.setCursor(0, 0);
tft.println("Failed to connect DRV2605");
delay(5000);
esp_restart();
}
drv.begin(&Wire1);
drv.setMode(DRV2605_MODE_REALTIME);
}
void setup(void) {
// WiFi.mode(WIFI_OFF);
Serial.begin(9600);
setCpuFrequencyMhz(80); // scaling it back saves so much of the precious battery life
btStop();
ESP_BT.begin("AVAS");
setupDRV();
setupWire();
setupTFT();
setupRTC();
setupVolt();
while (ESP_BT.available() == false)
{
tft.setTextColor(TFT_BLUE, TFT_WHITE);
tft.fillScreen(TFT_WHITE);
tft.setCursor(0, 0);
tft.println("Bluetooth not connected");
delay(3000);
}
while (ESP_BT.available() == true){
tft.setTextColor(TFT_RED, TFT_WHITE);
tft.fillScreen(TFT_WHITE);
tft.setCursor(0, 0);
tft.println("Bluetooth connected");
delay(5000);
break;
}
if(boots == 0) //Run this only the first time
{
targetTime = millis() + 1000 ; //adds 1 min to compiler time
boots ++;
}
setupPins();
}
String Volt() {
uint16_t v = analogRead(BATT_ADC_PIN);
float battery_voltage = ((float)v / 4095.0) * 2.0 * 3.3 * (vref / 1000.0);
return String(battery_voltage) + "V";
}
void RTC() {
if (targetTime < millis()) {
RTC_Date datetime = rtc.getDateTime();
hh = 11;
mm = datetime.minute;
ss = datetime.second;
// Serial.printf("hh:%d mm:%d ss:%d\n", hh, mm, ss);
targetTime = millis() + 1000;
if (ss == 0 || initial) {
initial = 0;
tft.setTextColor(TFT_GREEN, TFT_WHITE);
tft.setCursor (8, 60);
tft.print(__DATE__); // This uses the standard ADAFruit small font
}
tft.setTextColor(TFT_BLUE, TFT_WHITE);
tft.drawCentreString(Volt(), 120, 60, 1); // Next size up font 2
// Update digital time
uint8_t xpos = 6;
uint8_t ypos = 0;
if (omm != mm) { // Only redraw every minute to minimise flicker
// Uncomment ONE of the next 2 lines, using the ghost image demonstrates text overlay as time is drawn over it
tft.setTextColor(0x39C4, TFT_WHITE); // Leave a 7 segment ghost image, comment out next line!
//tft.setTextColor(TFT_BLACK, TFT_BLACK); // Set font colour to black to wipe image
// Font 7 is to show a pseudo 7 segment display.
// Font 7 only contains characters [space] 0 1 2 3 4 5 6 7 8 9 0 : .
tft.drawString("88:88", xpos, ypos, 7); // Overwrite the text to clear it
tft.setTextColor(0xFBE0, TFT_WHITE); // Orange
omm = mm;
if (hh < 10) xpos += tft.drawChar('0', xpos, ypos, 7);
xpos += tft.drawNumber(hh, xpos, ypos, 7);
xcolon = xpos;
xpos += tft.drawChar(':', xpos, ypos, 7);
if (mm < 10) xpos += tft.drawChar('0', xpos, ypos, 7);
tft.drawNumber(mm, xpos, ypos, 7);
}
if (ss % 2) { // Flash the colon
tft.setTextColor(0x39C4, TFT_WHITE);
xpos += tft.drawChar(':', xcolon, ypos, 7);
tft.setTextColor(0xFBE0, TFT_WHITE);
} else {
tft.drawChar(':', xcolon, ypos, 7);
}
}
}
void loop() {
if (charge_indication) {
charge_indication = false;
if (digitalRead(CHARGE_PIN) == LOW) {
tft.pushImage(140, 55, 16, 16, charge);
} else {
tft.fillRect(140, 55, 16, 16, TFT_WHITE);
}
}
if (digitalRead(TP_PIN_PIN) == HIGH) {
if (!pressed) {
initial = 1;
targetTime = millis() + 1000;
tft.fillScreen(TFT_WHITE);
omm = 99;
func_select = func_select + 1 > 2 ? 0 : func_select + 1;
pressed = true;
pressedTime = millis();
} else {
if (millis() - pressedTime > 3000) {
esp_restart();
}
}
} else {
pressed = false;
}
RTC();
if (ESP_BT.available())
{
byte pwm = ESP_BT.read(); //Read what we receive
// drv.setRealtimeValue(pwm);
// drv.go();
tft.setTextColor(TFT_RED, TFT_WHITE);
tft.fillScreen(TFT_WHITE);
tft.setCursor(0, 0);
tft.println(pwm);
}
}