Hackster is hosting Impact Spotlights: Industrial Automation. Watch the stream live on Thursday!Hackster is hosting Impact Spotlights: Industrial Automation. Stream on Thursday!
issaom
Published © GPL3+

YouTube analytics on ESP32

Getting online statistics from the YouTube channel

IntermediateFull instructions provided504
YouTube analytics on ESP32

Things used in this project

Hardware components

FireBeetle ESP32 IOT Microcontroller (Supports Wi-Fi & Bluetooth)
DFRobot FireBeetle ESP32 IOT Microcontroller (Supports Wi-Fi & Bluetooth)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Drill / Driver, Cordless
Drill / Driver, Cordless
Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

yotube_Whul93NOLU.JPG

Code

YoutubeAnalyticsV17.ino

Arduino
#define RedL 17
#define GreL 16
#define BluL 2

#include <WiFi.h>
#include <HTTPClient.h>
#include "time.h"
#include <SPI.h>
#include <U8g2lib.h>
#include <EEPROM.h>
U8G2_SSD1309_128X64_NONAME0_F_4W_HW_SPI u8g2(U8G2_R0, /* cs=*/ 5, /* dc=*/ 4, /* reset=*/ 15);

const char* ssid     = "";                    // ssid
const char* password = "";                    // 
const char* ntpServer = "pool.ntp.org";       //  
const long  gmtOffset_sec = 10800;            // 3600*3=10800 (UTC+3) 
const int   daylightOffset_sec = 0;           //    
String youtubeApiKey = "";

String youtubeNameChammel = "UCWnYJoytogNmXg4BHZV8XLg";

String   AllVideoList = "";                   //     
uint32_t AllVideoCount = 0;                   //    

//     
uint32_t ShowwC = 0;                          //  
uint32_t ShowlC = 0;                          //  
uint32_t ShowdC = 0;                          //  
uint32_t ShowcC = 0;                          //  

uint32_t OldShowcC = 0;                       //      

uint32_t ShowsC = 0;                          //  
uint32_t ShowvC = 0;                          //    

//  
uint32_t wC = 0;                              //  
uint32_t lC = 0;                              //          2,3
uint32_t dC = 0;                              //       4,6
uint32_t cC = 0;                              //    6,7

//        
int32_t DwC = 0;                              //        [1] - [0]
int32_t DlC = 0;                              //            [3] - [2]
int32_t DdC = 0;                              //         [5] - [4]
int32_t DcC = 0;                              //      [7] - [6]

uint32_t Last5Min [] = {0, 0, 0, 0, 0, 0 , 0, 0}; //    5 

uint32_t Last1Hour [] = {0, 0, 0, 0, 0, 0, 0, 0};
int Last1HourADDR = 0;                            //     
uint8_t *Last1HourPTR  = (uint8_t*)&Last1Hour;    //    Last1Hour

uint32_t Last1Day [] = {0, 0, 0, 0, 0, 0, 0, 0};
int Last1DayADDR = 32;                            //     
uint8_t *Last1DayPTR  = (uint8_t*)&Last1Day;      //    Last1Day

boolean wifiOK = false;
void wifiShow () {
  u8g2.setFont(u8g2_font_open_iconic_all_1x_t);
  if (wifiOK) {
    u8g2.setCursor(119, 9); u8g2.write(253);        // wifi OK
  } else {
    u8g2.setCursor(119, 9); u8g2.write(121);        // wifi no OK
  }
}

boolean refreshOK = false;
void refreshShow () {
  u8g2.setFont(u8g2_font_open_iconic_all_1x_t);
  if (refreshOK) {
    u8g2.setCursor(119, 20); u8g2.write(243);       //   
  } else {
    u8g2.setCursor(119, 20); u8g2.write(115);       //   
  }
}

TaskHandle_t Task1;
TaskHandle_t Task2;
#include "ESP32_Task1code.h"
#include "ESP32_Task2code.h"

