Created
November 18, 2014 11:46
-
-
Save YuuichiAkagawa/ac15be90895a6481a471 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <unistd.h> | |
#include <pthread.h> | |
#define LED1 13 | |
#define LED2 12 | |
#define LED3 11 | |
pthread_t th[3]; | |
pthread_mutex_t mutex; // Mutex | |
void mutex_digitalWrite(uint8_t pin, uint8_t val) | |
{ | |
pthread_mutex_lock( &mutex ); | |
digitalWrite(pin, val); | |
pthread_mutex_unlock( &mutex ); | |
} | |
void *thread1(void *thdata) | |
{ | |
while(1){ | |
mutex_digitalWrite(LED1, HIGH); | |
usleep( 500000 ); | |
mutex_digitalWrite(LED1, LOW); | |
usleep( 500000 ); | |
} | |
} | |
void *thread2(void *thdata) | |
{ | |
while(1){ | |
mutex_digitalWrite(LED2, HIGH); | |
usleep( 250000 ); | |
mutex_digitalWrite(LED2, LOW); | |
usleep( 250000 ); | |
} | |
} | |
void *thread3(void *thdata) | |
{ | |
while(1){ | |
mutex_digitalWrite(LED3, HIGH); | |
usleep( 125000 ); | |
mutex_digitalWrite(LED3, LOW); | |
usleep( 125000 ); | |
} | |
} | |
void setup() { | |
pinMode(LED1, OUTPUT); | |
pinMode(LED2, OUTPUT); | |
pinMode(LED3, OUTPUT); | |
pthread_mutex_init( &mutex, NULL ); | |
pthread_create( &th[0], NULL, thread1, (void *)NULL ); | |
pthread_create( &th[1], NULL, thread2, (void *)NULL ); | |
pthread_create( &th[2], NULL, thread3, (void *)NULL ); | |
} | |
void loop() { | |
delay(5000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment