|
发表于 2018-11-19 15:48:54
|
显示全部楼层
const int ain = 44; //pin 44 is the first digital input for Gate A
- g" O+ L4 i _- `0 [8 Wconst int aout = 45;// pin 45 is the second digital input for Gate A
* m: {6 X. [- Cconst int bin = 42; // same for Gate B! W: d( x% x Z/ n1 M& n" H! X* s
const int bout = 43; // same for Gate B' U/ N E; C1 j
" d) X4 e. Z& R! \0 w
// Variables will change:+ x1 J& X# t1 A* \
int ins = 0; // counts ins and outs6 {3 z6 k( e- M* c5 H
int outs = 0;* \ f* T/ D/ {* R1 g, o- E2 ?
% D _& y- Y' r. b
int ai = 0; // Gate A 1st pin status: z' W6 g; N7 A' R' V6 f) S
int lai = 0; // Gate A last status of 1st pin# {+ F1 S$ B9 d- @ F, x! V
int ao = 0; // Gate A 2nd pin status
: z" v8 c% D# s- J8 W6 A; Y8 B; Pint lao = 0; // Gate A last status of 2nd pin
# w" D, R6 X# g$ x2 x- y7 F& e* B
int bi = 0;6 z) x& q! v5 H/ |' _: j4 F
int lbi = 0;
6 C) U% V# d( @+ p2 fint bo = 0;
+ s, R2 E3 x a2 zint lbo = 0;/ D" ?; U+ m* K% L% l i
- K: z& s/ u+ }* t# Bint count = 0; // this just tests if there has been a change in our bee count8 R% I- O# p# X$ H& K8 H
int lcount = 0;
! |" H, N+ V. `3 k) s# o) P
& A/ ?6 ? l) `7 V/ f% ]" p, h/ Q9 ] p( K
void setup() {
# J4 O% [5 R ~# B5 e, Y // initialize the button pin as a input:
) `/ s" C( W. o$ B pinMode(ain, INPUT);
6 M) g, D! Y2 l/ V, p pinMode(aout, INPUT);
/ H- d# k! i. `0 D! V5 a pinMode(bin, INPUT);
* Q d& L/ x% R+ T4 K pinMode(bout, INPUT);
) s" z* ^8 n1 g' p: N1 y5 Q
0 Y( Z, `! M, ^# j9 w( V // initialize serial communication: z+ R: Q- @0 ^3 S
Serial.begin(38400); //a bit different than the Arduino here.... 38400. B2 e2 p. x$ {% t
}
; o( V7 ?/ }+ H4 \; j
9 ^# K! S" E: @. o2 S1 g+ a; v% Nvoid loop() {
9 g: C; [' w# i" ~ // read the pushbutton input pin:) i) t7 F6 H5 ^5 Z0 a* @, y
ai = digitalRead(ain);2 I. E4 ^% b. [& }
ao = digitalRead(aout);1 X& F4 [. v, l
5 V1 b. L* n$ y$ C. w* e bi = digitalRead(bin);
9 I9 E& S1 g& X) S2 b0 h6 p: e bo = digitalRead(bout);1 f5 N, J7 ?* a
1 |" k, p i7 e if (lai != ai){ // has the status if the 1st pin changed?0 B5 }; p5 Q; s* G) G' _% M3 g4 l
if (ai > ao) { // if yes, is the bee going in or out?
$ O3 d) i+ u6 j) t- Y$ A' O! {! E ins++; // if its going in add one bee to ins
# h$ }8 g: j; e& L: p% k }}1 O0 H! J! ~) _7 W6 L7 C, B' w
if (lao != ao){ & G8 ]3 a( ~; x$ {5 x4 H2 T' o+ Q
if (ao > ai) {
4 H) _2 `3 D+ u# W outs++;$ h# d. _/ i6 g1 q& s$ [
}} C: J5 Y: a/ z; Y
9 O# C* M# B, M! j7 X8 N7 g6 ]; X3 y# k if (lbi != bi){4 i8 S+ ]" Y4 l+ H* z( R! _
if (bi > bo) {
1 O. K4 O" \8 ?- _ ins++;, G* V* ~7 ]6 O+ F
}}
; @# b+ A1 M* e$ k4 _. e. i if (lbo != bo){ 8 w' x6 Z/ ~& a2 ?% m+ ~4 ~: E
if (bo > bi) {: a9 p8 I# P4 E2 x
outs++;: V) v# [& X6 i
}}3 R5 @6 H; P0 u1 }: F# E
- X- z# s4 g3 A* y/ R3 ilai = ai; // updates the last status3 d, I* C1 n& { z
lao = ao;3 c* O" P$ B5 k* F* S
lbi = bi;4 X/ h' U/ ` G* N7 F- r% p
lbo = bo;# z7 c; c9 V# p+ l" h: {$ b
# q# v# k. G$ q* m% h1 N, l, A1 F/ t
count = ins + outs;7 G3 c/ Y/ C" Q: I' i2 M
0 M# |* y; j1 _! ~ L6 ~
if (lcount != count){ // if the count has changed we print the new count
' W7 {0 B% G m
5 @: t' e @) y, Z+ W( ]) `5 H$ k9 h0 H Serial.print("number In: ");
' J* e0 d! X% h' n; Q# n t# T Serial.println(ins);
4 v3 I a) D, \/ m/ `- _. Y5 |* U Serial.print("number Out: ");/ L8 K/ V9 k9 G. ~( V
Serial.println(outs);
, \$ G1 {: z! z/ `% {
1 K7 j+ N( B+ ~" e1 `8 flcount = count;
& f0 J, B) p& b0 _, N* X}2 r. s R2 C |& h
: ?5 H; I! K1 I0 N+ n} |
|