void setup() {
  Serial.begin(115200);
  EEPROM.begin(64);
  pinMode (RedL, OUTPUT);
  pinMode (GreL, OUTPUT);
  pinMode (BluL, OUTPUT);

  u8g2.begin();
  u8g2.setFont(u8g2_font_profont12_mr);           //   ( 8 )
  u8g2.clearBuffer();                             //     
  u8g2.sendBuffer();                              //  
  u8g2.setCursor(0, 8);
  u8g2.print(ssid);
  wifiShow ();
  u8g2.setFont(u8g2_font_profont12_mr);
  u8g2.sendBuffer();
  u8g2.setCursor(0, 19);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    u8g2.print(".");
    u8g2.sendBuffer();
  }
  u8g2.clearBuffer();
  u8g2.setCursor(0, 8);
  u8g2.print(WiFi.localIP());                     //  IP - 
  wifiOK = true;
  wifiShow ();
  u8g2.sendBuffer();
  delay(5000);

  //         
  for (byte i = 0; i < 32; i++) {
    *(Last1HourPTR + i) = EEPROM.read(Last1HourADDR + i);
  }
  for (byte i = 0; i < 32; i++) {
    *(Last1DayPTR + i) = EEPROM.read(Last1DayADDR + i);
  }

  configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);  //       
  //delay(2000);
  xTaskCreatePinnedToCore(Task1code, "Task1", 10000, NULL, 1, &Task1, 0); // 0
  delay(500);
  xTaskCreatePinnedToCore(Task2code, "Task2", 10000, NULL, 1, &Task2, 1); // 1
  delay(500);
}

void loop() {

}

ESP32_Task1code.h

Arduino
//  0  ,   ,   
#include "ESP32_fourBtSens.h"

uint8_t ShowDisp = 0; //     0.5 

boolean dot = false;                                                      //   
const uint8_t Yico[]  = {0x00, 0x7E, 0xF7, 0xE7, 0xC7, 0xE7, 0xF7, 0x7E}; //  

uint8_t sectionI = 0;                                                     //  
const char *sectionS[]  = {                                               //    
  "M+",     // 0   5 
  "M-",     // 1   1 
  "H+",     // 2   1 
  "H-",     // 0   5 
  "D+",     // 1   1 
  "D-",     // 2   1 
};

boolean powerSave = false;                                        //  / 
uint8_t BtSt1and3 = 0;                                            //   2-  

