C defaulting issue -


the title vague, can't think of how describe issue concisely. here code:

#include <digiusb.h>  // global #define num_leds 2 int defaultreadings[] = {2,2};  void read_usb() {     int usbreadings[num_leds], i, numchars = 0;     (i=0;digiusb.available();i++)     {         usbreadings[i] = digiusb.read()-'0';         numchars++;     }      (i=0;i<numchars;i++)     {         if (usbreadings[i] == 0)             // usbreadings local, defaultreadings global             usbreadings[i] = defaultreadings[i];         digiusb.write(usbreadings[i]+'0');     }      memcpy(defaultreadings,usbreadings,sizeof(defaultreadings)); } 

so defaultreadings variable initialized {2,2}. when available(), new characters read local array usbreadings.

next when usbreadings iterated through, if usbreadings value zero, function should revert it's old entry in position. size of usbreadings same size of defaultreadings.

however, not working in practice. code runs fine , generates no errors, not doing think should.

here example of reads , writes through 4 function calls read_usb().

  actual            intended  reads:  3,2       reads:  3,2 writes: 3,2       writes: 3,2 reads:  3,0       reads:  3,0 writes: 3,3       writes: 3,2 reads:  0,4       reads:  0,4 writes: 2,4       writes: 3,4 reads:  0,3       reads:  0,3 writes: 2,3       writes: 3,3 

as can see in last read/write row, work correctly sometimes. there can see in code give these weird values? i've been messing hours , haven't come reason strange behavior.

edit:

my solution

for trying remotely close, may helpful. unfortunately pretty case specific, think general solution can abstracted interested in answer.

ultimately, decided put "thinking" on computer side. if didn't gather comments or post, code goes on digispark arduino-"mockoff" plugged computer via usb port. reads , computer. instead of having digispark parse zeros , remember last, having computer so.

biggest reason in support of digispark's awful/non-existant debugging capabilities.

if read 1 value in first loop, second value of usbreadings remain undefined nevertheless copied defaultreadings


Comments