|
|
初学arduino,做了一个模拟的报靶系统,用arduino实现自己的小创意真是件开心的事情~发上来大家一起交流学习
智能报靶系统主要分为两大部分,一个是报靶,一个是遥控控制靶子的起、倒。
实验器材
uno板子*2
舵机*4
液晶显示屏*1
锡箔纸
温度传感器*1
红外接收*1
一、报靶模块
这个是我们的精华,在靶子的前后两面各贴一张锡箔纸,把锡箔纸剪成环形,每一环代表一个靶数,不同的环接在不同的数字口,当子弹穿过靶子,相当于导通正反两面的锡箔纸,给相应的分区一个高电平,板子读取相应数字口的高电平,在液晶屏上显示不同的环数。一共分了十个区,接在不同的数字口。同时多加了一个温度传感器显示温度。原理很简单,现在报靶系统有很多高科技的,我们是经济适用型~
图片
代码
#include <LiquidCrystal.h> //申明1602液晶的函数库
//申明1602液晶的11个引脚所连接的Arduino数字端口
LiquidCrystal lcd(12,11,10,9,8,15,14);
int redpin=5;
int yellowpin=6;
int greenpin=7;
int red;
int yellow;
int green;
int bluepin=2;
int blackpin=4;
int blue;
int black;
int rpin=16;
int ypin=17;
int gpin=18;
int r;
int y;
int g;
int blupin=3;
int blapin=13;
int blu;
int bla;
int potPin = A5; //设置模拟口5为LM35的信号输入端口
float temperature = 0; //设置temperature为浮点变量
long val=0; //设置val为长整数变量
void setup()
{pinMode(redpin,INPUT);
pinMode(yellowpin,INPUT);
pinMode(greenpin,INPUT);
pinMode(bluepin,INPUT);
pinMode(blackpin,INPUT);
pinMode(rpin,INPUT);
pinMode(ypin,INPUT);
pinMode(gpin,INPUT);
pinMode(blupin,INPUT);
pinMode(blapin,INPUT);
lcd.begin(16,2); //初始化1602液晶工作模式
//定义1602液晶显示范围为2行16列字符
lcd.setCursor(0,0); //把光标定位在第0行,第0列
lcd.print("Ring"); //显示
delay(200);
}
void loop()
{
val = analogRead(potPin); //val变量为从LM35信号口读取到的数值
temperature = (val*0.0048828125*1000); //把读取到的val转换为温度数值的10倍
lcd.setCursor(9, 1) ; //设置光标位置为 x第二行第一个位置
lcd.print((long)temperature/10); //显示温度整数位
lcd.print("."); //显示小数点
lcd.print( (long)temperature%10); //显示温度小数点后一位
lcd.print((char)223); //显示o符号
lcd.print("C"); //显示字母C
delay(200); //延时2秒,这里也就是刷新速度。
red=digitalRead(redpin);
if(red==HIGH)
{ lcd.setCursor(0,2); //显示8环
lcd.print("08");
lcd.setCursor(4,2); //显示“<<"矫正射击,表示打在8环偏右,需要向左偏
lcd.print((char)60);
lcd.setCursor(5,2);
lcd.print((char)60);
delay(1000); }
yellow=digitalRead(yellowpin);
if(yellow==HIGH)
{lcd.setCursor(0,2); //显示9环
lcd.print("09");
lcd.setCursor(4,2); //显示“<<"矫正射击,表示打在9环偏右,需要向左偏
lcd.print((char)60);
lcd.setCursor(5,2);
lcd.print((char)60);
delay(1000); }
green=digitalRead(greenpin);
if(green==HIGH)
{lcd.setCursor(0,2); //显示10环
lcd.print("10");
lcd.setCursor(4,2); //显示“<<"矫正射击,表示打在10环偏右,需要向左偏
lcd.print((char)60);
lcd.setCursor(5,2);
lcd.print((char)60);
delay(1000); }
blue=digitalRead(bluepin);
if(blue==HIGH)
{ lcd.setCursor(0,2);
lcd.print("06");
lcd.setCursor(4,2);
lcd.print((char)60);
lcd.setCursor(5,2);
lcd.print((char)60);
delay(1000); }
black=digitalRead(blackpin);
if(black==HIGH)
{lcd.setCursor(0,2);
lcd.print("07");
lcd.setCursor(4,2);
lcd.print((char)60);
lcd.setCursor(5,2);
lcd.print((char)60);
delay(1000); }
r=digitalRead(rpin);
if(r==HIGH)
{ lcd.setCursor(0,2);
lcd.print("08");
lcd.setCursor(4,2);
lcd.print((char)62);
lcd.setCursor(5,2);
lcd.print((char)62);
delay(1000); }
y=digitalRead(ypin);
if(y==HIGH)
{lcd.setCursor(0,2);
lcd.print("09");
lcd.setCursor(4,2);
lcd.print((char)62);
lcd.setCursor(5,2);
lcd.print((char)62);
delay(1000); }
g=digitalRead(gpin);
if(g==HIGH)
{lcd.setCursor(0,2);
lcd.print("10");
lcd.setCursor(4,2);
lcd.print((char)62);
lcd.setCursor(5,2);
lcd.print((char)62);
delay(1000); }
blu=digitalRead(blupin);
if(blu==HIGH)
{ lcd.setCursor(0,2);
lcd.setCursor(4,2);
lcd.print((char)62);
lcd.setCursor(5,2);
lcd.print((char)62);
delay(1000); }
bla=digitalRead(blapin);
if(bla==HIGH)
{lcd.setCursor(0,2);
lcd.print("07");
lcd.setCursor(4,2);
lcd.print((char)62);
lcd.setCursor(5,2);
lcd.print((char)62);
delay(1000); }
lcd.setCursor(0,2);
lcd.print("00");
lcd.setCursor(4,2);
lcd.print("00");
}
二、红外遥控靶机模块
原理就是用红外遥控器控制四个舵机,用遥控器上的按键设置不同的模式,来控制舵机的转向,舵机上装上靶子,也就是控制靶子的起、倒。
图片
代码
#include <Servo.h>//调用Servo函数控制舵机
#include <IRremote.h>//调用IRremote函数实现红外遥控
Servo myservo1;//定义舵机变量名
Servo myservo2;//定义舵机变量名
int inpin=7;//定义数字7 接口
int val;//定义变量val
int i,j;
int RECV_PIN=11;
long on1 = 0x00FFA25D;
long off1 = 0x00FFE01F;
long on2 = 0x00FF629D;
long off2 = 0x00FFA857;
long on3 = 0x00FFE21D;
long off3 = 0x00FF906F;
long on4 = 0x00FF22DD;
long off4 = 0x00FF6897;
long on5 = 0x00FF02FD;
long off5 = 0x00FF9867;
long on6 = 0x00FFC23D;
long off6 = 0x00FFB047;
IRrecv irrecv(RECV_PIN);
decode_results results;
// Dumps out the decode_results structure.
// Call this after IRrecv::decode()
// void * to work around compiler issue
//void dump(void *v) {
// decode_results *results = (decode_results *)v
void dump(decode_results *results) {
int count = results->rawlen;
if (results->decode_type == UNKNOWN)
{
Serial.println("Could not decode message");
}
else
{
if (results->decode_type == NEC)
{
Serial.print("Decoded NEC: ");
}
else if (results->decode_type == SONY)
{
Serial.print("Decoded SONY: ");
}
else if (results->decode_type == RC5)
{
Serial.print("Decoded RC5: ");
}
else if (results->decode_type == RC6)
{
Serial.print("Decoded RC6: ");
}
Serial.print(results->value, HEX);
Serial.print(" (");
Serial.print(results->bits, DEC);
Serial.println(" bits)");
}
Serial.print("Raw (");
Serial.print(count, DEC);
Serial.print("): ");
for (int i = 0; i < count; i++)
{
if ((i % 2) == 1) {
Serial.print(results->rawbuf*USECPERTICK, DEC);
}
else
{
Serial.print(-(int)results->rawbuf*USECPERTICK, DEC);
}
Serial.print(" ");
}
Serial.println("");
}
void setup()
{
pinMode(RECV_PIN, INPUT);
myservo1.attach(9);//定义舵机接口9,接两个舵机,同时控制两个舵机
myservo2.attach(10);//定义舵机接口10,接两个舵机,同时控制两个舵机
pinMode(inpin,INPUT);//定义按键接口为输入接口
irrecv.enableIRIn(); // Start the receiver
}
int on = 0;
unsigned long last = millis();
void loop()
{
if (irrecv.decode(&results))
{
// If it's been at least 1/4 second since the last
// IR received, toggle the relay
if (millis() - last > 250)
{
on = !on;
// digitalWrite(8, on ? HIGH : LOW);
digitalWrite(13, on ? HIGH : LOW);
dump(&results);
}
}
if (results.value == on1 )
{myservo1.write(90);//设置舵机1、2起
}
if (results.value == off1 )
{
myservo2.write(90);//设置舵机3、4起
}
if (results.value == on2 )
{myservo1.write(0);//设置舵机1、2倒
}
if (results.value == off2 )
{
myservo2.write(0);//设置舵机3、4倒
}
}
视频还没来得及上传,传好了把链接发上来。
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|