//      ( )
void printYoutubeA() {

  struct tm timeinfo;                          //    
  if (!getLocalTime(&timeinfo)) {
    Serial.println("Failed to obtain time");
    return;
  }

  if (sectionI == 0) {
    //       5  (M+)
    DwC = ShowwC - Last5Min [1];              //   
    DlC = ShowlC - Last5Min [3];              //   
    DdC = ShowdC - Last5Min [5];              //   
    DcC = ShowcC - Last5Min [7];              //   
  }

  if (sectionI == 1) {
    //         5  (M-)
    DwC = Last5Min [1] - Last5Min [0];        //   
    DlC = Last5Min [3] - Last5Min [2];        //   
    DdC = Last5Min [5] - Last5Min [4];        //   
    DcC = Last5Min [7] - Last5Min [6];        //       
  }

  if (sectionI == 2) {
    //        (H+)
    DwC = ShowwC - Last1Hour [1];              //   
    DlC = ShowlC - Last1Hour [3];              //   
    DdC = ShowdC - Last1Hour [5];              //   
    DcC = ShowcC - Last1Hour [7];              //   
  }

  if (sectionI == 3) {
    //        (H-)
    DwC = Last1Hour [1] - Last1Hour [0];        //   
    DlC = Last1Hour [3] - Last1Hour [2];        //   
    DdC = Last1Hour [5] - Last1Hour [4];        //   
    DcC = Last1Hour [7] - Last1Hour [6];        //   
  }

  if (sectionI == 4) {
    //        (D+)
    DwC = ShowwC - Last1Day [1];              //   
    DlC = ShowlC - Last1Day [3];              //   
    DdC = ShowdC - Last1Day [5];              //   
    DcC = ShowcC - Last1Day [7];              //   
  }

  if (sectionI == 5) {
    //        (D-)
    DwC = Last1Day [1] - Last1Day [0];        //   
    DlC = Last1Day [3] - Last1Day [2];        //   
    DdC = Last1Day [5] - Last1Day [4];        //   
    DcC = Last1Day [7] - Last1Day [6];        //   
  }


  u8g2.clearBuffer();                            //  
  wifiShow ();                                   //   Wi Fi
  refreshShow ();                                //    
  u8g2.setFont(u8g2_font_open_iconic_human_1x_t);
  u8g2.setCursor(0,  8); u8g2.print(char(64));   // ico 
  u8g2.setCursor(0, 52); u8g2.print(char(68));   // ico 
  u8g2.setFont(u8g2_font_open_iconic_www_1x_t);
  u8g2.setCursor(0, 19); u8g2.print("I");        // ico 
  u8g2.setCursor(0, 30); u8g2.print("R");        // ico 
  u8g2.setCursor(0, 41); u8g2.print("K");        // ico 
  u8g2.drawXBMP(0, 56, 8, 8, Yico);              // ico 

  u8g2.setFont(u8g2_font_profont12_mr);
  u8g2.setCursor(12,  8); u8g2.print(ShowwC);   //  
  u8g2.print(" +"); u8g2.print(DwC);

  u8g2.setCursor(12, 19); u8g2.print(ShowlC);   //  
  u8g2.print(" +"); u8g2.print(DlC);

  u8g2.setCursor(12, 30); u8g2.print(ShowdC);   //  
  u8g2.print(" +"); u8g2.print(DdC);

  u8g2.setCursor(12, 41); u8g2.print(ShowcC);   //  
  u8g2.print(" +"); u8g2.print(DcC);

  u8g2.setCursor(12, 52); u8g2.print(ShowsC);   //  

  u8g2.setCursor(12, 64); u8g2.print(ShowvC);   //    

  u8g2.setCursor(45, 64);                       //    
  u8g2.print(&timeinfo, "%H");
  if (dot) u8g2.print(':'); else u8g2.print(' ');
  dot = !dot;
  u8g2.print(&timeinfo, "%M");
  //u8g2.print(':');
  //u8g2.print(&timeinfo, "%S");
  u8g2.print(' ');
  u8g2.print(&timeinfo, "%d");
  u8g2.print('.');
  u8g2.print(&timeinfo, "%m");
  //u8g2.print('.');
  //u8g2.print(&timeinfo, "%Y");
  u8g2.print(" ");
  u8g2.print(sectionS[sectionI]);
  u8g2.sendBuffer();                              //   
}

void Task1code( void * pvParameters ) {

  for (;;) { // 

    readBtSens ();                                //   StBt
    if (bitRead(StBt, 0) && !bitRead(StBt, 1)) {  //     1- 
      bitSet(StBt, 1);                            //     -   
      if (sectionI != 5) sectionI++;              //   
    }
    if (bitRead(StBt, 2) && !bitRead(StBt, 3)) {  //     2- 
      bitSet(StBt, 3);                            //     -   
      digitalWrite(BluL,LOW);                     //   
    }
    if (bitRead(StBt, 4) && !bitRead(StBt, 5)) {  //     3- 
      bitSet(StBt, 5);                            //     -   
      if (sectionI != 0) sectionI--;              //   
    }
    if (bitRead(StBt, 6) && !bitRead(StBt, 7)) {  //     4- 
      bitSet(StBt, 7);                            //     -   
      // 
    }

    if ((StBt == B00110011) && (BtSt1and3 == 0 )) {   //  1  3   / 
      powerSave = !powerSave;
      u8g2.setPowerSave(powerSave);
      BtSt1and3 = 100;                                 //   1 
    } else {
      if (BtSt1and3 != 0) BtSt1and3--;
    }

    delay(10);

    ShowDisp ++;                                     //    1   0.5 
    if (ShowDisp >= 50) {
      printYoutubeA();
      ShowDisp = 0;
    }
  }
}

