I am new to Arduino, and for my first project, Im trying to make a motion sensor. Basically, I have it set up so that whenever the motion sensor is triggered, which is based on if the object close to it is 120 cm away, a LED turns on and a servo motor runs. However, I have a mini water gun that I want to fire with that servo motor. I have linked the model closest to mine I could find. However, my servo motor does not move fast enough for it to trigger the water gun, as it requires a quick push on the trigger as far as it can go to fire. I have a Micro Servo 9g SG90, and as well as everything in the Elegoo Uno R3 Most Complete Starter Kit.
#include <Servo.h>
Servo myServo;
const int trigPin = 7;
const int echoPin = 6;
const int ledPin = 13;
const int servoPin = 5;
float duration, distance;
void setup() {
myServo.attach(servoPin);
myServo.write(330);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(trigPin, LOW);
delay(2);
digitalWrite(trigPin, HIGH);
delay(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
//gets one way in cm
distance = (duration / 29)/2;
Serial.print("Distance: ");
Serial.println(distance);
if(distance > 0 && distance < 120){
digitalWrite(ledPin, HIGH);
servoSpin();
} else {
digitalWrite(ledPin, LOW);
}
delay(250);
}
void servoSpin(){
myServo.write(0);
myServo.write(330);
}
Link to water gun model:
water gun
Anyone got any tips?