I think I figured it out I was reading the bits the wrong way. For example, when I send 2345 I originally assumed that s.0=2, s.1=3, s.2=4 and s.3=5 but it was in fact the other way. My code now works 90% of the time, occasionally my program will jump to if statements that it is not supposed to go to and write data there.
Bookmarks