git: https://gitee.com/swordfly/arduino_library.git
HRS1H-S-DC5V的原电路图:
上图是arduino中提供的HRS1H-S-DC5V继电器电路图,经过测试,个人觉得应该是下面的:
该型号内部应该是3和4不连接,6和4内部是连接的(个人推测)。
/** * author wjf * date 2017/12/30 14:23 * 新版测试品代码备份 * 每个长注释中的代码,都可以单独的模块运用 */ ////////////////引入头文件////////////////////////////////////////////////// #include <IRremote.h> // 红外 #include <Servo.h> //舵机 //通用字符串库 #include <stdio.h> #include <string.h> #include <stdlib.h> //LCD #include <LiquidCrystal_I2C.h> //LCD #include <Wire.h> #include <LCD.h> /** * 定义LCD对象, * 第一个参数0x20需要先扫描I2C地址扫描代码地址: * http://www.cnblogs.com/SATinnovation/p/8047240.html * 后面的暂时不知道什么意思,可以这么直接用 */ LiquidCrystal_I2C lcd(0x20,2,1,0,4,5,6,7); //////////////////////定义LCD方法//////////////////////////////////////////////////////////// void initLCD(){ lcd.begin (16,2); // for 16 x 2 LCD module lcd.setBacklightPin(3,POSITIVE); lcd.setBacklight(HIGH); } void wjf_setLcd(char *str){//设置LCD显示的字符串 lcd.home (); // set cursor to 0,0 lcd.print(str); lcd.setCursor (0,1); // go to start of 2nd line //lcd.print(millis()); //delay(1000); lcd.setBacklight(LOW); // Backlight off delay(250); lcd.setBacklight(HIGH); // Backlight on delay(1000); } /////////////机车移动电机/////////////////////////////////// //电机PIN const int IN1=5; const int IN2=4; const int ENA=6; const int IN3=8; const int IN4=7; const int ENB=9; //电机速度 int speed = 100; void Motor1_Forward(int Speed) //电机1正转,Speed值越大,电机转动越快 { digitalWrite(IN1,HIGH); digitalWrite(IN2,LOW); analogWrite(ENA,Speed); } void Motor1_Backward(int Speed) //电机1反转,Speed值越大,电机转动越快 { digitalWrite(IN1,LOW); digitalWrite(IN2,HIGH); analogWrite(ENA,Speed); } void Motor1_Brake() //电机1刹车 { digitalWrite(IN1,LOW); digitalWrite(IN2,LOW); } void Motor2_Forward(int Speed) //电机2正转,Speed值越大,电机转动越快 { digitalWrite(IN3,HIGH); digitalWrite(IN4,LOW); analogWrite(ENB,Speed); } void Motor2_Backward(int Speed) //电机2反转,Speed值越大,电机转动越快 { digitalWrite(IN3,LOW); digitalWrite(IN4,HIGH); analogWrite(ENB,Speed); } void Motor2_Brake() { digitalWrite(IN3,LOW); digitalWrite(IN4,LOW); } //////////千水星电机///////////////////////////////////// const int pin2 = 2; const int pin3 = 3; const int pin10 = 10; const int pin11 = 11; //初始化千水星 void qsx_init(){ pinMode(pin2,OUTPUT); pinMode(pin3,OUTPUT); pinMode(pin10,OUTPUT); pinMode(pin11,OUTPUT); } //电机1前进 void qsx_motor_1_forward(){ digitalWrite(pin2,HIGH); //给高电平 digitalWrite(pin3,LOW); //给低电平 } //电机2前进 void qsx_motor_2_forward(){ digitalWrite(pin11,HIGH); //给高电平 digitalWrite(pin10,LOW); //给低电平 } //电机1后退 void qsx_motor_1_backward(){ digitalWrite(pin2,LOW); digitalWrite(pin3,HIGH); } //电机2后退 void qsx_motor_2_backward(){ digitalWrite(pin11,LOW); digitalWrite(pin10,HIGH); } //电机1停止 void qsx_motor_1_stop(){ digitalWrite(pin2,LOW); digitalWrite(pin3,LOW); } //电机2停止 void qsx_motor_2_stop(){ digitalWrite(pin11,LOW); digitalWrite(pin10,LOW); } ///////////定义左右舵机//////////////////////////////// //左右转舵机 const int IN28=28; Servo myservo; int point = 1;//0,1,2 int pos = 90; void getLeftAndRight_left() { if(point == 2){ getLeftAndRight_center(); } for (pos = 30; pos < 90; pos += 1) { myservo.write(pos); delay(15); } point = 0; } void getLeftAndRight_center() { Serial.println(110); Serial.println(pos); if (pos == 90) { for (pos = 90; pos > 30; pos -= 1) { myservo.write(pos); delay(15); } }else if(pos == -30){ for (pos = -30; pos < 30; pos += 1) { myservo.write(pos); delay(15); } } point = 1; } void getLeftAndRight_right() { if(point == 0){ getLeftAndRight_center(); } for (pos = 30; pos > -30; pos -= 1) { myservo.write(pos); delay(15); } point = 2; } void initLeftAndRight() { myservo.attach(IN28); myservo.write(pos); } ///////////定义上下舵机//////////////////////////////// //上下转舵机 const int IN22=22; Servo myservo2; int pos2 = 90; void getLeftAndRight_left2() { for (pos2 = 30; pos2 < 90; pos2 += 1) { myservo2.write(pos2); delay(15); } } void getLeftAndRight_center2() { Serial.println(110); Serial.println(pos2); if (pos2 == 90) { for (pos2 = 90; pos2 > 30; pos2 -= 1) { myservo2.write(pos2); delay(15); } }else if(pos2 == -30){ for (pos2 = -30; pos2 < 30; pos2 += 1) { myservo2.write(pos2); delay(15); } } } void getLeftAndRight_right2() { for (pos2 = 30; pos2 > -30; pos2 -= 1) { myservo2.write(pos2); delay(15); } } void initLeftAndRight2() { myservo2.attach(IN22); myservo2.write(pos2); } //红外接收 int RECV_PIN = 23; IRrecv irrecv(RECV_PIN); decode_results results; long control[7][3] = {//遥控器矫正数字 {16580863, 16613503, 16597183}, {16589023, 16621663, 16605343}, {16584943, 16617583, 16601263}, {16593103, 16625743, 16609423}, {16582903, 16615543, 16599223}, {16591063, 16623703, 16607383}, {16586983, 16619623, 16603303} }; //初始化电机PIN void initMotor(){ pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT); pinMode(ENA, OUTPUT); pinMode(IN4, OUTPUT); pinMode(IN3, OUTPUT); pinMode(ENB, OUTPUT); } //初始化红外接收 void initIR(){ irrecv.enableIRIn(); } ////////////////定义超声波////////////////////////////////////////////// const int echoPin = 26;//回声针脚 const int trigPin = 24;//触发针脚 float distance;//存放距离 //初始化超声波针脚 void initUltrasonic(){ pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); } //获取距离 void getDistance(){ // 产生一个10us的高脉冲去触发trigPin digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // 检测脉冲宽度,并计算出距离 distance = pulseIn(echoPin, HIGH) / 58.00; Serial.print(distance); Serial.print("cm"); } void setup() { pinMode(13, OUTPUT); digitalWrite(13, LOW);//打开的时候LOW改成HIGHT Serial.begin(9600); //初始电机 initMotor(); //初始化红外接收 initIR(); //初始化左右转舵机 initLeftAndRight(); //初始化上下转舵机 initLeftAndRight2(); //初始化LCD initLCD(); wjf_setLcd("Hello world"); //初始化千水星 qsx_init(); } void loop() { if (irrecv.decode(&results)) { Serial.println(results.value, HEX);//以16进制换行输出接收代码 if (results.value == 4294967295) { //long click } else { if (results.value == control[0][0]) { Motor1_Forward(speed); } else if (results.value == control[0][1]) {//上 //同进 Motor1_Forward(speed); Motor2_Backward(speed); } else if (results.value == control[0][2]) { Motor2_Backward(speed); } else if (results.value == control[1][1]) {//中 Motor2_Brake(); Motor1_Brake(); } else if (results.value == control[1][2]) {//右 } else if (results.value == control[2][0]) { Motor1_Backward(speed); } else if (results.value == control[1][0]) {//左 } else if (results.value == control[2][1]) {//下 //同退 Motor1_Backward(speed); Motor2_Forward(speed); } else if (results.value == control[2][2]) { Motor2_Forward(speed); } else if (results.value == control[3][0]) {//0 //千水星1电机前进 qsx_motor_2_forward(); Serial.println(0); } else if (results.value == control[3][1]) { //千水星1,2电机停止 qsx_motor_1_stop(); qsx_motor_2_stop(); } else if (results.value == control[3][2]) {//st qsx_motor_1_forward(); Serial.println(2); } else if (results.value == control[4][0]) {//1 } else if (results.value == control[4][1]) {//2 } else if (results.value == control[4][2]) {//3 } else if (results.value == control[5][0]) {//4 } else if (results.value == control[5][1]) {//5 } else if (results.value == control[5][2]) {//6 } else if (results.value == control[6][0]) {//7 getDistance();//获取超声波测得的距离 } else if (results.value == control[6][1]) {//8 getLeftAndRight_center(); } else if (results.value == control[6][2]) {//9 getLeftAndRight_left(); } } irrecv.resume(); // 接收下一个值 } delay(100); }