stick robot
This commit is contained in:
@@ -0,0 +1,8 @@
|
|||||||
|
|
||||||
|
|
||||||
|
#idea
|
||||||
|
.idea
|
||||||
|
*.iml
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,108 @@
|
|||||||
|
/*
|
||||||
|
Sweep-arm robot that strike obstacles with a stick.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <Servo.h>
|
||||||
|
|
||||||
|
// ===== Pins =====
|
||||||
|
#define SERVO1_PIN D5 // GPIO14
|
||||||
|
#define SERVO2_PIN D6 // GPIO12
|
||||||
|
|
||||||
|
#define TRIG_PIN D1 // GPIO5
|
||||||
|
#define ECHO_PIN D2 // GPIO4
|
||||||
|
|
||||||
|
Servo servo1;
|
||||||
|
Servo servo2;
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
Serial.begin(115200);
|
||||||
|
|
||||||
|
// HC-SR04
|
||||||
|
pinMode(TRIG_PIN, OUTPUT);
|
||||||
|
pinMode(ECHO_PIN, INPUT);
|
||||||
|
|
||||||
|
// Servos
|
||||||
|
servo1.attach(SERVO1_PIN, 500, 2500);
|
||||||
|
servo2.attach(SERVO2_PIN, 500, 2500);
|
||||||
|
|
||||||
|
// Center servos
|
||||||
|
servo2.write(90);
|
||||||
|
servo2.detach();
|
||||||
|
delay(5000); //wait 5 sec to allow upload without moving
|
||||||
|
servo1.write(90);
|
||||||
|
|
||||||
|
Serial.println("Robot arm initialized");
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
static int angle = 0;
|
||||||
|
static int direction = 1;
|
||||||
|
|
||||||
|
static unsigned long lastServoMove = 0;
|
||||||
|
static unsigned long lastMeasure = 0;
|
||||||
|
|
||||||
|
// Move servo every 20ms
|
||||||
|
if (millis() - lastServoMove >= 20) {
|
||||||
|
lastServoMove = millis();
|
||||||
|
|
||||||
|
servo1.write(angle);
|
||||||
|
|
||||||
|
angle += direction;
|
||||||
|
|
||||||
|
if (angle >= 180) {
|
||||||
|
angle = 180;
|
||||||
|
direction = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (angle <= 0) {
|
||||||
|
angle = 0;
|
||||||
|
direction = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Measure distance every 50ms
|
||||||
|
if (millis() - lastMeasure >= 50) {
|
||||||
|
lastMeasure = millis();
|
||||||
|
|
||||||
|
long duration;
|
||||||
|
float distance;
|
||||||
|
|
||||||
|
digitalWrite(TRIG_PIN, LOW);
|
||||||
|
delayMicroseconds(2);
|
||||||
|
|
||||||
|
digitalWrite(TRIG_PIN, HIGH);
|
||||||
|
delayMicroseconds(10);
|
||||||
|
|
||||||
|
digitalWrite(TRIG_PIN, LOW);
|
||||||
|
|
||||||
|
duration = pulseIn(ECHO_PIN, HIGH, 30000);
|
||||||
|
|
||||||
|
if (duration > 0) {
|
||||||
|
distance = duration * 0.0343 / 2;
|
||||||
|
|
||||||
|
Serial.print("Angle: ");
|
||||||
|
Serial.print(angle);
|
||||||
|
Serial.print(" Distance: ");
|
||||||
|
Serial.println(distance);
|
||||||
|
|
||||||
|
if (distance >= 14 && distance <= 22) {
|
||||||
|
Serial.println("TARGET!");
|
||||||
|
|
||||||
|
//adjust position to compensate stick offset
|
||||||
|
angle -=8;
|
||||||
|
servo1.write(angle);
|
||||||
|
|
||||||
|
// hit with stick
|
||||||
|
servo2.attach(SERVO2_PIN, 500, 2500);
|
||||||
|
delay(100);
|
||||||
|
servo2.write(165);
|
||||||
|
delay(1000);
|
||||||
|
servo2.write(90);
|
||||||
|
delay(3000);
|
||||||
|
servo2.detach();
|
||||||
|
delay(100);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user