Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * This program is used to implement a simple "traffic light" based on
- * distance sensors.
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <pthread.h>
- #include <time.h>
- #include <sys/time.h>
- #include <sys/types.h>
- #include <wiringPi.h>
- /**
- * setup structure for the thread
- */
- typedef struct _distSetup {
- // id of the sensor
- short id;
- // the trigger pin for the distance sensor
- short gpio_trigger;
- // the echo pin for the distance sensor
- short gpio_echo;
- // the pin for the red (stop) led
- short gpio_led_red;
- // the pin for the green (go) led
- short gpio_led_green;
- // actual distance
- unsigned int distance;
- } distSetup;
- // get the actual time in microseconds
- // currently we dont need the seconds)
- typedef unsigned long long u64;
- u64 usec(){
- struct timeval tv;
- gettimeofday(&tv, NULL);
- return tv.tv_usec;
- }
- /**
- * here starts the measuring for one sensor
- */
- void*
- start_measure(void *arg)
- {
- distSetup *ds = (distSetup*)arg;
- u64 startTime, stopTime;
- // init the pins
- pinMode(ds->gpio_trigger, OUTPUT);
- pinMode(ds->gpio_echo, INPUT);
- pinMode(ds->gpio_led_red, OUTPUT);
- pinMode(ds->gpio_led_green, OUTPUT);
- digitalWrite(ds->gpio_trigger, LOW);
- digitalWrite(ds->gpio_led_red, LOW);
- digitalWrite(ds->gpio_led_green, LOW);
- while(1){
- // trigger the measurement
- digitalWrite(ds->gpio_trigger, HIGH);
- delay(1);
- digitalWrite(ds->gpio_trigger, LOW);
- wait for echo
- while(digitalRead(ds->gpio_echo) == 0);
- startTime = usec();
- measure the time
- while(digitalRead(ds->gpio_echo) == 1);
- stopTime = usec();
- stopTime += stopTime > startTime ? 0 : 100000;
- ds->distance = ((stopTime - startTime) / 2) / 34.3;
- if(ds->distance < 20) {
- digitalWrite(ds->gpio_led_red, HIGH);
- digitalWrite(ds->gpio_led_green, LOW);
- }else{
- digitalWrite(ds->gpio_led_red, LOW);
- digitalWrite(ds->gpio_led_green, HIGH);
- }
- delay(1000);
- }
- return NULL;
- }
- #define SENSOR_COUNT 3
- int
- main(int argc __attribute__((unused)), char *argv[] __attribute__((unused)))
- {
- pthread_t pid[SENSOR_COUNT];
- distSetup myDistSetup[SENSOR_COUNT];
- /** setup for the first sensor */
- myDistSetup[0].id = 1;
- myDistSetup[0].gpio_trigger = 8; // GPIO 00 - PIN 3
- myDistSetup[0].gpio_echo = 9; // GPIO 01 - PIN 5
- myDistSetup[0].gpio_led_red = 7; // GPIO 04 - PIN 7
- myDistSetup[0].gpio_led_green = 15; // GPIO 14 - PIN 8
- /** setup for the second sensor */
- myDistSetup[1].id = 2;
- myDistSetup[1].gpio_trigger = 0; // GPIO 17 - PIN 11
- myDistSetup[1].gpio_echo = 2; // GPIO 21 - PIN 13
- myDistSetup[1].gpio_led_red = 3; // GPIO 22 - PIN 15
- myDistSetup[1].gpio_led_green = 4; // GPIO 23 - PIN 16
- /** setup for the third sensor */
- myDistSetup[2].id = 3;
- myDistSetup[2].gpio_trigger = 12; // GPIO 10 - PIN 19
- myDistSetup[2].gpio_echo = 13; // GPIO 09 - PIN 21
- myDistSetup[2].gpio_led_red = 14; // GPIO 11 - PIN 23
- myDistSetup[2].gpio_led_green = 10; // GPIO 08 - PIN 24
- wiringPiSetup();
- if(daemon(0, 0) != 0){
- fprintf(stderr, "Cant daemonize\n");
- return 1;
- }
- {
- FILE *fd = fopen("/var/run/tsl.pid", "w");
- fprintf(fd, "%ld", getpid());
- fclose(fd);
- }
- int i;
- for(i = 0; i < SENSOR_COUNT; i++){
- pthread_create(&(pid[i]), NULL, start_measure, &(myDistSetup[i]));
- }
- while(1){
- FILE *stats = fopen("/var/www/html/ampel.txt","w");
- fprintf(stats, "%d,%d,%d", myDistSetup[0].distance > 20 ? 0 : 1, myDistSetup[1].distance > 20 ? 0 : 1, myDistSetup[2].distance > 20 ? 0 : 1);
- fclose(stats);
- sleep(1);
- }
- for(i = 0; i < SENSOR_COUNT; i++){
- void *ret;
- pthread_join(pid[i], &ret);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement