Linux #4 2019 – Simuleer een kruispunt listing
- September 9, 2019
- 0
#include <stdio.h>
#include <pigpio.h>
#include <stdlib.h>
void start_traffic_light() {
printf(“Button for traffic is pushed.\n”);
time_sleep(0.5);
printf(“Letting traffic pass.\n”);
gpioWrite(15,0);
gpioWrite(21,1);
time_sleep(3.0);
gpioWrite(21,0);
gpioWrite(20,1);
time_sleep(1.5);
gpioWrite(20,0);
gpioWrite(21,0);
gpioWrite(15,1);
}
void shutdown_traffic_light() {
printf(“Shutting down traffic light.\n”);
gpioWrite(15,0);
gpioTerminate();
time_sleep(0.5);
exit(0);
}
int main() {
int o,q;
if (gpioInitialise() < 0) {
printf(“Gpio initialisation failed\n”);
return 1;
}
if (gpioRead(15) == 0)
gpioWrite(15,1);
gpioSetMode(12, PI_INPUT);
gpioSetPullUpDown(12, PI_PUD_UP);
gpioSetMode(14, PI_INPUT);
gpioSetPullUpDown(14, PI_PUD_UP);
gpioSetMode(15, PI_OUTPUT);
gpioSetMode(20, PI_OUTPUT);
gpioSetMode(21, PI_OUTPUT);
printf(“\tTraffic light is operational.\n\n”);
while(1) {
o = gpioRead(14);
q = gpioRead(12);
if (o == 0)
start_traffic_light();
if (q == 0)
shutdown_traffic_light();
}
}