|
发表于 2018-11-19 15:48:54
|
显示全部楼层
const int ain = 44; //pin 44 is the first digital input for Gate A
8 o6 R, s' A. \2 Aconst int aout = 45;// pin 45 is the second digital input for Gate A
& g7 c9 }* R, X( P+ ]8 Uconst int bin = 42; // same for Gate B
9 p* ]0 O- d/ b7 d5 W" N9 Z yconst int bout = 43; // same for Gate B9 T- R, h' B/ v/ w+ c
* A' r* G/ K6 h8 j1 ]# i* {
// Variables will change:" }3 N' g# h/ D' X
int ins = 0; // counts ins and outs
, w) N4 f$ _* f q. V: Rint outs = 0;
6 i+ e. k; V. P3 `8 N
; B! t, B& O) ^; p; k0 v" Nint ai = 0; // Gate A 1st pin status
9 t5 ^! f, _0 P9 [9 jint lai = 0; // Gate A last status of 1st pin2 t' y5 I7 I, H1 T. T1 Z
int ao = 0; // Gate A 2nd pin status
8 P6 ]7 D1 _7 @+ b6 `8 T, cint lao = 0; // Gate A last status of 2nd pin
. { }+ ~. o) B! g2 F5 d: M& }
- G1 t, J% B. l# e. w8 Pint bi = 0;
. z" ~% G5 X; Q8 `int lbi = 0;. p* w( o5 P; w; z$ ]$ V
int bo = 0;* ^6 Y7 \( b) P1 k- x) R( w3 f
int lbo = 0;3 _* j" E9 p5 ?
$ {: F( {6 V: r! \7 b! O. R
int count = 0; // this just tests if there has been a change in our bee count
1 K$ A1 W8 Y1 N, _int lcount = 0;, N7 ~6 D$ E$ Q1 F& l% p: {, w
4 b( t7 D0 d! A* V$ u3 M' f
. n. l7 K7 h- A! G, i H7 J$ f
void setup() {
) m8 ` x4 G) H8 v // initialize the button pin as a input:7 z. C8 l3 D' A
pinMode(ain, INPUT);: v8 |9 G% ]7 G2 B
pinMode(aout, INPUT);4 `$ }5 ~5 a9 z& l5 B4 n
pinMode(bin, INPUT);9 \5 l1 W& b @$ D: I
pinMode(bout, INPUT);
7 {' {) V& N6 v5 j- i
4 L& O. N/ U- r) I9 W // initialize serial communication:* r0 d) B0 Q: u8 Y! w
Serial.begin(38400); //a bit different than the Arduino here.... 384002 Q, O4 Z6 R* Y1 Y4 x& a
}
* W: Q) T" I$ A2 f+ s3 o# k; W0 P# A! w- D' p
void loop() {
d3 s3 |# M. ] // read the pushbutton input pin:2 S8 X/ ]- m; o8 X/ |9 `7 s
ai = digitalRead(ain);
% c+ V) O. f" }, W+ B$ |* W ao = digitalRead(aout);4 {$ J8 D! b7 D9 Y3 k: L+ I
8 e4 {3 e3 O! g' u
bi = digitalRead(bin);4 ~: g7 ? _" e# Y4 z6 C* I
bo = digitalRead(bout);
3 e. Q7 S+ c9 D6 k- N# y
1 P/ Y$ _3 [9 B/ q1 p- ~ if (lai != ai){ // has the status if the 1st pin changed?# `: K5 _1 w1 T9 J4 k! d) H3 q
if (ai > ao) { // if yes, is the bee going in or out?: J6 l: \8 j; g& e S2 G7 s( N7 F
ins++; // if its going in add one bee to ins6 g; b7 P6 V2 i4 @/ r5 [
}}- [9 a- B. F+ P
if (lao != ao){
$ A( A7 `9 _' r if (ao > ai) {5 u* F+ Q: D! c. M% [' D
outs++;* }* K3 f5 j$ [ x% m4 B
}}4 n c3 W+ m$ K; H$ _7 [
4 ]( e5 M# b+ {& F- Z4 z& { if (lbi != bi){
1 V) h: f# G: f# [0 E4 O' H/ q& b if (bi > bo) { 9 M$ o; S2 z; C6 Y" P5 Q h2 k& l
ins++;" [* q. r: Y) b2 q4 o
}}
% j8 ]2 w* o4 I1 M4 H2 k if (lbo != bo){ 4 r: R! _- ?# s$ Q" h/ N7 M
if (bo > bi) {
|3 A: ]& x1 m- ~1 D outs++;* z' J8 o, q/ G2 x% c
}}9 f5 o, J% r3 V% @- t
1 t4 U/ M; x% U/ S
lai = ai; // updates the last status# b f6 U; W0 j' i
lao = ao;# ]8 e; I+ p- Q2 C3 K/ I
lbi = bi;
$ h9 m$ O. l9 H: G5 p; albo = bo;1 X+ u* P( v4 ^ ?: X6 N! [- c
. k j5 b2 ~. W. P$ W* P
count = ins + outs;( s* Z' t) C7 x7 L" |! [+ F
. c4 I& B) S' E1 Y% G2 _
if (lcount != count){ // if the count has changed we print the new count
: M" [* r1 U* D- i5 V i5 n& @) U6 J
Serial.print("number In: ");" l. v8 ^/ ^% }+ s: I8 J# R
Serial.println(ins);
- ?& ? T, n2 B; l4 U7 e Serial.print("number Out: ");/ D& l" R: x4 D% ] f7 s
Serial.println(outs);
3 d( g3 z7 y1 h. ?4 W) n: @/ _: F# \ l: j, E* O3 m
lcount = count;& Z) Z3 G) H! L+ {- J
}7 r) |+ T5 x1 Y1 m
# x" n4 S h6 l" x! ^/ t. v' G} |
|