Need Help Now

UNO BOARD
so we need to have a science fair project done by tomorrow. (No comments on last minute.) We’ve been working on it for week and can’t seem to fix all the bugs. So what we’re trying to do is connect a PIR sensor to Arduino and get output to a servo. We want the servo to pull a trigger of a nerf gun to create a turret if you will. However everytime we try to upload the code it will either come up with, COM 3 is already in use and direct us to tools > serial port. As well as a servo hums but never moves. The L led lights up when I put my hand in front of the PIR sensor the L LED (turns off then turns back on when I remove my hand) however it never actually sends a signal to the servo I’ve tried putting it in all the USB ports to stop the COM 3, I’ve gone to the device manager to also stop COM 3 message error but nothing seems to work. Help would be greatly appreciated!

CODE:

#include <Servo.h>



Servo servo1;

int ledPin = 13;
int inputPin = 2;
int pirState = LOW;
int val = 0;

void setup()
  {
    pinMode (1, OUTPUT);
    servo1.attach(9);
    
    pinMode (ledPin, OUTPUT);
    pinMode (inputPin, INPUT);
    Serial.println("Hello?");
  }

void loop()
  { 
    if (pirState == HIGH)
      {
        servo1.writeMicroseconds(1500);
        servo1.writeMicroseconds(9);
        
        loop;
      }
      
    
    val = digitalRead(inputPin);
    if (val == HIGH)
      {
        digitalWrite(ledPin, HIGH);
        delay(150);
        
        if (pirState == LOW)
          {
            Serial.println("Target aquired!");
            pirState = HIGH;
          }
      }else
        {
          digitalWrite(ledPin, LOW);
          delay(300);
          
          if (pirState == HIGH)
            {
              Serial.println("Target lost.");
              pirState = LOW;
            }
        }
  }

SERVO:
Ground to GND
Power to 5V
Signal input from digital pin 9

PIR SENSOR:
Ground to GND
VCC to 3.3V
OUT to Digital Pin 2

I don't understand what you could possibly think we could do to help you without a schematic and your code. Are we psychic ?

Yup.. no schematic. No code. Can't help much.

The way to stop what you call the "COM3 error" is to stop the program that's using it.

Obviously the problems uploading the sketch are unrelated to problems with the behaviour of the sketch, and need to be resolved first.

Unplug the Arduino from the USB socket, open the Tools/Serial Port menu and make a note of what ports are listed there.

Plug the Arduino in. Look at Tools/Serial Port again and note which port is new. Select that one. Make sure the correct board type is selected.

Upload your sketch. If you still get the 'serial port already in use' type errors then look for device management software on the PC that may be scanning serial ports - mobile phone management software sometimes does this sort of thing.

click the MODIFY button in the upper right of the post window. Highlight all you code. click the "#" CODE TAGS button on the toolbar above just to the left of the QUOTE button. click SAVE (at the bottom). When you post code on the forum, please make a habit of using the code tags "#" button.

After verifying the code and getting no errors 5 times and uploading it with no error the servo still vibrates but never actually moves. It just hums with no response to the PIR Sensor

It just hums

Perhaps it has forgotten the words.

What do your debug prints tell you is happening?

If it's not a continuous rotation servo then maybe you try telling it to go to an angle. (use servo.write instead)

    servo1.writeMicroseconds(1500);
    servo1.writeMicroseconds(9);

Send the servo to one end of its travel and before it has time to move send it back. That will make it hum for sure. The program cannot wait for the servo to reach its target position because it has no feedback from the servo.

    loop;Why ?

  pinMode (1, OUTPUT);Why ? Particularly as pin 1 is the Tx pin for Serial comms on a Uno.

I don't think those commands work for a normal Non-continuous rotation servo do they ?

Those commands will probably work well enough to cause the problem @UKHeliBob has spotted. The code needs to allow time for the servos to get somewhere before issuing another command.

The lowest number of microseconds should be in the region of 1000 not 9. And the highest value should be about 2000.

Also, from the first post, it looks like the servo is being powered from the Arduino 5v pin. That can cause the Arduino to reset or brown-out with all sorts of interesting consequences. Give the servo its own power supply with the ground connected to the Arduino ground.

...R

How can you use those commands for a NON-CONTINUOUS rotation servo (0 to 180 degrees) ?

raschemmel: How can you use those commands for a NON-CONTINUOUS rotation servo (0 to 180 degrees) ?

servo.writeMicroseonds(1500); does exactly the same thing as servo.write(90). Actually, it's really the other way around. When you want a servo to move to 90 degrees the pulses must be 1500 microseconds wide.

You can use either system and you can mix them up.

...R

the servo still vibrates but never actually moves. It just hums with no response to the PIR Sensor

Well, the servo1.writeMicroseconds(9) is probably forcing the servo against its internal hard stop, in effect stalling the servo motor. The servo library internally limits the lowest servo.writeMicroseconds() value to something like 544, which may still allow the hard stop to be encountered. You might be able to modify the below code to work with the PIR output.

//zoomkat servo button test 7-30-2011

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
Servo servo1;

void setup()
{
  pinMode(button1, INPUT);
  servo1.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(160);
  }
  else {
    servo1.write(20);
  }
}