|
发表于 2018-11-19 15:48:54
|
显示全部楼层
const int ain = 44; //pin 44 is the first digital input for Gate A0 `& S6 x% c) q, f& A. G7 p
const int aout = 45;// pin 45 is the second digital input for Gate A5 B5 ?; K7 D) s" @- t7 I
const int bin = 42; // same for Gate B: u- e. u1 @& M! P& h. P; S# A+ R
const int bout = 43; // same for Gate B
+ E9 X+ B6 I9 v. a s0 c/ s4 p" Q3 _1 N3 @( L# {. e4 y2 {! z8 r
// Variables will change:. \8 v0 ~" P+ M* \
int ins = 0; // counts ins and outs
6 e/ j( h, H2 o# I! L) sint outs = 0;
/ v3 h6 Y+ X' T' o. M, q- u K) M' E F: D: y; D" ]
int ai = 0; // Gate A 1st pin status; K; o) G4 ^; V' J" O6 s
int lai = 0; // Gate A last status of 1st pin/ L# @ d w4 O: {9 o; q, a! ~" }
int ao = 0; // Gate A 2nd pin status# r0 D( Q9 X$ A3 K" ~0 [/ h: P
int lao = 0; // Gate A last status of 2nd pin" X7 B( I1 g) |/ A
6 N% i) i( I$ F& N9 ~% a/ |- W% qint bi = 0;
$ g+ A3 I4 t8 F# G5 l+ @* Cint lbi = 0;; F& s7 ]! B v3 Y
int bo = 0;; Z4 G3 r' H" w- O" J
int lbo = 0;9 S% j" f& f9 q' m
5 }& `; G+ [* b1 n
int count = 0; // this just tests if there has been a change in our bee count
7 _" C& `1 s% }+ _7 T" E7 k3 `int lcount = 0;& C5 r2 D- a" _
* A# H$ s) ]. r/ [; w- ~5 n# w
! W. r9 I- B; s* G% U8 G: {void setup() {
/ s+ Y' Q" u3 e. a7 W+ p+ N // initialize the button pin as a input:5 `6 V1 P7 S+ c8 N, S& T
pinMode(ain, INPUT);
4 @5 F/ M: G% p- r7 d( c3 g pinMode(aout, INPUT);
2 z% Z: A# ~1 h8 P2 s5 x3 v$ y; N! M pinMode(bin, INPUT);
7 U* {% a- G' C0 y& e$ F8 J |, ` pinMode(bout, INPUT);5 j+ _8 Z$ U k7 k' I& \# B
! U6 [( r X# Y5 t8 y, P // initialize serial communication:
9 @6 d& B% c! ^ Serial.begin(38400); //a bit different than the Arduino here.... 38400: Y) ~5 g4 [5 z" X! `
}( l+ z% u$ s# n. w
1 K' g( m# F& f0 {3 W$ e, Jvoid loop() {, s. j, h3 x* _
// read the pushbutton input pin:1 [2 f3 H5 K9 q. G5 `) }
ai = digitalRead(ain);9 @: ]3 B; `: z# L/ ^
ao = digitalRead(aout);
/ t0 R& R4 y9 r
) Y, x# T3 s; k2 Y' s bi = digitalRead(bin);0 W& i G' I+ T) b
bo = digitalRead(bout);/ Y. j% T5 o2 y0 V( S" B/ J- N; Q
; @7 |6 l- `7 q6 x+ U+ D" o if (lai != ai){ // has the status if the 1st pin changed?& d& C- [9 T/ w! P! @, c5 \
if (ai > ao) { // if yes, is the bee going in or out?6 P2 A% B; b F% U/ A
ins++; // if its going in add one bee to ins4 Z- ?% g+ g7 p( m
}}
' X' K! J/ V4 S _$ Y, @ if (lao != ao){
1 l1 I( z1 n' S2 b; U% k if (ao > ai) {
: W& } }# W8 H, i. J& ^. R8 F8 A outs++;7 C# a1 z4 O. r5 x6 \( \
}}
7 u$ {4 V. Q- u4 v# ^- [ ( \2 `6 o; {6 s2 X9 v3 k' Z( R
if (lbi != bi){
0 \6 o9 r2 Y( I2 V; j0 d if (bi > bo) { * U" T; c/ l# _0 C" i) U, Y
ins++;
9 N5 `$ V1 f# v, i }}
( Q. {) b! M( O2 d+ T( }9 J' G if (lbo != bo){ 3 O Q7 b, b9 @) Q6 M8 Q
if (bo > bi) {9 c$ M' v$ M' v- [% e
outs++;
, h- m. U% q2 A }}
) b8 e8 w2 ^. V) y w, p9 E! i ! p+ q" M- a* z+ d% j6 `( b
lai = ai; // updates the last status: e( ~7 t- R V% E
lao = ao;
/ V3 @; Z6 B5 b- \lbi = bi;* l3 c) e2 A% D6 X$ i3 a" _
lbo = bo;; K; A% O( i' j5 s1 i
' ~* y# ?+ k4 G+ ^count = ins + outs;9 x) I: T/ c/ ?5 z# X
/ Q m/ V ~/ u9 g% T- F, }if (lcount != count){ // if the count has changed we print the new count! C4 Q4 p* m9 ]7 y
/ H( ?% T) I3 w9 ?+ j5 \6 T Serial.print("number In: ");# [4 q; q4 I7 s+ ~$ b' J
Serial.println(ins);$ `4 a" _" I4 J5 L4 T
Serial.print("number Out: ");
2 ?' J1 W$ j8 M* P5 K Serial.println(outs);
+ @5 \2 I/ ~7 _9 m
) e+ T6 U6 h% w0 e" glcount = count;
1 L d' _8 S* E3 h; N* M}9 ] g. y( n+ Y; m" h: r1 w- S
( ~# F' |8 I5 P3 V" V4 h! c} |
|