tag:blogger.com,1999:blog-2695824644249624667.post2102891045613649294..comments2023-06-23T16:34:10.338+02:00Comments on build hardware and write code for it: protothread and arduino, a first easy examplejanhttp://www.blogger.com/profile/10266203182393633198noreply@blogger.comBlogger7125tag:blogger.com,1999:blog-2695824644249624667.post-86553758858381556912018-08-09T08:24:15.946+02:002018-08-09T08:24:15.946+02:00Exactly! Frankly I don't see protothreading ho...Exactly! Frankly I don't see protothreading how is similar to multi-threading?! I hope someone explains to me please.<br />Youssef Diranihttps://www.blogger.com/profile/15647750536733944152noreply@blogger.comtag:blogger.com,1999:blog-2695824644249624667.post-3198749225844842782018-02-25T23:16:25.028+01:002018-02-25T23:16:25.028+01:00alternatively with global array and reference - wo...alternatively with global array and reference - works, too:<br /><br />static unsigned long timestamp[2] = { 0 } ;<br />....<br />static int protothread(struct pt *pt, int interval, unsigned long *tstp ) {<br />....<br /> PT_WAIT_UNTIL(pt, millis() - *tstp > interval );<br /> *tstp = millis(); // take a new timestamp<br />....<br />void loop() {<br /> protothread(&pt1, 900, &timestamp[0] ); // schedule the two protothreads<br /> protothread(&pt2, 1000, &timestamp[1] ); // by calling them infinitely<br />}<br />Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2695824644249624667.post-67504265600770957882018-02-25T23:03:19.046+01:002018-02-25T23:03:19.046+01:00ah, the array approach seems to work:
static int ...ah, the array approach seems to work:<br /><br />static int protothread1(struct pt *pt, int interval, int cpy ) {<br /> static unsigned long timestamp[2] = { 0 } ;<br /> PT_BEGIN(pt);<br /> while(1) { // never stop <br /> <br /> PT_WAIT_UNTIL(pt, (int)(millis() - timestamp[cpy]) > interval );<br /> timestamp[cpy] = millis(); // take a new timestamp<br /> toggleLED();<br /> }<br /> PT_END(pt);<br />}<br /><br />....<br /><br />void loop() {<br /> protothread1(&pt1, 900, 0); // schedule the two protothreads<br /> protothread1(&pt2, 1000, 1); // by calling them infinitely<br />}Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2695824644249624667.post-74794251931765181472018-02-25T22:39:38.863+01:002018-02-25T22:39:38.863+01:00Thanks!
Nice and simple example for a complex issu...Thanks!<br />Nice and simple example for a complex issue :-)<br />Can we avoid the doubling of code?<br />I have similiar ideas where I want to control multiple identical hardware items (eg servos) concurrently.<br />I see the requirement for keeping the persistent variables apart.<br />Proposals:<br />- instantiate them in the caller and hand a pointer<br />- declare them as arrays and hand over an index<br />Will this work?<br />Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2695824644249624667.post-58003194549426092982016-09-17T20:47:26.982+02:002016-09-17T20:47:26.982+02:00no you cant do this...
no you cant do this...<br />Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2695824644249624667.post-75498554927296339632016-03-29T00:32:46.975+02:002016-03-29T00:32:46.975+02:00Doing the following seems to be both clearer and e...Doing the following seems to be both clearer and easier to understand: <br /><br />#define LEDPIN 13<br /><br />void setup() {<br /> pinMode(LEDPIN, OUTPUT); // LED init<br />}<br /><br />void toggleLED() {<br /> boolean ledstate = digitalRead(LEDPIN); // get LED state<br /> ledstate ^= 1; // toggle LED state using xor<br /> digitalWrite(LEDPIN, ledstate); // write inversed state back<br />}<br /><br />void thread1(int interval) {<br /> static long next_time = 0; // remember the next_time for next action (note static)<br /> if(millis()<next_time) return; // if time has not arrived, return<br /> next_time += interval; // else update next_time by period<br /> toggleLED(); // and do the action<br />}<br /><br />void thread2(int interval) {<br /> static long next_time = 0;<br /> if(millis()<next_time) return;<br /> next_time += interval;<br /> toggleLED();<br />}<br /><br />void loop() {<br /> thread1(900);<br /> thread2(1000);<br />}David P. Harrishttps://www.blogger.com/profile/17219165222374900004noreply@blogger.comtag:blogger.com,1999:blog-2695824644249624667.post-85743763102642741242014-02-08T22:23:51.291+01:002014-02-08T22:23:51.291+01:00Hi,
first, very thanks for this tutorial,
I am lo...Hi,<br /><br />first, very thanks for this tutorial,<br />I am looking for a code which I can blink diference leds and start which one to blink by a pushbutton. I have two leds and two pushbuttons, When I push the first button, I want to blinking the first led and when a push again the first button I want to turnoff the blinking of first led. I want to control the two leds in the same way. Do you know how to do this?<br /><br />ThanksAlex Medeiroshttps://www.blogger.com/profile/12927568797615759897noreply@blogger.com