Arduino Uno, multiple processes, multi-threading -


i´m facing problem application in arduino uno.

the board has sensor counting products every second, after time sends number of products server process takes more second, code registering products not called until process completed piece of product not counted.

i´ve been looking if arduino supports multi-threading in order have thread sending data server , other 1 registering number of products, have no clear answer far.

what best solution face problem?

const long max_iteration = 100000; const int off = 1; const int on = 0; const int photoelectric_sensor = 3;  int counter = 0; long iteration = 0; int state = off;  void loop() {     registerproduct();     if (iteration >= max_iteration) {          // process takes more second          senddatatoserver();          iteration = 0;     }     iteration++; }  void registerproduct() {     int currentsensorstate = digitalread(photoelectric_sensor);     if (currentsensorstate != state) {         if (currentsensorstate == on) {             counter++;         }     }     state = currentsensorstate; }  void senddatatoserver() {    // sends data through http protocol, , sets counter 0 } 

it looks might want redesign this. while might want use threading on desktop platform don't have hardware support concurrency in manner on arduino uno. have interrupts can use current problem.

put sensor on pin change interrupt. increment counter in isr pin change interrupt. arduino uno has few general purpose pin change interrupts, @ datasheet more info.

then create timer using 1 of internal timers. attach interrupt, when timer interrupt fires take counter amount , put aside. need send value on server. try make send not block. may need service send in main loop on multiple loop cycles.

by using interrupts, timing ones, free lot of processor cycles. try keep amount of code in each of isrs minimal possible don't lose data interrupts being missed.


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -