Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using analogRead(A0); in loop causes failure #5

Closed
bbkiwi opened this issue Mar 3, 2017 · 4 comments
Closed

using analogRead(A0); in loop causes failure #5

bbkiwi opened this issue Mar 3, 2017 · 4 comments

Comments

@bbkiwi
Copy link

bbkiwi commented Mar 3, 2017

Hi Germán,
FSBrowserNG is working for me on a nodemcu v1.0 using the libraries you have specified and the most recent arduino IDE. However when I added some code in the loop it failed. The error seems related to using analogRead(A0) in my code, as it vanishes if I don't use it and generate random data. I can recreate the problem simply with just this code at the start of the main loop:
int n = analogRead(A0);

I found that if I slow down the analogRead to once every 2ms, the problem goes away. However I would
like to sample faster than 500 Hz.

WifiLEDerror.txt
Thanks

Regards, Bill

@gmag11
Copy link
Owner

gmag11 commented Mar 3, 2017

Please, send a link to your code. Are you using delay() to control sampling?

@bbkiwi
Copy link
Author

bbkiwi commented Mar 3, 2017

No. I discovered delay is not compatible with the async library.
This is the simplest example that fails
FSBrowserNGmod.txt

Here I slow down sampling and it works if interval is 2ms or more. I also moved ESPHTTPServer.handle();
to setup
FSBrowserNGTestAddingCode.txt

@gmag11
Copy link
Owner

gmag11 commented Mar 8, 2017

Yes, it works as you've noticed.

Think that ESP8266 is a limited device. Its IP stack runs on software and there is one unique core. So, if you ask for intensive work IP will be unstable.

I've tested that you can do sampling as fast as 250Hz, but anyway sampling is unstable; if processor attends HTTP requests then it cannot do sampling. You can increase sampling to around 350 Hz if processor clock is set to 160 MHz.

For real-time operations, you can look for a different platform as, for instance, ESP32. Moreover, you will have to play with timer interrupts to be able to get stable sampling frequency.

You can check Color Chrod by CNLohr where high-frequency sampling (around 600Hz) is done but that project does not use Arduino environment and has needed a great optimization using assembler code.

More details here: http://spectrum.ieee.org/geek-life/hands-on/how-to-turn-music-into-colors-with-a-wifi-bridge.

Another option is to use an external I2S ADC.

@bbkiwi
Copy link
Author

bbkiwi commented Mar 8, 2017 via email

@gmag11 gmag11 closed this as completed Mar 8, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants