Commit a0217353d6e914afe0f94b6ed39e3fd8a7aa4be7

tehtävä 19 - liikennevalot
demot/demo5/liikennevalot.c
(100 / 0)
  
1#include <wiringPi.h>
2#include <stdio.h>
3#include <unistd.h>
4
5int main (void)
6{
7 char nappi;
8 char valo;
9 int tila = 0; /* painikkeen tilamuuttuja */
10
11 int A_PUN = 4;
12 int A_KEL = 5;
13 int A_VIH = 6;
14
15 int J_PUN = 12;
16 int J_VIH = 16;
17 int J_KEL = 25;
18
19 int PAINIKE = 21;
20
21 if (wiringPiSetupGpio() == -1)
22 exit(1);
23
24 pinMode(A_PUN, OUTPUT);
25 pinMode(A_KEL, OUTPUT);
26 pinMode(A_VIH, OUTPUT);
27 pinMode(J_PUN, OUTPUT);
28 pinMode(J_VIH, OUTPUT);
29 pinMode(J_KEL, OUTPUT);
30
31 pinMode(PAINIKE, INPUT);
32
33 nappi = '0';
34 valo = '0';
35
36 digitalWrite(A_VIH, HIGH);
37 digitalWrite(J_PUN, HIGH);
38
39 for (;;)
40 {
41 tila = digitalRead(PAINIKE);
42
43 if (nappi == '0' && valo == '0')
44 {
45 /* odota napin painallusta */
46 if (tila == HIGH)
47 {
48 digitalWrite(J_KEL, HIGH);
49 digitalWrite(A_VIH, LOW);
50 digitalWrite(A_KEL, HIGH);
51 sleep(2);
52 digitalWrite(A_KEL, LOW);
53 digitalWrite(A_PUN, HIGH);
54 digitalWrite(J_VIH, HIGH);
55 digitalWrite(J_KEL, LOW);
56 digitalWrite(J_PUN, LOW);
57 nappi = '1';
58 valo = '1';
59 }
60 }
61
62 else if (nappi == '1' && valo == '1')
63 {
64 /* jaa tilaan kunnes nappia painetaan */
65 if (tila == LOW)
66 nappi = '0';
67 }
68
69 else if (nappi == '0' && valo == '1')
70 {
71 if (tila == HIGH)
72 {
73 digitalWrite(A_KEL, HIGH);
74 sleep(2);
75 digitalWrite(A_PUN, LOW);
76 digitalWrite(A_VIH, HIGH);
77 digitalWrite(A_KEL, LOW);
78 digitalWrite(J_PUN, HIGH);
79 digitalWrite(J_VIH, LOW);
80 nappi = '1';
81 valo = '0';
82 }
83 }
84
85 else if (nappi == '1' && valo == '0')
86 {
87 /* jaa tilaan kunnes nappia painetaan */
88 if (tila == LOW)
89 {
90 nappi = '0';
91 // sleep(1);
92 }
93
94
95 }
96
97 }
98
99 return 0;
100}