ESP32_Task2code.h

Arduino
String httpGETRequest(const char* serverName) {
  HTTPClient http;
  // Your IP address with path or Domain name with URL path
  http.begin(serverName);
  // Send HTTP POST request
  int httpResponseCode = http.GET();
  String payload = "{}";
  if (httpResponseCode > 0) {
    //Serial.print("HTTP Response code: ");
    //Serial.println(httpResponseCode);
    payload = http.getString();
  }
  else {
    //Serial.print("Error code: ");
    //Serial.println(httpResponseCode);
  }
  // Free resources
  http.end();
  return payload;
}

#include "ESP32_youtubeA.h"               //      
//  1 
int Now1Min = 0;
int Old1Min = 0;
//  5 
int Old5Min = 0;
//  10 
int Old10Min = 0;
//  1 
int Now1Hour = 0;
int Old1Hour = 0;
//  1 
int Now1Day = 0;
int Old1Day = 0;


void Task2code( void * pvParameters ) {

  refreshOK = true;

  getYoutubeChanel();           //   
  makeVideoList();              //    ID   
  refreshA ();                  //  
  ShowwC = wC;                  //   -   
  ShowlC = lC;                  //   -   
  ShowdC = dC;                  //   -   
  ShowcC = cC;                  //   -   
  //  5   (    5+5 )
  Last5Min [1] = ShowwC;
  Last5Min [3] = ShowlC;
  Last5Min [5] = ShowdC;
  Last5Min [7] = ShowcC;
  Last5Min [0] = ShowwC;
  Last5Min [2] = ShowlC;
  Last5Min [4] = ShowdC;
  Last5Min [6] = ShowcC;


  struct tm timeinfo;                                    //    
  if (!getLocalTime(&timeinfo)) {
    Serial.println("Failed to obtain time");
    return;
  }
  //        
  Now1Min = timeinfo.tm_min;
  Old1Min = Now1Min;
  Now1Hour = timeinfo.tm_hour;
  Old1Hour = Now1Hour;
  Now1Day =  timeinfo.tm_mday;
  Old1Day = Now1Day;

  refreshOK = false;

  for (;;) { // 

    struct tm timeinfo;                                  //    
    if (!getLocalTime(&timeinfo)) {
      Serial.println("Failed to obtain time");
      return;
    }
    Now1Min = timeinfo.tm_min;
    Now1Hour = timeinfo.tm_hour;
    Now1Day =  timeinfo.tm_mday;

    if (Now1Min != Old1Min) {                            //    1  1 
      //Serial.println("Now1Min");
      Old1Min = Now1Min;
    }

    if ((Now1Min % 5 == 0) && (Now1Min != Old5Min)) {    //    1   5  (5  )
      //Serial.println("Now5Min");
      Old5Min = Now1Min;

      refreshOK = true;             //     

      OldShowcC = ShowcC;           //    

      getYoutubeChanel();           //   

      //     
      wC = 0;                       //  
      lC = 0;                       //  
      dC = 0;                       //  
      cC = 0;                       //  

      //      
      refreshA ();                  //  

      //    (-10 )
      Last5Min [0] = Last5Min [1];  //      OLD
      Last5Min [2] = Last5Min [3];  //      OLD
      Last5Min [4] = Last5Min [5];  //          OLD
      Last5Min [6] = Last5Min [7];  //    OLD

      //     ( -5 )
      Last5Min [1] = ShowwC;        //      NEW
      Last5Min [3] = ShowlC;        //      NEW
      Last5Min [5] = ShowdC;        //          NEW
      Last5Min [7] = ShowcC;        //    NEW

      //   0 
      ShowwC = wC;                  //   -   
      ShowlC = lC;                  //   -   
      ShowdC = dC;                  //   -   
      ShowcC = cC;                  //   -   

      //  /     
      if (OldShowcC != ShowcC) digitalWrite (BluL,HIGH);
      
      refreshOK = false;            //  
      
    }

    if ((Now1Min % 10 == 0) && (Now1Min != Old10Min)) {  //    1   10  ( )
      Serial.println("Now10Min");
      Old10Min = Now1Min;
    }

    if (Now1Hour != Old1Hour) {                          //    1    (  1 )
      Serial.println("Now1Hour");
      Old1Hour = Now1Hour;

      //    ( )
      Last1Hour [0] = Last1Hour [1];  //     OLD
      Last1Hour [2] = Last1Hour [3];  //     OLD
      Last1Hour [4] = Last1Hour [5];  //         OLD
      Last1Hour [6] = Last1Hour [7];  //   OLD

      //     ( )
      Last1Hour [1] = ShowwC;        //      NEW
      Last1Hour [3] = ShowlC;        //      NEW
      Last1Hour [5] = ShowdC;        //          NEW
      Last1Hour [7] = ShowcC;        //    NEW

      //        

      for (uint8_t i = 0; i < 32; i++) {
        EEPROM.write((Last1HourADDR + i), (*(Last1HourPTR + i)));
        EEPROM.commit();
      }

    }

    if (Now1Day != Old1Day) {                            //    1    (  1 )
      //       
      configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
      Serial.println("Now1Day");
      Old1Day = Now1Day;

      //    (   00:00)
      Last1Day [0] = Last1Day [1];  //      OLD
      Last1Day [2] = Last1Day [3];  //      OLD
      Last1Day [4] = Last1Day [5];  //          OLD
      Last1Day [6] = Last1Day [7];  //    OLD

      //     (   00:00)
      Last1Day [1] = ShowwC;        //      NEW
      Last1Day [3] = ShowlC;        //      NEW
      Last1Day [5] = ShowdC;        //          NEW
      Last1Day [7] = ShowcC;        //    NEW

      //        
      for (uint8_t i = 0; i < 32; i++) {
        EEPROM.write((Last1DayADDR + i), (*(Last1DayPTR + i)));
        EEPROM.commit();
      }
    }

    delay(10);
  }

}

