用Arduino做个鱼竿
钓鱼养生。。。
(搭配尼尔机械纪元使用
运行效果:
尼尔: 机械纪元 之 钓鱼外设
记录一下制作过程:
材料:
Arduino UNO, 杜邦线,伺服器,面包板,LED,1k ohm电阻,超声波距离传感器
步骤:
-
超声波传感器连线:
Vcc to 13
Trig to 12
Echo to 11
Gnd to 10 -
Servo 连线
Orange to 9
Red to 5v
Brown to GND -
LED 连线
绿灯
+ to 3
- to GND
橘灯
+ to 2
- to 3
(记得给每个LED串联一个1k ohm 电阻 (棕 黑 红 金)
- 将超声波距离传感器放到支架里
(这样当鱼竿被放到支架上时,距离小于某个值Dist
当鱼竿被拿起时,距离大于Dist, 程序检测到鱼竿被拿起
4. 将LED电路连接好后,固定到鱼竿上,起到状态指示的作用(和让结构看起来更复杂更高级一点
(鱼竿放在底座上时绿灯亮,鱼竿拿起时橘等亮
-
将伺服器固定到enter键旁(抛勾/收杆
-
程序:
整体思路:
初始鱼竿放在底座上,绿灯亮
当鱼竿抬起时,超声波距离传感器检测到鱼竿被移走,敲一下回车 (抛勾
鱼竿不在底座上时,橘灯亮鱼竿被放回,橘灯熄灭,绿灯亮
此时为等待鱼上钩中看到鱼上钩时
再次抬起鱼竿,此时超声波距离传感器再次检测到鱼竿被移走,敲一下回车 (收竿
鱼竿被抬起,所以橘灯亮注意:
当鱼竿放在底座上时,程序每10ms检测一次鱼竿状态,确保能在鱼竿被抬起时迅速反应
当鱼竿离开底座时,程序每500ms检测一次鱼竿状态,这样可以避免缓慢放鱼竿回底座时误触发回车
// A code block
//for servo
#include <Servo.h> //include the servo library for this program
Servo myservo;
//for sonor
#define VCC_PIN 13
#define TRIGGER_PIN 12 // sonar trigger pin will be attached to Arduino pin 12
#define ECHO_PIN 11 // sonar echo pint will be attached to Arduino pin 11
#define GROUND_PIN 10 //
#define MAX_DISTANCE 200 // maximum distance set to 200 cm
#define LED_O 2
#define LED_G 3
// defines variables
long duration;
int distance;
void setup() {
//sonor setup
Serial. begin(9600); // set data transmission rate to communicate with computer
pinMode(ECHO_PIN, INPUT) ;
pinMode(TRIGGER_PIN, OUTPUT) ;
pinMode(GROUND_PIN, OUTPUT); // tell pin 10 it is going to be an output
pinMode(VCC_PIN, OUTPUT); // tell pin 13 it is going to be an output
digitalWrite(GROUND_PIN,LOW); // tell pin 10 to output LOW (OV, or ground)
digitalWrite(VCC_PIN, HIGH) ; // tell pin 13 to output HIGH (+5V)
//motor setup
Serial. begin(9600); //set data transmission rate to communicate with computer
pinMode(8,INPUT_PULLUP); //pin 8 forced to HIGH when there is no external input
myservo.attach(9); //attaches the servo on pin 9 to the servo object
myservo.write(0);
pinMode(LED_O, OUTPUT);
pinMode(LED_G, OUTPUT);
}
void loop() {
//get distance
digitalWrite(TRIGGER_PIN, LOW); // Clears the trigPin
delayMicroseconds(2);
digitalWrite(TRIGGER_PIN, HIGH); // Sets the trigPin on HIGH state for 10 micro seconds
delayMicroseconds(20);
digitalWrite(TRIGGER_PIN, LOW);
duration = pulseIn(ECHO_PIN, HIGH);
distance= duration*0.034/2; // Calculating the distance
//print the distance measrued in the while loop
Serial.print("Distance: ") ; // Prints the distance on the Serial Monitor
Serial.println(distance);
Serial.println("");
// if the distance measurement becomes extremely high, it is likely an error.
// Default to a maximum value of MAX_DISTANCE to identify this condition.
if (distance > MAX_DISTANCE)
{distance = MAX_DISTANCE ;}
if (distance > 10)
{
myservo.write(25);
delay(300);
myservo.write(0);
delay(500);
while (distance >10){
digitalWrite(LED_O, HIGH);
digitalWrite(LED_G, LOW);
//get distance
digitalWrite(TRIGGER_PIN, LOW); // Clears the trigPin
delayMicroseconds(2);
digitalWrite(TRIGGER_PIN, HIGH); // Sets the trigPin on HIGH state for 10 micro seconds
delayMicroseconds(20);
digitalWrite(TRIGGER_PIN, LOW);
duration = pulseIn(ECHO_PIN, HIGH);
distance= duration*0.034/2;
//print the distance measrued in the while loop
Serial.print("Distance in while loop: ") ; // Prints the distance on the Serial Monitor
Serial.println(distance);
Serial.println("");
delay(500);
}
}
else{
digitalWrite(LED_G, HIGH);
digitalWrite(LED_O, LOW);
}
}