arduino 中的scoop库

ArduinoSCoop多线程 实例程序代码 /*#include #include */ #include //多线程程序 #include //超声波传感器 #include //电机传感器 //LiquidCrystal_I2C lcd(0x27,16,2); defineTask(LEDTask);//LED 灯的线程 defineTask(ServoTask);//舵机线程 #define TRIGGER_PIN 2 #define ECHO_PIN 3 #define MAX_DISTANCE 400 NewPing sonar(TRIGGER_PIN,ECHO_PIN,MAX_DISTANCE);//超声波传感器 Servo myservo;//电机传感器 const int replyPin=A2;//光敏传感器 const int soundPin=A0;//声音传感器 const int senorPin=8;//触摸感应键 const int rPin=7;//Led const int ledPin=6; //Led int value=0;//光敏传感器 int sound=0;//声音传感器 int Senor=-1; void LEDTask::setup(){ pinMode(replyPin,INPUT); pinMode(soundPin,INPUT); pinMode(rPin,OUTPUT); } void LEDTask::loop(){ value=analogRead(replyPin);//光敏传感器感应到光强度 Serial.print("光敏传感器感应到光强度:"); Serial.println(value); sound=analogRead(soundPin);//声音感应器感应到声音大小 Serial.print("声音感应器感应到声音大小:"); Serial.println(sound); unsigned int uS=sonar.ping();//超声波传感器感应到数据 Serial.print("超声波传感器感应到数据:"); Serial.println(uS/US_ROUNDTRIP_CM); if(value>400&&(sound>80||uS/US_ROUNDTRIP_CM<5)){ analogWrite(rPin,255); analogWrite(ledPin,0); delay(5000); }else{ analogWrite(rPin,0); analogWrite(ledPin,0); } } void ServoTask::setup(){ pinMode(senorPin,INPUT); myservo.attach(9); myservo.write(0); } void ServoTask::loop(){ Senor=digitalRead(senorPin);//触摸感应键感应 Serial.print("触摸感应键感应:"); Serial.println(Senor); if(Senor==1){ myservo.write(30); delay(100); myservo.write(60); delay(100); myservo.write(90); delay(3000); myservo.write(60); delay(100); myservo.write(30); delay(100); myservo.write(0); } } void setup(){ mySCoop.start(); Serial.begin(9600); } void loop(){ yield(); } //这段程序有一个舵机、一个LED灯(两者都是执行器),两个执行器执行时需要相互不干扰,所以使用多线程
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值