From 0d84f6ab9e5ea0a80d605f5af1ca30926315582e Mon Sep 17 00:00:00 2001 From: petrukhnov Date: Thu, 18 Jun 2026 21:38:48 +0100 Subject: [PATCH] stick robot --- .gitignore | 8 ++ README.md | 4 +- .../2026-06-18_hit-with-stick.ino | 108 ++++++++++++++++++ 3 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 src/2026-06-18_hit-with-stick/2026-06-18_hit-with-stick.ino diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..25830a1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ + + +#idea +.idea +*.iml + + + diff --git a/README.md b/README.md index 56a6051..0ead40e 100644 --- a/README.md +++ b/README.md @@ -1 +1,3 @@ -1 \ No newline at end of file +# arduino robots + + diff --git a/src/2026-06-18_hit-with-stick/2026-06-18_hit-with-stick.ino b/src/2026-06-18_hit-with-stick/2026-06-18_hit-with-stick.ino new file mode 100644 index 0000000..10e3218 --- /dev/null +++ b/src/2026-06-18_hit-with-stick/2026-06-18_hit-with-stick.ino @@ -0,0 +1,108 @@ +/* +Sweep-arm robot that strike obstacles with a stick. +*/ + +#include + +// ===== 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); + } + } + } +} \ No newline at end of file