ESP32_youtubeA.h

Arduino
//    
void makeVideoList () {

  if (WiFi.status() == WL_CONNECTED) {
    wifiOK = true;
    AllVideoList = "";  //    
    AllVideoCount = 0;  //        
    int32_t findStart = 0; int32_t findNext = 0; int32_t findEnd = 0;

    //     ""
    String serverPath = "https://www.googleapis.com/youtube/v3/channels?part=contentDetails&id=" + youtubeNameChammel + "&key=" + youtubeApiKey;
    String S = httpGETRequest(serverPath.c_str());
    String uploads = "";
    findStart = S.indexOf("\"uploads", findNext);
    findNext = findStart + 12;
    findEnd = S.indexOf("\"", findNext);
    uploads = S.substring(findNext, findEnd);
    delay(25);

    //    ID  30       30
    serverPath = "https://www.googleapis.com/youtube/v3/playlistItems?playlistId=" + uploads + "&maxResults=30&key=" + youtubeApiKey + "&part=contentDetails";
    S = httpGETRequest(serverPath.c_str());

    //       11  + 
    findStart = 0; findNext = 0; findEnd = 0;
    findStart = S.indexOf("\"videoId", findNext);
    while ( findStart != -1) {
      findNext = findStart + 12;
      findEnd = S.indexOf("\"", findNext);
      AllVideoList = AllVideoList + S.substring(findNext, findEnd);
      AllVideoList = AllVideoList + ",";
      AllVideoCount++;
      ShowvC = AllVideoCount;                       //  
      findStart = S.indexOf("\"videoId", findEnd);
    }

    //     
    String nextPageToken = "";
    findStart = 0; findNext = 0; findEnd = 0;
    findStart = S.indexOf("\"nextPageToken", findNext);
    if (findStart == -1) return; //   30  -  
    findNext = findStart + 18;
    findEnd = S.indexOf("\"", findNext);
    nextPageToken = S.substring(findNext, findEnd);

label:
    serverPath = "https://www.googleapis.com/youtube/v3/playlistItems?playlistId=" + uploads + "&maxResults=30&key=" + youtubeApiKey + "&part=contentDetails&pageToken=" + nextPageToken;
    S = httpGETRequest(serverPath.c_str());

    //       11  + 
    findStart = 0; findNext = 0; findEnd = 0;
    findStart = S.indexOf("\"videoId", findNext);
    while ( findStart != -1) {
      findNext = findStart + 12;
      findEnd = S.indexOf("\"", findNext);
      AllVideoList = AllVideoList + S.substring(findNext, findEnd);
      AllVideoList = AllVideoList + ",";
      AllVideoCount++;
      ShowvC = AllVideoCount;                       //  
      findStart = S.indexOf("\"videoId", findEnd);
    }

    //     
    nextPageToken = "";
    findStart = 0; findNext = 0; findEnd = 0;
    findStart = S.indexOf("\"nextPageToken", findNext);
    if (findStart == -1) return; // nextPageToken   -    
    findNext = findStart + 18;
    findEnd = S.indexOf("\"", findNext);
    nextPageToken = S.substring(findNext, findEnd);
    delay(25);
    goto label;

  } else {
    //  
    wifiOK = false;
  }

}


