Sorry, I forgot to reply here. You’re not going to be able to do this with a Pi using Cayenne. You shouldn’t do this using python because of the possibility of Python waiting for the OS to give it time to run. You might be able to use C but I don’t have any experience there.
Your best bet is to use Arduino since there is no OS to wait on. It looks like the code you posed is actually for an Arduino, so if you want to go down that path I can try to help you there.