dr_scar
September 5, 2024, 9:41am
1
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
#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);
}
b707
September 5, 2024, 9:43am
2
Please explain in more detail the problem that you have with this code.
fuch99
September 5, 2024, 9:44am
3
What's the condition for the motor to stay in position 2?
dr_scar
September 5, 2024, 9:46am
5
im trying to make my servo stay at 0 degrees for longer
b707
September 5, 2024, 9:47am
6
And what is your problem in it?
According to your code, the servo still at 0 degrees while the pirPin
is LOW
dr_scar
September 5, 2024, 9:48am
7
its not really a problem, but I need to make my servo pause between position 1 and position 2
b707
September 5, 2024, 9:50am
8
What is the position "between pos 1 and pos 2" ? Between 50 and 0 degrees? - something like 25?
dr_scar
September 5, 2024, 9:51am
9
my problem is I don't know how to make pirPin LOW for longer.
pirPin is connected to a PIR motion sensor
dr_scar
September 5, 2024, 9:52am
10
there is no in between so it is 50
b707
September 5, 2024, 9:53am
11
Try to add a delay in position 2, something like that:
1 Like
dr_scar
September 5, 2024, 9:54am
12
I tried that it worked but it also delayed the the time it took for the servo to react by 1 second
dr_scar
September 5, 2024, 9:56am
13
sorry I have to go i'l be back in 11 hours
b707
September 5, 2024, 10:04am
14
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
ec2021
September 5, 2024, 4:45pm
15
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
dr_scar
September 5, 2024, 8:18pm
16
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.
b707
September 5, 2024, 8:20pm
17
dr_scar:
that does not work
Please explain your "not work" more clearly.
sonofcy
September 5, 2024, 8:25pm
18
Do you mean you want your code to delay(msecs) after //position 2 ???
dr_scar
September 5, 2024, 8:35pm
19
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);
}
ec2021
September 5, 2024, 9:02pm
20
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!