//    
void getYoutubeChanel() {
  if (WiFi.status() == WL_CONNECTED) {
    wifiOK = true;
    //    
    String serverPath = "https://www.googleapis.com/youtube/v3/channels?part=statistics&id=" + youtubeNameChammel + "&key=" + youtubeApiKey;
    //   json
    String S = httpGETRequest(serverPath.c_str());   
    //   
    String s; int32_t findStart = 0; int32_t findNext = 0; int32_t findEnd = 0;  
    findStart = S.indexOf("\"subscriberCount", findNext);
    findNext = findStart + 20;
    findEnd = S.indexOf("\"", findNext);
    s = S.substring(findNext, findEnd);
    ShowsC = s.toInt();
    delay(25);

  } else {
    //  
    wifiOK = false;
  }
}

//     1  50- 
//    : idvideo1,idvideo2,....,idvideo50
void getYoutubeVideoS(String listVideo) {

  if (WiFi.status() == WL_CONNECTED) {
    
    wifiOK = true;

    String serverPath = "https://www.googleapis.com/youtube/v3/videos?id=" + listVideo + "&key=" + youtubeApiKey + "&part=statistics";
    String S = httpGETRequest(serverPath.c_str());

    String s = ""; int32_t findStart = 0; int32_t findNext = 0; int32_t findEnd = 0;
    uint32_t viewCount = 0;                          //  
    findStart = S.indexOf("\"viewCount", findNext);
    while ( findStart != -1) {
      findNext = findStart + 14;
      findEnd = S.indexOf("\"", findNext);
      s = S.substring(findNext, findEnd);
      viewCount = viewCount + s.toInt();
      //Serial.println(s.toInt());
      findStart = S.indexOf("\"viewCount", findEnd);
    }

    s = ""; findStart = 0; findNext = 0; findEnd = 0;
    uint32_t likeCount = 0;                          //  
    findStart = S.indexOf("\"likeCount", findNext);
    while ( findStart != -1) {
      findNext = findStart + 14;
      findEnd = S.indexOf("\"", findNext);
      s = S.substring(findNext, findEnd);
      likeCount = likeCount + s.toInt();
      //Serial.print(s.toInt()); Serial.print(" - ");
      findStart = S.indexOf("\"likeCount", findNext);
    }

    s = ""; findStart = 0; findNext = 0; findEnd = 0;
    uint32_t dislikeCount = 0;                        //  
    findStart = S.indexOf("\"dislikeCount", findNext);
    while ( findStart != -1) {
      findNext = findStart + 17;
      findEnd = S.indexOf("\"", findNext);
      s = S.substring(findNext, findEnd);
      dislikeCount = dislikeCount + s.toInt();
      //Serial.print(s.toInt()); Serial.print(" - ");
      findStart = S.indexOf("\"dislikeCount", findNext);
    }

    s = ""; findStart = 0; findNext = 0; findEnd = 0;
    uint32_t commentCount = 0;                        //  
    findStart = S.indexOf("\"commentCount", findNext);
    while ( findStart != -1) {
      findNext = findStart + 17;
      findEnd = S.indexOf("\"", findNext);
      s = S.substring(findNext, findEnd);
      commentCount = commentCount + s.toInt();
      //Serial.print(s.toInt()); Serial.println(" - ");
      findStart = S.indexOf("\"commentCount", findNext);
    }

    wC = wC + viewCount;                     //  
    lC = lC + likeCount;                     //  
    dC = dC + dislikeCount;                  //  
    cC = cC + commentCount;                  //  

  } else {
    //   -   
    wifiOK = false;
  }
}

