//* Idea of bidirectional packet communication *

int main () { initialize_digital_io(); setDigitalIO(..neutral..values..); initialize_uarts(...); // if needed initialize_adc(); // if needed initialize_pwm(); // if needed pwm_set(..neutral_values..); // if needed initialize_timer(...); error_count = 0; initialize_watchdog(...); global_enable_interrupts(); while(1) { while ( wait_for_ready_from_ISR() ) /* do nothing */ ; reset_ready_from_ISR(); reset_watchdog(); // capture OR/AND get captured data var0 = get_data_captured_in_interrupts(0); var1 = get_data_captured_in_interrupts(1); ... varN = get_data_captured_in_interrupts(N); // perform computations as needed, avoid FLOATING POINT, try to stay INTEGER // send processed data putc(0); // send the header value or sequence of values putc(var0); putc(var1); ... putc(varN); check = var0^var1^...^varN; // or use any other [more] decent checksum function putc(check); if ( anything_receive3d() ) { // we will assume that all will be received soon // if that cannot be assumed then use interrupt-based receive instead while ( 0!= getc() ) ; // skip any data that were a reminder from the previous packet rec0 = getc(); rec1 = getc(); ... recM = getc(); check = getc(); if ( 0 == rec0^rec1^...^recM^check ) { error_count = 0; // perform computations as needed, avoid FLOATING POINT, try to stay INTEGER ..computed..values..compute..here.. setDigitalIO(..computed..values..); setPWM(..computed..values..); } else { // skip erroneous data if (error_count<max_error) { ++error_count; } else { setDigitalIO(..neutral..values..); setPWM(..neutral..values..); error_count = 0; } } } } return(0); }