Hi,Could anyone help with a code issue. I am trying to get a random activation of a PIR which then operates a servo. Something like a 20% activation ratio. The servo will then do a small sweep once. Then wait for the next random activation. I have the PIR setting the sweep function OK but cannot seem to add the random function successfully. Any help would be appreciated.
Code so far is attached.
Thanks
Andy
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
int alarmPin = 0;
int alarmValue = 0;
void setup () {
Serial.begin (9600);
myservo.attach(7); // attaches the servo on pin 9 to the servo object
pinMode(alarmPin, INPUT);
delay (2000); // it takes the sensor 2 seconds to scan the area around it before it can detect infrared presence.
}
void loop (){
alarmValue = analogRead(alarmPin);
if (alarmValue < 100){
// cycle through every angle (rotate the servo)
for (pos = 90; pos < 110; pos += 1) // goes from 90 degrees to 110 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 110; pos>=90; pos-=1) // goes from 110 degrees to 90 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
delay(9000);
}
When you post code please use the code tags (use the # button) ...
I am trying to get a random activation of a PIR which then operates a servo. Something like a 20% activation ratio.
Don't know what you exactly mean by this
void loop ()
{
...
// wait a random time
long t = random(20) * 1000;
delay(t);
}
or
#define CHANCE 20
...
void loop ()
{
alarmValue = analogRead(alarmPin);
int x = random(100);
if (alarmValue < 100 && x < CHANCE)
{
// cycle through every angle (rotate the servo)
...
Thanks for the replies.
What I mean by activation is when the PIR goes low or someone moves in front of it it will only operate the servo randomly, not every time. I have tried the randomSeed function but it just always seems to move the servo every time there is movement in front of the PIR. I am thinking it is something simple but cannot figure it out. I am new to this and am still learning so might be doing something wrong.
This is what I tried with the random code.
Thanks
A
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int pos = 0; // variable to store the servo position
int alarmPin = 0;
int alarmValue = 0;
long randomNumber;
void setup () {
Serial.begin (9600);
myservo.attach(7); // attaches the servo on pin 7 to the servo object
pinMode(alarmPin, INPUT);
delay (2000); // it takes the sensor 2 seconds to scan the area around it before it can detect infrared presence.
randomSeed (analogRead (1)); // randomize
}
void loop (){
alarmValue = analogRead(alarmPin);
randomNumber=random(10);
if (alarmValue < 100){
// cycle through every angle (rotate the servo)
for (pos = 90; pos < 110; pos += 1) // goes from 90 degrees to 110 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 110; pos>=90; pos-=1) // goes from 110 degrees to 90 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
delay(9000);
}
}
You're not using the random number for anything in your code.
Try this, it should trigger ~half the time:
void loop (){
alarmValue = analogRead(alarmPin);
randomNumber=random(10);
if (alarmValue < 100 && randomNumber<5){
// cycle through every angle (rotate the servo)
for (pos = 90; pos < 110; pos += 1) // goes from 90 degrees to 110 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 110; pos>=90; pos-=1) // goes from 110 degrees to 90 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
delay(9000);
}
}