主程序:
代码:
#include <Servo.h>volatile int 按钮;
volatile int 充电继电器;
volatile int 发射继电器;
volatile int 充电继电器状态;
volatile int 发射继电器状态;
volatile int 舵机1角度;
volatile int 舵机2角度;
Servo servo_6;
Servo servo_7;void 调试() {Serial.print(String("A4:") + String(analogRead(A4)));Serial.print(String("A5:") + String(analogRead(A5)));Serial.println(String("SW:") + String(analogRead(A3)));
}void 舵机控制() {if (analogRead(A4) <= 300) {delay(20);if (analogRead(A4) <= 300) {舵机1角度 = 舵机1角度 - 2;if (舵机1角度 <= 0) {舵机1角度 = 0;}servo_6.write(舵机1角度);delay(10);}}if (analogRead(A4) >= 700) {delay(20);if (analogRead(A4) >= 700) {舵机1角度 = 舵机1角度 + 2;if (舵机1角度 >= 140) {舵机1角度 = 140;}servo_6.write(舵机1角度);delay(10);}}if (analogRead(A5) <= 300) {delay(20);if (analogRead(A5) <= 300) {舵机2角度 = 舵机2角度 - 2;if (舵机2角度 <= 0) {舵机2角度 = 0;}servo_7.write(舵机2角度);delay(10);}}if (analogRead(A5) >= 700) {delay(20);if (analogRead(A5) >= 700) {舵机2角度 = 舵机2角度 + 2;if (舵机2角度 >= 160) {舵机2角度 = 160;}servo_7.write(舵机2角度);delay(10);}}
}boolean mixly_digitalRead(uint8_t pin) {pinMode(pin, INPUT);boolean _return = digitalRead(pin);pinMode(pin, OUTPUT);return _return;
}void 继电器控制() {if (mixly_digitalRead(按钮) == LOW) {delay(50);if (mixly_digitalRead(按钮) == LOW) {if (充电继电器状态 == 0) {充电继电器状态 = 1;pinMode(充电继电器, OUTPUT);digitalWrite(充电继电器,HIGH);发射继电器状态 = 0;pinMode(发射继电器, OUTPUT);digitalWrite(发射继电器,LOW);Serial.println(String("充电开,") + String("发射关"));while (mixly_digitalRead(按钮) == LOW) {}} else if (充电继电器状态 == 1) {充电继电器状态 = 2;pinMode(充电继电器, OUTPUT);digitalWrite(充电继电器,LOW);发射继电器状态 = HIGH;pinMode(发射继电器, OUTPUT);digitalWrite(发射继电器,HIGH);while (mixly_digitalRead(按钮) == LOW) {}Serial.println(String("充电关,") + String("发射开"));}} else if (充电继电器状态 == 2) {充电继电器状态 = 0;pinMode(充电继电器, OUTPUT);digitalWrite(充电继电器,LOW);发射继电器状态 = HIGH;pinMode(发射继电器, OUTPUT);digitalWrite(发射继电器,LOW);while (mixly_digitalRead(按钮) == LOW) {}Serial.println(String("充电关,") + String("充电关"));}}
}void setup(){按钮 = 2;充电继电器 = 13;发射继电器 = 12;充电继电器状态 = 0;发射继电器状态 = 0;舵机1角度 = 90;舵机2角度 = 90;servo_6.attach(6);servo_7.attach(7);pinMode(按钮, INPUT);充电继电器状态 = 0;发射继电器状态 = 0;pinMode(充电继电器, OUTPUT);digitalWrite(充电继电器,LOW);pinMode(发射继电器, OUTPUT);digitalWrite(发射继电器,LOW);servo_6.write(舵机1角度);delay(0);servo_7.write(舵机2角度);delay(0);Serial.begin(9600);
}void loop(){继电器控制();舵机控制();}