Back
Project 4
Let us Say "No" bravely!
- Project Description -
Say No —— A remote control device for controlling finger shaking
Put the servo in the finger made of ultra-light clay,
and the infrared remote control can control it to start shaking and stopping.
MODE 1 : DEFENSE BUFF——>NO;
MODE2 : RIDICULE BUFF——>YOU CAN NOT DO IT
- Materials -
-
1x Infrared remote control
-
1x Infrared receiver
-
1× Steering gear
-
Ultralight clay
- Fritzing -

- Code -
#include <Servo.h>
#define IR_IN 2 //红外接收
int Pulse_Width=0;//存储脉宽
int ir_code=0x00;//命令值
Servo myservo;
int pos = 0;
int servoPin = 11;
void timer1_init(void)//定时器初始化函数
{
TCCR1A = 0X00;
TCCR1B = 0X05;//给定时器时钟源
TCCR1C = 0X00;
TCNT1 = 0X00;
TIMSK1 = 0X00; //禁止定时器溢出中断
}
void remote_deal(void)//执行译码结果函数
{
switch(ir_code)
{
//case 0xff00://停止
//myservo.write(0);
//break;
case 0xfe01://VOL+
servo_control_up();
break;
case 0xf609://VOL-
setup();
break;
}
}
char logic_value()//判断逻辑值“0”和“1”子函数
{
while(!(digitalRead(IR_IN))); //低等待
Pulse_Width=TCNT1;
TCNT1=0;
if(Pulse_Width>=7&&Pulse_Width<=10)//低电平560us
{
while(digitalRead(IR_IN));//是高就等待
Pulse_Width=TCNT1;
TCNT1=0;
if(Pulse_Width>=7&&Pulse_Width<=10)//接着高电平560us
return 0;
else if(Pulse_Width>=25&&Pulse_Width<=27) //接着高电平1.7ms
return 1;
}
return -1;
}
void pulse_deal()//接收地址码和命令码脉冲函数
{
int i;
//执行8个0
for(i=0; i<8; i++)
{
if(logic_value() != 0) //不是0
return;
}
//执行6个1
for(i=0; i<6; i++)
{
if(logic_value()!= 1) //不是1
return;
}
//执行1个0
if(logic_value()!= 0) //不是0
return;
//执行1个1
if(logic_value()!= 1) //不是1
return;
//解析遥控器编码中的command指令
ir_code=0x00;//清零
for(i=0; i<16;i++ )
{
if(logic_value() == 1)
{
ir_code |=(1<<i);
}
}
}
void remote_decode(void)//译码函数
{
TCNT1=0X00;
while(digitalRead(IR_IN))//是高就等待
{
if(TCNT1>=1563) //当高电平持续时间超过100ms,表明此时没有按键按下
{
ir_code = 0xff00;
return;
}
}
//如果高电平持续时间不超过100ms
TCNT1=0X00;
while(!(digitalRead(IR_IN))); //低等待
Pulse_Width=TCNT1;
TCNT1=0;
if(Pulse_Width>=140&&Pulse_Width<=143)//9ms
{
while(digitalRead(IR_IN));//是高就等待
Pulse_Width=TCNT1;
TCNT1=0;
if(Pulse_Width>=68&&Pulse_Width<=72)//4.5ms
{
pulse_deal();
return;
}
else if(Pulse_Width>=34&&Pulse_Width<=36)//2.25ms
{
while(!(digitalRead(IR_IN)));//低等待
Pulse_Width=TCNT1;
TCNT1=0;
if(Pulse_Width>=7&&Pulse_Width<=10)//560us
{
return;
}
}
}
}
void setup()
{
myservo.attach(servoPin);
//pinMode(LED_RED,OUTPUT);//设置与红灯连接的引脚为输出模式
pinMode(IR_IN,INPUT);//设置红外接收引脚为输入
myservo.write(0); //close cap on power on
delay(500);
myservo.detach();
}
void servo_control_up()
{
for(int i = 0; i < 5; i++)
{
//Change distance as per your need
myservo.attach(servoPin);
delay(1);
myservo.write(0);
delay(500);
myservo.write(30);
delay(500);
}
}
void loop()
{
timer1_init();//定时器初始化
while(1)
{
remote_decode(); //译码
remote_deal(); //执行译码结果
}
}
)
Project Process
—— discussion ——

—— processing——

—— coding ——

—— final effect ——
.png)
.png)
.png)
—— problem ——
The remote control can control the finger to start shaking, but cannot stop it.
—— solution ——
With the help of Bob teacher, We solved the code problem.