|
发表于 2018-11-19 15:48:54
|
显示全部楼层
const int ain = 44; //pin 44 is the first digital input for Gate A# z! v* R+ m1 N4 {9 | I# \. C
const int aout = 45;// pin 45 is the second digital input for Gate A! Q) N; d' O/ e" L) z
const int bin = 42; // same for Gate B z0 a" e- ]) m1 [) L
const int bout = 43; // same for Gate B
- T4 s' w3 A2 \" w% Q" F! O& i! [8 O( l# h- i
// Variables will change:9 m. d( W, V- W! d' w6 m/ f0 u6 s
int ins = 0; // counts ins and outs- A9 l: q& A1 L1 y
int outs = 0;+ [% S5 H0 o8 y) N8 d1 S/ r( ~
) J/ R$ z2 M8 A; X) d; F
int ai = 0; // Gate A 1st pin status- o* [$ z! ]; _3 |2 s p0 Y
int lai = 0; // Gate A last status of 1st pin$ z: ^) } w8 C4 K
int ao = 0; // Gate A 2nd pin status
) [& o4 R4 g$ X, N! X% Y2 U8 Xint lao = 0; // Gate A last status of 2nd pin6 ]# n! N2 A& d" M, e* e, J
0 ~) k5 A T3 J2 ~( | H, D
int bi = 0;
/ |+ w2 i) K. C, ?" Y, m+ _int lbi = 0;
, U; ]7 H2 g" o; |int bo = 0;& H$ x6 v& Y# O: k+ h
int lbo = 0;
7 C; u) U8 F) R# W9 y. x7 k4 d" _) m
int count = 0; // this just tests if there has been a change in our bee count& k6 \0 u# O/ q9 W0 n7 L& V3 y
int lcount = 0;. o$ U7 J8 b- O
/ ] g$ H9 a0 y9 X- i
' M5 s' l! f; ]3 i0 N/ @6 u
void setup() {
- }% a; r# g9 g // initialize the button pin as a input:' D, ^% D, ^& L8 j6 M- H
pinMode(ain, INPUT);- G5 {7 {5 n5 f; _, N% g
pinMode(aout, INPUT);
0 G7 d6 j' b& k, H1 ~ pinMode(bin, INPUT);. J- K, L' w1 a0 _5 P- K
pinMode(bout, INPUT);
. h R/ O* J( S8 z- x) J9 |
, H9 W7 `8 \2 ]2 S, X# F // initialize serial communication:) x5 @4 w/ w4 _6 i1 q6 e+ {
Serial.begin(38400); //a bit different than the Arduino here.... 38400
: P* ~% X# \9 t2 K; k$ F}, r! `3 b. T& o: Y, T
! H V7 M2 I) n( M0 vvoid loop() {
6 \/ V" a' d/ M // read the pushbutton input pin:9 p) {, p6 {- [( S
ai = digitalRead(ain);1 D$ ~% S5 o+ W" Q. p# Q2 h: N
ao = digitalRead(aout);
; o1 y( [# F- u+ m2 i6 ^3 v9 K4 X6 t) a- J
bi = digitalRead(bin); |; S" y1 {* H; i K
bo = digitalRead(bout);+ |$ }9 U' N$ I2 X" w
+ g5 n m+ F" t# r& [& S6 r+ v if (lai != ai){ // has the status if the 1st pin changed?1 W+ Z! l' ~) M; z
if (ai > ao) { // if yes, is the bee going in or out?9 v% d, ]+ d0 }% T
ins++; // if its going in add one bee to ins
% d( t# m: D; E& G }}! H3 i1 t; l0 {$ C G
if (lao != ao){ ' n. X6 x8 s; A' x! I
if (ao > ai) {2 V o" \/ t" f
outs++;, A5 E/ H9 `5 {3 n
}}
4 ]7 G4 i. z1 Y
9 B* R& j1 q3 C5 {: z8 A, @ if (lbi != bi){3 L% Q! S7 E8 | h6 V: f6 K' Z
if (bi > bo) {
# R0 o y7 G+ B. d( W ins++;7 V1 |# W8 s( P# l
}}
; |/ s1 r& f* k5 X \' e! q if (lbo != bo){
8 c* g* P, f5 `- K/ X2 p) y if (bo > bi) {2 {6 j( V1 E. P: r' [; U
outs++;5 Q0 T' c1 H* n
}}( l! I" R J5 d. F
" p/ B! V( y/ v7 J' ^" ~lai = ai; // updates the last status
7 o% \. ]' d r, D9 llao = ao;
# ?- I3 u, Q; c( Rlbi = bi;
+ j( ~9 Z6 q; C( _6 F, W! Z6 }8 hlbo = bo;
: ]5 X+ Q$ x4 I3 u
V9 a( P: e6 `. e6 zcount = ins + outs;# }- H, H* K$ H
$ T p6 p+ x/ @. r4 M6 {
if (lcount != count){ // if the count has changed we print the new count7 O; T1 q6 @& ]( Z2 L& a
6 E/ t8 o. ]' k& I& p" J& v1 E- I
Serial.print("number In: ");' F' u% a: t7 z& e' K
Serial.println(ins);
# q$ b% E; ^) T; n3 ? Serial.print("number Out: ");# T8 V; D: _7 s4 j
Serial.println(outs);: g: [2 H' v& ^8 C: y% j8 g
( R& y* b5 v8 H' \! Y C
lcount = count;
; O$ B! F9 a: g' \/ w/ D0 D}
8 J2 ?& n% i9 U3 j7 q2 A
7 V, N# D( P# l} |
|