Post by 徐瑶锦 on Feb 8, 2021 2:08:23 GMT
#include <IRremote.h>
#include <Steper_28BYJ48.h>
Steper_28BYJ48 A(16, 3, 4, 5, 6);//步进对象
int Buzzerpin = 7;//蜂鸣器接3口
int AAA = 0;
const int RECV_PIN = 9;//声明两个变量
const int RED = 13;
volatile int CSB;
const int GREEN= 12;
IRrecv irrecv(RECV_PIN);//声明一个接收对象
decode_results results; //声明一个存放结果的对象
unsigned char R;
float checkdistance_11_10()
{
digitalWrite(11, LOW);
delayMicroseconds(2);
digitalWrite(11, HIGH);
delayMicroseconds(10);
digitalWrite(11, LOW);
float distance = pulseIn(10, HIGH) / 58.00;
//delay(10);
return distance;
}
void Alarm() //蜂鸣器发出警报
{
for(int i=0;i<100;i++){
digitalWrite(Buzzerpin,HIGH); //发声音
delay(1);
digitalWrite(Buzzerpin,LOW); //不发声音
delay(1); //修改延时时间,改变发声频率
}
}
void AlarmA() //蜂鸣器发出警报
{
if( AAA == 0 )
{
digitalWrite(Buzzerpin,HIGH); //发声音
delay(1);
digitalWrite(Buzzerpin,LOW); //不发声音
delay(1); //修改延时时间,改变发声频率
}
}
void setup()
{
pinMode(Buzzerpin,OUTPUT); //设置蜂鸣器接口为输出状态
pinMode(11, OUTPUT);
pinMode(10, INPUT);
//Serial.begin(9600);
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
Serial.begin(9600);//设置窜口的工作频率
//Serial.println(checkdistance_11_10());
irrecv.enableIRIn(); // 运行接收对象的工作方法,即启动接收工作
pinMode(RED, LOW);
pinMode(GREEN, LOW);
}
void loop()
{
CSB = checkdistance_11_10();
Serial.println(CSB);
if (irrecv.decode(&results)) //如果接收对象转码接收的结果为真
{
Serial.println(results.value);//打印收接收到的数值
delay(300);//可以减少误操作,使发送的数据比较准确
if ( results.bits > 0 )//如果结果的长度大于0
{
if ( 83573445 == results.value )
{
Serial.println("red");
digitalWrite( RED, HIGH ); //写D13口高电平
AAA=0;
for (R=0;R<30;R++)
{
//if( A == 0 )
//{
Alarm();
CSB = checkdistance_11_10();
Serial.println(CSB);
if ( CSB <= 4 )
{
AAA=1;
A.step(-750);//主轴前进12步,67.5度
//delay(6000);
//delay(1000);
//digitalWrite(GREEN, LOW ); //写12口低电平
Serial.println("green");
digitalWrite( GREEN, HIGH ); //写D12口高电平
digitalWrite(RED, LOW ); //写13口低电平
}
delay(100);
}
//A.step(120);//主轴前进12步,67.5度
//delay(6000);
//delay(1000);
//digitalWrite(RED, LOW ); //写13口低电平
}
if ( 83606085 == results.value )
{
Serial.println("green");
digitalWrite( GREEN, HIGH ); //写D12口高电平
//delay(1000);
//digitalWrite(GREEN, LOW ); //写12口低电平
}
if ( 83589765 == results.value )
{
Serial.println("LOW");
digitalWrite( GREEN, LOW ); //写D12口高电平
digitalWrite(RED, LOW ); //写13口低电平
//delay(1000);
//digitalWrite(GREEN, LOW ); //写12口低电平
}
}
irrecv.resume(); //重置接收对象,准备好接收下一个红外信号
}
}
#include <Steper_28BYJ48.h>
Steper_28BYJ48 A(16, 3, 4, 5, 6);//步进对象
int Buzzerpin = 7;//蜂鸣器接3口
int AAA = 0;
const int RECV_PIN = 9;//声明两个变量
const int RED = 13;
volatile int CSB;
const int GREEN= 12;
IRrecv irrecv(RECV_PIN);//声明一个接收对象
decode_results results; //声明一个存放结果的对象
unsigned char R;
float checkdistance_11_10()
{
digitalWrite(11, LOW);
delayMicroseconds(2);
digitalWrite(11, HIGH);
delayMicroseconds(10);
digitalWrite(11, LOW);
float distance = pulseIn(10, HIGH) / 58.00;
//delay(10);
return distance;
}
void Alarm() //蜂鸣器发出警报
{
for(int i=0;i<100;i++){
digitalWrite(Buzzerpin,HIGH); //发声音
delay(1);
digitalWrite(Buzzerpin,LOW); //不发声音
delay(1); //修改延时时间,改变发声频率
}
}
void AlarmA() //蜂鸣器发出警报
{
if( AAA == 0 )
{
digitalWrite(Buzzerpin,HIGH); //发声音
delay(1);
digitalWrite(Buzzerpin,LOW); //不发声音
delay(1); //修改延时时间,改变发声频率
}
}
void setup()
{
pinMode(Buzzerpin,OUTPUT); //设置蜂鸣器接口为输出状态
pinMode(11, OUTPUT);
pinMode(10, INPUT);
//Serial.begin(9600);
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
Serial.begin(9600);//设置窜口的工作频率
//Serial.println(checkdistance_11_10());
irrecv.enableIRIn(); // 运行接收对象的工作方法,即启动接收工作
pinMode(RED, LOW);
pinMode(GREEN, LOW);
}
void loop()
{
CSB = checkdistance_11_10();
Serial.println(CSB);
if (irrecv.decode(&results)) //如果接收对象转码接收的结果为真
{
Serial.println(results.value);//打印收接收到的数值
delay(300);//可以减少误操作,使发送的数据比较准确
if ( results.bits > 0 )//如果结果的长度大于0
{
if ( 83573445 == results.value )
{
Serial.println("red");
digitalWrite( RED, HIGH ); //写D13口高电平
AAA=0;
for (R=0;R<30;R++)
{
//if( A == 0 )
//{
Alarm();
CSB = checkdistance_11_10();
Serial.println(CSB);
if ( CSB <= 4 )
{
AAA=1;
A.step(-750);//主轴前进12步,67.5度
//delay(6000);
//delay(1000);
//digitalWrite(GREEN, LOW ); //写12口低电平
Serial.println("green");
digitalWrite( GREEN, HIGH ); //写D12口高电平
digitalWrite(RED, LOW ); //写13口低电平
}
delay(100);
}
//A.step(120);//主轴前进12步,67.5度
//delay(6000);
//delay(1000);
//digitalWrite(RED, LOW ); //写13口低电平
}
if ( 83606085 == results.value )
{
Serial.println("green");
digitalWrite( GREEN, HIGH ); //写D12口高电平
//delay(1000);
//digitalWrite(GREEN, LOW ); //写12口低电平
}
if ( 83589765 == results.value )
{
Serial.println("LOW");
digitalWrite( GREEN, LOW ); //写D12口高电平
digitalWrite(RED, LOW ); //写13口低电平
//delay(1000);
//digitalWrite(GREEN, LOW ); //写12口低电平
}
}
irrecv.resume(); //重置接收对象,准备好接收下一个红外信号
}
}