void refreshA () {

  uint32_t remainVideo = AllVideoCount;
  String S = AllVideoList;
  String s = "";

label:

  if (remainVideo <= 30) {                   //  
    S.remove(S.lastIndexOf(','));            //   
    getYoutubeVideoS(S);    
    return;
  }

  do {                                       //    30 
    s = S.substring(0, 360);
    s.remove(s.lastIndexOf(','));
    S.remove(0, 360);
    getYoutubeVideoS(s);
    remainVideo = remainVideo - 30;
    delay(25);
  } while (remainVideo > 30);

  goto label;
}

ESP32_fourBtSens.h

Arduino
#define bt1 14      // 1 GPIO 14
#define bt2 27      // 2 GPIO 27
#define bt3 12      // 3 GPIO 12
#define bt4 13      // 4 GPIO 13

uint8_t BtF1 = 1;   //      1
uint8_t BtF2 = 1;   //      2
uint8_t BtF3 = 1;   //      3
uint8_t BtF4 = 1;   //      4
uint8_t StBt = 0;   //     4    /  24=8
const uint8_t SensThreshold = 50; //  
const uint8_t filtrCount = 3;     //        (max7 bit)
                                  //     -   (  7)

//   4    /  24=8
void readBtSens () {

  //    1-  
  if ((touchRead(bt1)) < SensThreshold) {
    if (bitRead(BtF1, filtrCount)) bitSet(StBt, 0); else BtF1 <<= 1;
  }
  else {                          //    
    if (bitRead(BtF1, 0)) {
      bitClear(StBt, 0); bitClear(StBt, 1);
    } else BtF1 >>= 1;
  }

  //    2-  
  if ((touchRead(bt2)) < SensThreshold) {
    if (bitRead(BtF2, filtrCount)) bitSet(StBt, 2); else BtF2 <<= 1;
  }
  else {                          //    
    if (bitRead(BtF2, 0)) {
      bitClear(StBt, 2); bitClear(StBt, 3);
    } else BtF2 >>= 1;
  }

  //    3-  
  if ((touchRead(bt3)) < SensThreshold) {
    if (bitRead(BtF3, filtrCount)) bitSet(StBt, 4); else BtF3 <<= 1;
  }
  else {                          //    
    if (bitRead(BtF3, 0)) {
      bitClear(StBt, 4); bitClear(StBt, 5);
    } else BtF3 >>= 1;
  }

  //    4-  
  if ((touchRead(bt4)) < SensThreshold) {
    if (bitRead(BtF4, filtrCount)) bitSet(StBt, 6); else BtF4 <<= 1;
  }
  else {                          //    
    if (bitRead(BtF4, 0)) {
      bitClear(StBt, 6); bitClear(StBt, 7);
    } else BtF4 >>= 1;
  }

}

Credits

issaom
6 projects • 16 followers
Contact

Comments

Please log in or sign up to comment.