Hi to all , I bought a project ,was promised the code but the file was empty when I downloaded it.
(already tried contacting him and ask to supply the code ,had no luck)
I'm making a aromatic ball launcher for our very hyper border collie.
Components used:
2x brushless motors
2x esc
1xPIR sensor
1x servo motor
1x LED
the idea is when the PIR sensor detect a signal a LED turns on, the two motors needs to spin @ 100% power for 1 second then the servo motor needs to turn anti clockwise 90 degrees to release the tennis ball ,the two motors keeps spinning for a other 2 seconds before they powers down then 8 seconds delay before the PIR sensor looks for a signal again.
below is my crippled code.
#include <Servo.h>
Servo esc1; // Create a servo object for the first ESC
Servo esc2; // Create a servo object for the second ESC
const int servoPin = 5; // Replace with the actual pin for the servo
Servo myservo;
void setup() {
esc1.attach(9); // Attach the first ESC to pin 9
esc2.attach(10); // Attach the second ESC to pin 10
esc1.writeMicroseconds(1000); // Initialize the first ESC with 0% throttle
esc2.writeMicroseconds(1000); // Initialize the second ESC with 0% throttle
pinMode(2, INPUT);//define arduino pin
pinMode(3, OUTPUT);//define arduino pin
Serial.begin(9600);//enable serial monitor
delay(5000); // Delay for 5 seconds
myservo.attach(5);
//servo position to 0 degrees
myservo.write(0);
}
void loop() {
bool value = digitalRead(2);//get value and save it boolean veriable
if (value == 1) { //check condition
Serial.println("ON");//print serial monitor ON
digitalWrite(3,HIGH);//LED on
esc1.writeMicroseconds(1900); // Start motor 1 at 90%
esc2.writeMicroseconds(1900); // Start motor 2 at 90%
delay(1500); // Run motors for 1.5 seconds
myservo.write(-90); // Rotate the servo to 90 degrees
delay(2000); // Run motors for 2 seconds
esc1.writeMicroseconds(1000); // Stop motor 1
esc2.writeMicroseconds(1000); // Stop motor 2
myservo.write(0); // Reset servo position to 0 degrees
delay(8000); // Wait for 8 seconds
} else {
Serial.println("OFF");//print serial monitor OFF
digitalWrite(3,LOW);//LED off
esc1.writeMicroseconds(1000); // Stop motor 1
esc2.writeMicroseconds(1000); // Stop motor 2
}
}
first issue is, when the PIR sensor is triggered, the 2 motors runs in a loop ,so yes sounds like a good idea to reset the PIR sensor ,would you perhaps know how?
second issue is, the servo motor does not stop it keeps rotating
but for some unknown reason ,it seems like its behaving like it should know.
to trigger a PIR detection in wokwi you click on the PIR sensor object and at the top of the simulation window you have a button to simulate motion
You'll see the servos moving towards various positions based on the timing you defined in your first post and after the pause the system is ready again. (the serial monitor shows some status info too)
of course you need the motors to be correctly powered, not through your Arduino !!
click to see the code
// https://forum.arduino.cc/t/seeking-help-with-first-project-automatic-ball-launcher/1171365/1
/*
when the PIR sensor detect a signal
- a LED turns on
- the two motors needs to spin @ 100% power for 1 second
- then the servo motor needs to turn anti clockwise 90 degrees to release the tennis ball
- the two motors keeps spinning for a other 2 seconds before they powers down
- then 8 seconds delay before the PIR sensor looks for a signal again
*/
enum {IDLE, ACTIVATION, RELEASE, POST_RELEASE, WAITING } state = IDLE;
const unsigned long activationDuration = 1000; // 1s
const unsigned long releaseDuration = 2000; // 2s
const unsigned long minWaitPostTrigger = 8000; // 8s
const int lockBallAngle = 45;
const int releaseBalldAngle = 135;
unsigned long startTime;
#include <Servo.h>
const byte pirPin = 2;
const byte ledPin = 3;
const byte servoPin = 5;
const byte esc1Pin = 9;
const byte esc2Pin = 10;
Servo esc1, esc2, servo;
void stopMotors() {
esc1.writeMicroseconds(1000); // 0% throttle
esc2.writeMicroseconds(1000); // 0% throttle
}
void activateMotors() {
esc1.writeMicroseconds(1900); // 90% throttle
esc2.writeMicroseconds(1900); // 90% throttle
}
void lockBall() {
servo.write(lockBallAngle);
}
void releaseBall() {
servo.write(releaseBalldAngle);
}
void ledOn() {
digitalWrite(ledPin, HIGH);
}
void ledOff() {
digitalWrite(ledPin, LOW);
}
bool motionDetected() {
return digitalRead(pirPin) == HIGH;
}
void setup() {
stopMotors();
lockBall();
pinMode(ledPin, OUTPUT);
pinMode(pirPin, INPUT);
esc1.attach(esc1Pin);
esc2.attach(esc2Pin);
servo.attach(servoPin);
Serial.begin(115200);
}
void loop() {
switch (state) {
case IDLE:
if (motionDetected()) {
Serial.println(F("MOVEMENT DETECTED"));
Serial.println(F("SYSTEM ACTIVATION"));
ledOn();
activateMotors();
startTime = millis();
state = ACTIVATION;
}
break;
case ACTIVATION:
if (millis() - startTime >= activationDuration) {
Serial.println(F("BALL RELEASED"));
releaseBall();
startTime = millis();
state = RELEASE;
}
break;
case RELEASE:
if (millis() - startTime >= releaseDuration) {
Serial.println(F("POST RELEASE PHASE ENDED. SYSTEM DEACTIVATION"));
stopMotors();
lockBall();
startTime = millis();
state = POST_RELEASE;
}
break;
case POST_RELEASE:
if (millis() - startTime >= minWaitPostTrigger) {
Serial.println(F("PAUSE ENDED, WAITING FOR PIR TO BECOME IDLE."));
state = WAITING;
}
break;
case WAITING:
if (not motionDetected()) {
Serial.println(F("PIR IS IDLE, READY AGAIN."));
ledOff();
state = IDLE;
}
break;
}
}