|
用模拟杆控制舵机和继电器。然后又加了一组红外遥控,但是后来发现距离太短,没什么用,就算是熟悉一下硬件吧。加了两个LED,随着舵机的左右摇摆会有不同的灯亮起。[kenrobot_code]/**
* Copyright(C), 2018-2038, KenRobot.com
* FileName: sketch_mar08a.ino
* Author: 啃萝卜
* Create: 2018/03/10
* Modify: 2018/03/10
*/
#include <IRremote.h>
#include <Servo.h>
IRrecv irRecv_0(A4);
decode_results irRecv_0_results;
int joystick_0_X = A0;
int joystick_0_Y = A2;
int joystick_0_Z = 4;
int led_0 = 3;
int led_1 = 6;
int relay_0 = 12;
Servo servo_0;
void bblink() {
float blight = 0;
for (blight = 0; blight <= 255; blight += 1) {
analogWrite(led_0, blight);
delay(2);
digitalWrite(led_0, LOW);
}
}
void yblink() {
float ylight = 0;
for (ylight = 0; ylight <= 255; ylight += 1) {
analogWrite(led_1, ylight);
delay(2);
digitalWrite(led_1, LOW);
}
}
void Aserv() {
float AservL = 90;
for (AservL = 90; AservL <= 150; AservL += 2) {
servo_0.write(AservL);
delay(20);
}
}
void Bserv() {
float BservR = 90;
for (BservR = 90; BservR >= 30; BservR -= 2) {
servo_0.write(BservR);
delay(20);
}
}
void setup() {
irRecv_0.enableIRIn();
pinMode(joystick_0_Z, INPUT);
pinMode(led_0, OUTPUT);
pinMode(led_1, OUTPUT);
pinMode(relay_0, OUTPUT);
Serial.begin(9600);
servo_0.attach(9);
}
void loop() {
int joyx = analogRead(joystick_0_X);
int joyy = analogRead(joystick_0_Y);
int joyz = digitalRead(4);
float xangle = map(joyx, 0, 1023, 0, 180);
float yangle = map(joyy, 0, 1023, 0, 180);
float light = xangle;
servo_0.write(xangle);
delay(150);
if (yangle > 90) {
digitalWrite(relay_0, LOW);
}
if (joyz == LOW) {
digitalWrite(relay_0, HIGH);
}
if (light >= 95) {
bblink();
}
if (light <= 80) {
yblink();
}
if (irRecv_0.decode( & irRecv_0_results) > 0) {
uint32_t AR = irRecv_0_results.value;
if (AR == 16228447) {
digitalWrite(relay_0, LOW);
}
if (AR == 16224367) {
digitalWrite(relay_0, HIGH);
}
if (AR == 16191727) {
Aserv();
delay(500);
}
if (AR == 16208047) {
Bserv();
delay(500);
}
irRecv_0.resume();
}
}[/kenrobot_code]
|
|