Example Sketches¶
To make using this library easier I prepared this simple sketches for
Microcontroller. You will find them in ArduinoIDE under
File->Examples->DigiProtocol as soon as you add library zip to IDE or
manually add project to Arduino libraries folder.
To test this sketches you can use Monitor tool on PC to which DigiSpark is connected.
Echo¶
This program send back every received message. You can see how to read message to defined buffer. It also has static-error-message saved on FLASH. Error message will be send when:
- incoming data is corrupted
- incoming frame has bigger size than defined buffer
- there is incoming data that doesn’t fit to protocol-frame
It’s easy to overlook how the request loop works. It’s called by 3 methods:
available()that also returns information about size of waiting datadelay()that makes your device sleep for specified time in ms, but in contrast to default Arduino delay it keeps USB connection in background.getRequest()/refresh()which are clear request-loop call.
It’s very important to call one of these methods as often as possible to make sure that all request will be executed almost immediately.
Send data¶
To test this sketch you should connect button to buttonPin (by default PIN0). DigiSpark will send information every time you push or release button.
Receive data without buffer¶
To be exact there always is buffer, but as long as my library wraps DigiUSB I can’t modify it’s behaviour. So in this sketch I mean reading without public buffer.
To test this sketch you will need 2 LEDs with their anodes connected to Pins 0 and 1.
This sketch is for situation where you always know how big messages you will handle. And you don’t have to keep it after proceeding. Then you can read message in real time.
In this scenario messages are always 2 bytes long. First byte is the letter (G for Green or R for Red) and State (0 for turn of led or 1 for turn on). After proceed state of LED we don’t need to remember this message, so we can use 2 local variables.