Can you help me with this code?

Hello can you help me with this code? I am trying to make my servo stay in position 2 stopCar.write(0); for longer. Thanks :slight_smile:

#include <Servo.h>

int servoPin = 4;
Servo stopCar;

int pirPin = 2;
int val = 0;

void setup() {
  // setup code, will run once:
  pinMode(pirPin, INPUT);
  stopCar.attach(servoPin);
  stopCar.write(50);
}

void loop() {
  // main code, runs reapetedly:

  val = digitalRead(pirPin);
  if(val == HIGH) {
    stopCar.write(50); // position 1
  }
  else{
    stopCar.write(0); //position 2
  }

  delay(250);
}

Please explain in more detail the problem that you have with this code.

What's the condition for the motor to stay in position 2?

im trying to make my servo stay at 0 degrees for longer

And what is your problem in it?
According to your code, the servo still at 0 degrees while the pirPin is LOW

its not really a problem, but I need to make my servo pause between position 1 and position 2

What is the position "between pos 1 and pos 2" ? Between 50 and 0 degrees? - something like 25?

my problem is I don't know how to make pirPin LOW for longer.
pirPin is connected to a PIR motion sensor

there is no in between so it is 50

Try to add a delay in position 2, something like that:

1 Like

I tried that it worked but it also delayed the the time it took for the servo to react by 1 second

sorry I have to go i'l be back in 11 hours

no, that's not true.

You probably changed the delay at the end of the loop(), where you have now the delay(250) line, make it longer.
And I suggest you add a delay only when the sensor is triggered

Hi @dr_scar,

in short:

Can you please post:

  • Is your PIR sensor signalizing "Motion Detected" by HIGH or by LOW signal?
  • Does your PIR Sensor have a single and multiple trigger mode? If yes: How do you use it?
  • Does your PIR Sensor have a delay time adjustment? If yes: How long is the delay time?
  • Do you want to extend the position time when "No Motion" or "Motion Detected" is active?

I have added @b707 's suggestions to your code plus some Serial output:

/*
   Wowki: https://wokwi.com/projects/408184485529159681
   Forum: https://forum.arduino.cc/t/can-you-help-me-with-this-code/1298754

*/

#include <Servo.h>

const byte servoPin = 4;
Servo stopCar;

const byte pirPin = 2;
byte val = 0;
byte oldVal = 255;

void setup() {
  Serial.begin(115200);
  pinMode(pirPin, INPUT);
  stopCar.attach(servoPin);
  stopCar.write(50);
}

void loop() {
  // main code, runs reapetedly:

  val = digitalRead(pirPin);
  if (val != oldVal) {
    oldVal = val;
    if (val == HIGH) {
      Serial.println("Position 1");
      stopCar.write(50); // position 1
    } else {
      Serial.println("Position 2");
      stopCar.write( 0); // position 2
      delay(10000);
      Serial.println("Ready");

    }
  }
}

You can test it on Wokwi

https://wokwi.com/projects/408184485529159681

If it does not fulfill your expectations answering the questions above may help you and us to find an appropriate solution ...

ec2021

1 Like

Sorry that does not work but I appreciate your effort.

My PIR signals "Motion Detected" on a HIGH signal, do you or any one else no how to extend the duration of ar delay the the servo returning to position 1.

Please explain your "not work" more clearly.

Do you mean you want your code to delay(msecs) after //position 2 ???

I think I got it working the way I wanted, it's sort of like @b707 suggestion but slightly tweaked. I know longer mind the delay as I can make it work IRL.

#include <Servo.h>
#include <Button.h>

int servoPin = 4;
Servo stopCar;

int pirPin = 2;
int val = 0;

void setup() {
  // setup code, will run once:
  pinMode(pirPin, INPUT);
  stopCar.attach(servoPin);
  stopCar.write(50);                // Position 1
}

void loop() {
  // main code, runs reapetedly:

  val = digitalRead(pirPin);
  if(val == HIGH) {
    stopCar.write(50);                // Position 1
  }
  else{
    delay(750);
    stopCar.write(0);                // Position 2
    }
  

  delay(1000);
}

The questions in my last post are based on typical features of Arduino PIR sensors.

If you don't take the trigger mode and delay time into account everything you do in the code is only guesswork ...

You surely get better support if you would provide the information as requested above.

However that's your choice of course!

Good luck!