├── README.md ├── examples └── ESP32analogReadNonBlocking │ ├── ESP32analogReadNonBlocking.ino │ ├── ESP32analogReadNonBlocking.sln │ ├── ESP32analogReadNonBlocking.v12.suo │ ├── ESP32analogReadNonBlocking.vcxproj │ └── ESP32analogReadNonBlocking.vcxproj.filters ├── keywords.txt ├── library.json ├── library.properties ├── license.txt └── src ├── ESP32analogReadNonBlocking.cpp └── ESP32analogReadNonBlocking.h /README.md: -------------------------------------------------------------------------------- 1 | # ESP32analogReadNonBlocking 2 | 3 | Non-blocking analogRead Function multiple sample averaging -------------------------------------------------------------------------------- /examples/ESP32analogReadNonBlocking/ESP32analogReadNonBlocking.ino: -------------------------------------------------------------------------------- 1 | /* 2 | ESP32analogReadNonBlocking 3 | Terry Myers 4 | github.com/terryjmyers/ESP32analogReadNonBlocking 5 | 6 | Parameters 7 | uint132_t NumOfSamples = Number of samples to average together for better smoothing. Note that this should be limited to <= 1,048,831 (2^32 / 4095) samples due to the uint32_t size 8 | unit8_t &ArbitrationToken = A global variable that each instance will use as a round robin token arbitrate the ADC 9 | 10 | I.E. XXX.analogRead(100, ADC1ArbitrationToken). This will take 100 samples ( minimum of ~1ms @ 10us per sample) 11 | 12 | Theory of operation: 13 | Pass in the Number of Samples to take and a globally scoped Arbitration Token. 14 | If the adc isn't busy and the arbitration token is 0 (ADC is free), this library will change the token to the pin to "take ownership" of ADC1, initiate a conversion, and immediatly return to allow your code to process 15 | Oneach subsequent call of analogRead, it will check to see the adc conversion is done. If so, it will initate another one if NumOfSamples > 1. 16 | If the NumOfSamples has been reached (even if you juset set to 1), it will update counts and release arbitration. 17 | This will allow the next instance of this function to take ownership and use the ADC. 18 | Ownership and use of the ADC will thusly happen in a "round robin" fashion in the order of code execution. 19 | 20 | */ 21 | 22 | #include 23 | 24 | //instantiate as many as you need 25 | //These two are on ADC1 and will round robin with each other 26 | ESP32analogReadNonBlocking pin36(36, 100000); 27 | ESP32analogReadNonBlocking pin39(39, 100000); 28 | //These two are on ADC2 and will round robin with each other 29 | ESP32analogReadNonBlocking pin04(4, 100000); 30 | ESP32analogReadNonBlocking pin25(25, 100000); 31 | 32 | //Create one Aribtration token per ADC used in your project 33 | uint8_t ArbitrationToken1; //Use one Aribtration Token for anything on ADC1, GPIO: 34, 35, 36, 37, 38, 39 34 | uint8_t ArbitrationToken2; //Use one Arbitration Token for anything on ADC2, GPIO: 4, 12, 13, 14, 15, 25, 26, 27 35 | 36 | uint32_t loopcounter;//loop counter for example to show that the code is non-blocking 37 | uint32_t loopcounterSerialPrintTimer; 38 | 39 | void setup() { 40 | Serial.begin(115200); 41 | } 42 | 43 | void loop() 44 | { 45 | //Call the tick function every loop. It handles the arbitration of the ADC, reading of the ADC, and sample averaging 46 | pin36.tick(ArbitrationToken1); 47 | pin36.tick(ArbitrationToken1); 48 | pin04.tick(ArbitrationToken2); 49 | pin25.tick(ArbitrationToken2); 50 | 51 | /* 52 | That's all you need 53 | "pinXX.counts" will contain the ADC reading you can use in your code 54 | 55 | 100,000 samples @ ~10us per sample gives a ~1sample per second rate which is useful for this example to not flood the serial output 56 | Each time the newValueFlag is true, print the raw averaged value to serial. 57 | You'll notice in the example below that GPIO36 and GPIO39 will take turns using ADC1, and GPIO04 and GPIO25 will take turns using ADC2 58 | Change the number of samples on the fly: pin36.NumOfSamples=10; 59 | */ 60 | if (pin36.newValueFlag) { Serial.print("GPIO36 raw counts = "); Serial.print(pin36.counts); } 61 | if (pin39.newValueFlag) { Serial.print("GPIO39 raw counts = "); Serial.print(pin39.counts); } 62 | if (pin04.newValueFlag) { Serial.print("GPIO04 raw counts = "); Serial.print(pin04.counts); } 63 | if (pin25.newValueFlag) { Serial.print("GPIO25 raw counts = "); Serial.print(pin25.counts); } 64 | 65 | //Once a second print how many loops the code has executed 66 | loopcounter++; 67 | if (millis() - loopcounterSerialPrintTimer > 1000) { 68 | Serial.print("Loops: "); 69 | Serial.println(loopcounter); 70 | loopcounterSerialPrintTimer = millis(); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /examples/ESP32analogReadNonBlocking/ESP32analogReadNonBlocking.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2013 4 | VisualStudioVersion = 12.0.21005.1 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ESP32analogReadNonBlocking", "ESP32analogReadNonBlocking.vcxproj", "{C5F80730-F44F-4478-BDAE-6634EFC2CA88}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Win32 = Debug|Win32 11 | Release|Win32 = Release|Win32 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {C5F80730-F44F-4478-BDAE-6634EFC2CA88}.Debug|Win32.ActiveCfg = Debug|Win32 15 | {C5F80730-F44F-4478-BDAE-6634EFC2CA88}.Debug|Win32.Build.0 = Debug|Win32 16 | {C5F80730-F44F-4478-BDAE-6634EFC2CA88}.Release|Win32.ActiveCfg = Release|Win32 17 | {C5F80730-F44F-4478-BDAE-6634EFC2CA88}.Release|Win32.Build.0 = Release|Win32 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | EndGlobal 23 | -------------------------------------------------------------------------------- /examples/ESP32analogReadNonBlocking/ESP32analogReadNonBlocking.v12.suo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terryjmyers/ESP32analogReadNonBlocking/bf27efd8775c98e22d6062ea2bb90b853e31cba1/examples/ESP32analogReadNonBlocking/ESP32analogReadNonBlocking.v12.suo -------------------------------------------------------------------------------- /examples/ESP32analogReadNonBlocking/ESP32analogReadNonBlocking.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | {C5F80730-F44F-4478-BDAE-6634EFC2CA88} 15 | 16 | 17 | ESP32analogReadNonBlocking 18 | 19 | 20 | 21 | Application 22 | true 23 | 24 | 25 | MultiByte 26 | 27 | 28 | Application 29 | false 30 | 31 | 32 | true 33 | MultiByte 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | Level3 49 | Disabled 50 | true 51 | $(ProjectDir)..\ESP32analogReadNonBlocking;$(ProjectDir)..\..\src;C:\Program Files (x86)\Arduino\libraries;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\libraries;$(ProjectDir)..\..\..\..\libraries;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\cores\esp32;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\cores\esp32\libb64;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\variants\lolin32;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\config;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\bluedroid;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\app_trace;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\app_update;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\bootloader_support;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\bt;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\driver;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\esp32;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\esp_adc_cal;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\ethernet;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\fatfs;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\freertos;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\heap;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\jsmn;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\log;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\mdns;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\mbedtls;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\mbedtls_port;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\newlib;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\nvs_flash;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\openssl;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\soc;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\spi_flash;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\sdmmc;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\spiffs;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\tcpip_adapter;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\ulp;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\vfs;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\wear_levelling;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\xtensa-debug-module;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\console;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\coap;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\wpa_supplicant;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\expat;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\json;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\nghttp;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\lwip;C:\Users\tmyers\AppData\Local\arduino15\packages\esp8266\tools\xtensa-lx106-elf-gcc\1.20.0-26-gb404fb9-2\xtensa-lx106-elf\include\c++\4.8.2;C:\Users\tmyers\AppData\Local\arduino15\packages\esp8266\tools\xtensa-lx106-elf-gcc\1.20.0-26-gb404fb9-2\xtensa-lx106-elf\include\c++\4.8.2\xtensa-lx106-elf;C:\Users\tmyers\AppData\Local\arduino15\packages\esp8266\tools\xtensa-lx106-elf-gcc\1.20.0-26-gb404fb9-2\xtensa-lx106-elf\include;C:\Users\tmyers\AppData\Local\arduino15\packages\esp8266\tools\xtensa-lx106-elf-gcc\1.20.0-26-gb404fb9-2\lib\gcc\xtensa-lx106-elf\4.8.2\include;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include;%(AdditionalIncludeDirectories) 52 | $(ProjectDir)__vm\.ESP32analogReadNonBlocking.vsarduino.h;%(ForcedIncludeFiles) 53 | false 54 | __ESP32_ESp32__;__ESP32_ESP32__;_VMDEBUG=1;ESP_PLATFORM;HAVE_CONFIG_H;F_CPU=240000000L;ARDUINO=10804;ARDUINO_LOLIN32;ARDUINO_ARCH_ESP32;ESP32;CORE_DEBUG_LEVEL=0;__cplusplus=201103L;_VMICRO_INTELLISENSE;%(PreprocessorDefinitions) 55 | 56 | 57 | true 58 | 59 | 60 | 61 | 62 | Level3 63 | Disabled 64 | true 65 | true 66 | true 67 | $(ProjectDir)..\ESP32analogReadNonBlocking;$(ProjectDir)..\..\src;C:\Program Files (x86)\Arduino\libraries;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\libraries;$(ProjectDir)..\..\..\..\libraries;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\cores\esp32;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\cores\esp32\libb64;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\variants\lolin32;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\config;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\bluedroid;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\app_trace;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\app_update;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\bootloader_support;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\bt;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\driver;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\esp32;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\esp_adc_cal;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\ethernet;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\fatfs;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\freertos;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\heap;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\jsmn;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\log;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\mdns;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\mbedtls;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\mbedtls_port;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\newlib;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\nvs_flash;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\openssl;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\soc;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\spi_flash;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\sdmmc;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\spiffs;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\tcpip_adapter;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\ulp;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\vfs;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\wear_levelling;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\xtensa-debug-module;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\console;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\coap;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\wpa_supplicant;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\expat;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\json;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\nghttp;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include\lwip;C:\Users\tmyers\AppData\Local\arduino15\packages\esp8266\tools\xtensa-lx106-elf-gcc\1.20.0-26-gb404fb9-2\xtensa-lx106-elf\include\c++\4.8.2;C:\Users\tmyers\AppData\Local\arduino15\packages\esp8266\tools\xtensa-lx106-elf-gcc\1.20.0-26-gb404fb9-2\xtensa-lx106-elf\include\c++\4.8.2\xtensa-lx106-elf;C:\Users\tmyers\AppData\Local\arduino15\packages\esp8266\tools\xtensa-lx106-elf-gcc\1.20.0-26-gb404fb9-2\xtensa-lx106-elf\include;C:\Users\tmyers\AppData\Local\arduino15\packages\esp8266\tools\xtensa-lx106-elf-gcc\1.20.0-26-gb404fb9-2\lib\gcc\xtensa-lx106-elf\4.8.2\include;C:\Program Files (x86)\Arduino\hardware\espressif\esp32\tools\sdk\include;%(AdditionalIncludeDirectories) 68 | $(ProjectDir)__vm\.ESP32analogReadNonBlocking.vsarduino.h;%(ForcedIncludeFiles) 69 | false 70 | __ESP32_ESp32__;__ESP32_ESP32__;ESP_PLATFORM;HAVE_CONFIG_H;F_CPU=240000000L;ARDUINO=10804;ARDUINO_LOLIN32;ARDUINO_ARCH_ESP32;ESP32;CORE_DEBUG_LEVEL=0;__cplusplus=201103L;_VMICRO_INTELLISENSE;%(PreprocessorDefinitions) 71 | 72 | 73 | true 74 | true 75 | true 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | CppCode 84 | 85 | 86 | 87 | 88 | 89 | 90 | VisualMicroDebugger 91 | 92 | 93 | 94 | 95 | -------------------------------------------------------------------------------- /examples/ESP32analogReadNonBlocking/ESP32analogReadNonBlocking.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | Header Files 23 | 24 | 25 | -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | ####################################### 2 | # Syntax Coloring Map 3 | ####################################### 4 | 5 | ####################################### 6 | # Datatypes (KEYWORD1) 7 | ####################################### 8 | ESP32analogReadNonBlocking KEYWORD1 9 | 10 | ####################################### 11 | # Methods and Functions (KEYWORD2) 12 | ####################################### 13 | newValueFlag KEYWORD2 14 | tick KEYWORD2 -------------------------------------------------------------------------------- /library.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ESP32analogReadNonBlocking by Terry Myers", 3 | "keywords": "ESP32 analogRead ", 4 | "description": "Non-blocking analogRead Function multiple sample averaging", 5 | "repository": 6 | { 7 | "type": "git", 8 | "url": "https://github.com/terryjmyers/ESP32analogReadNonBlocking" 9 | }, 10 | "frameworks": "arduino", 11 | "platforms": "esp32" 12 | } 13 | 14 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=ESP32analogReadNonBlocking 2 | version=1.0.0 3 | author=Terry Myers 4 | maintainer=Terry Myers 5 | sentence=Non-blocking analogRead Function multiple sample averaging 6 | paragraph=Non-blocking analogRead Function multiple sample averaging 7 | category=Signal Input/Output 8 | url=https://github.com/terryjmyers/ESP32analogReadNonBlocking.git 9 | architectures=esp32 10 | -------------------------------------------------------------------------------- /license.txt: -------------------------------------------------------------------------------- 1 | beerware -------------------------------------------------------------------------------- /src/ESP32analogReadNonBlocking.cpp: -------------------------------------------------------------------------------- 1 | #include "ESP32analogReadNonBlocking.h" 2 | 3 | void ESP32analogReadNonBlocking::tick(uint8_t &ArbitrationToken) { 4 | //&unit8_t &ArbitrationToken = A global variable that this routine will assign to handle arbitration between itself and other instances of this routine 5 | newValueFlag = false; 6 | if (adcBusy(_pin) || (ArbitrationToken != _pin && ArbitrationToken > 0)) return; //no point in doing anything if the adc is already doing something or someone else has ownership 7 | 8 | if (ArbitrationToken == 0) { //adc isn't busy, nor is it arbitrated, now's your chance! 9 | adcAttachPin(_pin); 10 | adcStart(_pin); 11 | ArbitrationToken = _pin; //take ownership of the token by setting it to your pin number 12 | _adcSamplesTotal = 0; //reset number of samples and total for averaging 13 | _adcCurrentSamples = 0; 14 | return; //no reason to continue, check for completion on the next loop 15 | } 16 | else if (ArbitrationToken == _pin) { //you have ownership and the adc is done. This really could have been just an else statement, but for completeness... 17 | 18 | _adcSamplesTotal += adcEnd(_pin); //get the adc results and add them to a total for averaging 19 | _adcCurrentSamples++; //increment the number of samples taken for averaging 20 | 21 | if (_adcCurrentSamples >= NumOfSamples) { //you have taken the requested number of samples, take the average and release arbitration 22 | counts = _adcSamplesTotal / _adcCurrentSamples; //take the average 23 | ArbitrationToken = 0; //release arbitration 24 | newValueFlag = true; 25 | } 26 | else { //you have more samples to take, start the adc again 27 | adcStart(_pin); 28 | } 29 | } 30 | 31 | } -------------------------------------------------------------------------------- /src/ESP32analogReadNonBlocking.h: -------------------------------------------------------------------------------- 1 | /* 2 | ESP32analogReadNonBlocking 3 | v1.1 4 | Terry Myers 5 | github.com/terryjmyers/ESP32analogReadNonBlocking 6 | 7 | Theory of operation: 8 | A globally scoped Arbitration Token is passed in. 9 | If the adc isn't busy and the arbitration token is 0 (ADC is free), this library will take ownership of the ADC by changing the valud of the token to its pin number, 10 | initiate a conversion, and immediatly return to allow your code to process 11 | On each subsequent call of tick, it will check to see the adc conversion is done. If so, it will initate another one if NumOfSamples > 1. 12 | If the NumOfSamples has been reached (even if you juset set to 1), it will update ".counts" and release arbitration. 13 | This will allow the next instance of this function to take ownership and use the ADC. 14 | Ownership and use of the ADC will thusly happen in a "round robin" fashion in the order of code execution. 15 | 16 | Notes: 17 | 1. The number of samples to take should be limited to <= 1,048,831 (2^32 / 4095) samples due to the uint32_t size of NumOfSamples 18 | 2. Change NumOfSamples on the fly after class instantiation by simply changing NumOfSamples. 19 | 3. Each ADC conversion only takes ~10us. Use this to figure out how best to optimize the number of samples you need vs how many GPIO are trying to use a single ADC 20 | */ 21 | 22 | #ifndef ESP32 23 | #error "ESP32analogReadNonBlocking.h library only valid for ESP32" 24 | #endif // !ESP32 25 | #include 26 | #ifndef ESP32analogReadNonBlocking_H 27 | #define ESP32analogReadNonBlocking_H 28 | 29 | class ESP32analogReadNonBlocking 30 | { 31 | public: 32 | ESP32analogReadNonBlocking(uint8_t pin, uint32_t NumOfSamplesToAverage) { // uint8_t pin = Valid GPIO pin using ADC1(GPIO34,35,36,37,38,and 39) or ADC2(GPIO4,12,13,14,15,25,26,and 27). NumOfSamples to average. *NOTE GPIO25, 26, and 27 cannot be used on ESP32-WROVER module 33 | NumOfSamples = NumOfSamplesToAverage; 34 | _pin = pin; 35 | //An initial useless adc read is performed because when the ESP32 boots up its ADC its "busy". 36 | //Doing this at least once, "releases" the ADC for the first instance of this library to take ownership and start working 37 | adcAttachPin(_pin); 38 | adcStart(_pin); 39 | }; 40 | 41 | void tick(uint8_t &ArbitrationToken); //call this every loop 42 | uint32_t NumOfSamples; //Change the number of samples on the fly after the constructor if you need to 43 | uint16_t counts; //access the raw counts directly at any time 44 | bool newValueFlag; //Set true for one loop when ".counts" is updated with a new averaged number. Can be used to update scaling and trigger other code only when nessesary 45 | 46 | private: 47 | 48 | uint8_t _pin; 49 | //Used for averaging 50 | uint32_t _adcSamplesTotal; 51 | uint32_t _adcCurrentSamples; 52 | }; 53 | #endif --------------------------------------------------------------------------------