Guest User

Sensor based "Traffic light" for the Pi

a guest
Mar 17th, 2017
117
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * This program is used to implement a simple "traffic light" based on
  3.  * distance sensors.
  4.  */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <pthread.h>
  9. #include <time.h>
  10. #include <sys/time.h>
  11. #include <sys/types.h>
  12. #include <wiringPi.h>
  13.  
  14. /**
  15.  * setup structure for the thread
  16.  */
  17. typedef struct _distSetup {
  18.   // id of the sensor
  19.   short id;
  20.   // the trigger pin for the distance sensor
  21.   short gpio_trigger;
  22.   // the echo pin for the distance sensor
  23.   short gpio_echo;
  24.   // the pin for the red (stop) led
  25.   short gpio_led_red;
  26.   // the pin for the green (go) led
  27.   short gpio_led_green;
  28.   // actual distance
  29.   unsigned int distance;
  30. } distSetup;
  31.  
  32. // get the actual time in microseconds
  33. // currently we dont need the seconds)
  34. typedef unsigned long long u64;
  35. u64 usec(){
  36.   struct timeval tv;
  37.   gettimeofday(&tv, NULL);
  38.   return tv.tv_usec;
  39. }
  40.  
  41. /**
  42.  * here starts the measuring for one sensor
  43.  */
  44. void*
  45. start_measure(void *arg)
  46. {
  47.   distSetup *ds = (distSetup*)arg;
  48.   u64 startTime, stopTime;
  49.   // init the pins
  50.   pinMode(ds->gpio_trigger, OUTPUT);
  51.   pinMode(ds->gpio_echo, INPUT);
  52.   pinMode(ds->gpio_led_red, OUTPUT);
  53.   pinMode(ds->gpio_led_green, OUTPUT);
  54.   digitalWrite(ds->gpio_trigger, LOW);
  55.   digitalWrite(ds->gpio_led_red, LOW);
  56.   digitalWrite(ds->gpio_led_green, LOW);
  57.   while(1){
  58.     // trigger the measurement
  59.     digitalWrite(ds->gpio_trigger, HIGH);
  60.     delay(1);
  61.     digitalWrite(ds->gpio_trigger, LOW);
  62.     wait for echo
  63.     while(digitalRead(ds->gpio_echo) == 0);
  64.     startTime = usec();
  65.     measure the time
  66.     while(digitalRead(ds->gpio_echo) == 1);
  67.     stopTime = usec();
  68.     stopTime += stopTime > startTime ? 0 : 100000;
  69.     ds->distance = ((stopTime - startTime) / 2)  / 34.3;
  70.     if(ds->distance < 20) {
  71.       digitalWrite(ds->gpio_led_red, HIGH);
  72.       digitalWrite(ds->gpio_led_green, LOW);
  73.     }else{
  74.       digitalWrite(ds->gpio_led_red, LOW);
  75.       digitalWrite(ds->gpio_led_green, HIGH);
  76.     }
  77.     delay(1000);
  78.   }
  79.   return NULL;
  80. }
  81.  
  82. #define SENSOR_COUNT 3
  83.  
  84. int
  85. main(int argc __attribute__((unused)), char *argv[] __attribute__((unused)))
  86. {
  87.   pthread_t pid[SENSOR_COUNT];
  88.   distSetup myDistSetup[SENSOR_COUNT];
  89.   /** setup for the first sensor */
  90.   myDistSetup[0].id = 1;
  91.   myDistSetup[0].gpio_trigger = 8;     // GPIO 00 - PIN 3
  92.   myDistSetup[0].gpio_echo = 9;        // GPIO 01 - PIN 5
  93.   myDistSetup[0].gpio_led_red = 7;     // GPIO 04 - PIN 7
  94.   myDistSetup[0].gpio_led_green = 15;  // GPIO 14 - PIN 8
  95.   /** setup for the second sensor */
  96.   myDistSetup[1].id = 2;
  97.   myDistSetup[1].gpio_trigger = 0;     // GPIO 17 - PIN 11
  98.   myDistSetup[1].gpio_echo = 2;        // GPIO 21 - PIN 13
  99.   myDistSetup[1].gpio_led_red = 3;     // GPIO 22 - PIN 15
  100.   myDistSetup[1].gpio_led_green = 4;   // GPIO 23 - PIN 16
  101.   /** setup for the third sensor */
  102.   myDistSetup[2].id = 3;
  103.   myDistSetup[2].gpio_trigger = 12;    // GPIO 10 - PIN 19
  104.   myDistSetup[2].gpio_echo = 13;       // GPIO 09 - PIN 21
  105.   myDistSetup[2].gpio_led_red = 14;    // GPIO 11 - PIN 23
  106.   myDistSetup[2].gpio_led_green = 10;  // GPIO 08 - PIN 24
  107.  
  108.   wiringPiSetup();
  109.  
  110.   if(daemon(0, 0) != 0){
  111.     fprintf(stderr, "Cant daemonize\n");
  112.     return 1;
  113.   }
  114.   {
  115.     FILE *fd = fopen("/var/run/tsl.pid", "w");
  116.     fprintf(fd, "%ld", getpid());
  117.     fclose(fd);
  118.   }
  119.   int i;
  120.   for(i = 0; i < SENSOR_COUNT; i++){
  121.     pthread_create(&(pid[i]), NULL, start_measure, &(myDistSetup[i]));
  122.   }
  123.   while(1){
  124.     FILE *stats = fopen("/var/www/html/ampel.txt","w");
  125.     fprintf(stats, "%d,%d,%d", myDistSetup[0].distance > 20 ? 0 : 1, myDistSetup[1].distance > 20 ? 0 : 1, myDistSetup[2].distance > 20 ? 0 : 1);
  126.     fclose(stats);
  127.     sleep(1);
  128.   }
  129.   for(i = 0; i < SENSOR_COUNT; i++){
  130.     void *ret;
  131.     pthread_join(pid[i], &ret);
  132.   }
  133.   return 0;
  134. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×