1单纯统计进出人数
#define pin_RED1 PA0 #define pin_RED2 PA1 int people_useNum=0; int state_RED1=0;// 红外1 int state_RED2=0;// 红外2 int car_state[2]={0,0}; int car_oldstate[2]={0,0}; void setup(){ Serial.begin(9600); pinMode(pin_RED1, INPUT); pinMode(pin_RED2, INPUT); } void loop(){ String Lightmsg="People Num:"+String(people_useNum); String Lightmsg1="IN_RED1:"+String(state_RED1); String Lightmsg2="OUT_RED2:"+String(state_RED2); state_RED1=!digitalRead(pin_RED1); state_RED2=!digitalRead(pin_RED2); car_state[0]=state_RED1; car_state[1]=state_RED2; if( car_state[0]==1){ if(car_oldstate[0]==0){ Serial.println("有人进入入口闸机"); car_oldstate[0]=1; } else{ Serial.println("人在入口闸机尚未离开"); } } else{ if(car_oldstate[0]==0){ Serial.println("入口等待有人进来"); } else{ Serial.println("人已经离开入口闸机,人数+1"); people_useNum=people_useNum+1; car_oldstate[0]=0; } } if( car_state[1]==1){ if(car_oldstate[1]==0){ Serial.println("有人进入出口闸机"); car_oldstate[1]=1; } else{ Serial.println("人在出口闸机尚未离开"); } } else{ if(car_oldstate[1]==0){ //Serial.println("出口闸机等待有人进来"); } else{ Serial.println("人已经离开出口闸机,人数-1"); people_useNum=people_useNum-1; if(people_useNum<0){people_useNum=0;} car_oldstate[1]=0; } } Serial.print("当前人数:");Serial.println(people_useNum); delay(1000); }
标签:RED2,RED1,car,oldstate,stm32,state,例子,Serial,人数 From: https://www.cnblogs.com/gooutlook/p/18148165