0

So I have a project where I need to run pthreads (coded in C) on an Arduino Uno. I've written and compiled the code and the output is what we need when I use a C compiler. But I'm having trouble running it on the board. I've installed the Pthreads library in the libraries folder of the Arduino, but it still gives me erroneous errors.

I'm jumbling back and forth without finding a solution, can anyone give me any tips? It's become so annoying that I'm thinking of buying a Pi.

Any help is much appreciated.

Edit: let me rephrase: the professor wants us to implement pthreads on an Arduino or Pi. I didn't decide pthreads is the solution, it is the requirement.

6
  • 1
    Try something more minimalistic but with the necessary functionality. Please see, for instance, github.com/mikaelpatel/Arduino-Scheduler Oct 17 '16 at 17:05
  • As Mike points out, only limited threading is supportable. If you really want pthreads you need something more along the lines of a raspberry pi or similar device built around a tablet, set-top-box or router chip running a traditional operating system, or at minimum a very large MCU with a lot of resources like the largest versions of an ARM Cortex M4 or M7. Essentially you either need to adjust your goals, or look outside the realm of Arduino. Oct 17 '16 at 17:39
  • I might be helpful to show us these errors you get.
    – Gerben
    Oct 17 '16 at 18:12
  • That comment of yours about buying a pi which was inappropriately removed by someone else was the most insightful thing in your post. While the pi has a number of implementation-level issues, it is in concept a far more appropriate choice for something that needs true multi-threading. Oct 17 '16 at 18:24
  • pthreads might be a requirement, but as you edit clarifies, Arduino is not. Of the two options allowed, pick the pi. You'll fight with SD cards and many other things, but threads will work about the same way as on an ordinary Linux. Though the way you have stated the assignment is unclear - are you supposed to create and use some pthreads, or are you supposed to implement support for them from scratch? Oct 18 '16 at 2:00
2

I need to run pthreads ...

You probably don't need to run pthreads. This sounds like an X-Y problem.

What you are probably saying is "I need to do multiple things at once" (such as flashing LEDs whilst taking ADC readings, or play sounds at the same time as moving a vehicle forwards).

You could get better answers if you described the project (X) and not your proposed solution (Y) which you think is pthreads.

I have a web page about How to do multiple things at once ... like cook bacon and eggs which goes into this sort of thing in some detail.

Some example code from that page which blinks different LEDs at different rates is:

// Which pins are connected to which LED
const byte greenLED = 12;
const byte redLED = 13;

// Time periods of blinks in milliseconds (1000 to a second).
const unsigned long greenLEDinterval = 500;
const unsigned long redLEDinterval = 1000;

// Variable holding the timer value so far. One for each "Timer"
unsigned long greenLEDtimer;
unsigned long redLEDtimer;

void setup () 
  {
  pinMode (greenLED, OUTPUT);
  pinMode (redLED, OUTPUT);
  greenLEDtimer = millis ();
  redLEDtimer = millis ();
  }  // end of setup

void toggleGreenLED ()
  {
   if (digitalRead (greenLED) == LOW)
      digitalWrite (greenLED, HIGH);
   else
      digitalWrite (greenLED, LOW);

  // remember when we toggled it
  greenLEDtimer = millis ();  
  }  // end of toggleGreenLED

void toggleRedLED ()
  {
   if (digitalRead (redLED) == LOW)
      digitalWrite (redLED, HIGH);
   else
      digitalWrite (redLED, LOW);

  // remember when we toggled it
  redLEDtimer = millis ();  
  }  // end of toggleRedLED

void loop ()
  {

  // Handling the blink of one LED.
  if ( (millis () - greenLEDtimer) >= greenLEDinterval)
     toggleGreenLED ();

  // The other LED is controlled the same way. Repeat for more LEDs
  if ( (millis () - redLEDtimer) >= redLEDinterval) 
    toggleRedLED ();

/* Other code that needs to execute goes here.
   It will be called many thousand times per second because the above code
   does not wait for the LED blink interval to finish. */

}  // end of loop

It's become so annoying that I'm thinking of buying a Pi.

Sure you can do that. You can buy a Mac or a Windows PC. You can buy 5 Arduinos. But if you want to learn how to do multiple things on an Arduino you can learn how to manage a "state machine" and do it all on $5 of hardware.


As an example of what can be done see this post about a retro arcade game called Toorum's Quest written by Petri Häkkinen and run on an Atmega328P (same chip as in the Arduino Uno).

Hardware and screenshots:

Toorum's Quest

In his code (which I compiled using the Arduino IDE to test it) he has:

  • Multiple sprites on a TV screen with a resolution of 104x80 with 256 colors.
  • Sprites are multiplexed so you can have as many as you want on the screen.
  • He moves them around according to game logic
  • He plays music and sound effects at the same time
  • He reads from a game controller
2
  • Sad part the project specifies use of POSIX on either an Arduino or Pi, and for budget constraints, i ended up with an Uno. Oct 18 '16 at 1:54
  • You still haven't said what you are trying to do. Did the project specify that you had to use pthreads?
    – Nick Gammon
    Oct 18 '16 at 20:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.