My first arduino project

Hello people of the internet! :slight_smile: I recently bought a arduino uno. I had the idea of building a sentry gun and all went well until i got to the programing. I managed to make the servo move the way i wanted but progrssion stopped there. This is the code that 100% works:
#include <Servo.h>

Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 90; // variable to store the servo position

void setup() {
myservo.attach(11); // attaches the servo on pin 11 to the servo object
}

void loop() {
for (pos = 15; pos <= 180; pos += 15) { // goes from 15 degrees to 180 degrees
// in steps of 15 degree
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(200); // waits 200ms for the servo to reach the position
}
for (pos = 180; pos >= 15; pos -= 15) { // goes from 180 degrees to 15 degrees
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(200); // waits 200ms for the servo to reach the position
}
}

I also bought a hc-sr05 sensor. My plan was to make the servo go back and forth 15° at a time (beacuse of the sensor) stop and scan then turn again. And if the sensor noticed anything within 1 meter it would stop and turn on a diod.

So now my shameful question is: Can some one explain the sensor or someone really nice wright some code to help me? :confused:

lyckad_rotation.ino (1.01 KB)

Hmpus345: Hello peapole

This is NOT a good start. Especially as I am no longer as thin as I used to be.

...R

Sorry, I do not know the hc-sr05 sensor so I cannot help you there. It seems to me that you should write a function that causes the hc-sr05 sensor to take a reading (and maybe fires the sentry gun, whatever that is), and there should be a call to the function in each of the for loops immediately after the delay(200) statements.

Yuck. I hate delay(...). millis() can be so much better.

If you want code written for you, you should go to the "Gigs and Collaborations" section of the forum. However, you will not learn nearly as much that way.

I wouldn’t recommend the use of an ultrasonic sensor to measure something small, but here is a quick attachment to help you out.

There is an example, a library and a wiring file for fritzing. The code is meant for 04, not 05 but it should still work.

-eLion101

Ultrasonic.ino (1.49 KB)

NewPing.zip (12.5 KB)

Never mind, you can't upload fritzing files, but the wiring is straightforward depending on code.