hi guys new to this and would appreciate some help
this is my code so far but i do not know hoe to proceed
#include <Servo.h>
Servo vert;
Servo hor;
void setup() {
vert.attach(10);
hor.attach(11);
}
void migrate(Servo &myServo, int newPos) {
int wait=random(1,20); //randomize the wait to make it more interesting
int pos = myServo.read(); //Read the current servo position
if (pos < newPos) {
for (int i=pos; i < newPos; i++) {
myServo.write(i);
delay(wait);
}
} else {
for (int i=pos; i > newPos; i--) {
myServo.write(i);
delay(wait);
}
}
}
void randomPosition() {
int rand=random(10,100); //The range is limited to 60 deg for better cat action
migrate(hor, rand);
rand=random(0,100); //The vertical range is limited to 45 deg also for better cat action.
migrate(vert, rand);
}
void loop() {
randomPosition();
delay(0);
}
[code][/code]
what i would like to do is connect a pir sensor and a day night sensor
the pir when activated should move the two servos after a two minute delay
after 20 minutes the sketch should reset but the pir should not be able to activate again for an hour
the day night switch should only allow the program to work durring the day
we have no idea how the sensors work so maybe telling us how they connect would be a good idea
#include <Servo.h>
Servo vert;
Servo hor;
void setup() {
vert.attach(10);
hor.attach(11);
}
void migrate(Servo &myServo, int newPos) {
int wait=random(1,20); //randomize the wait to make it more interesting
int pos = myServo.read(); //Read the current servo position
if (pos < newPos) {
for (int i=pos; i < newPos; i++) {
myServo.write(i);
delay(wait);
}
} else {
for (int i=pos; i > newPos; i--) {
myServo.write(i);
delay(wait);
}
}
}
void randomPosition() {
int rand=random(10,100); //The range is limited to 60 deg for better cat action
migrate(hor, rand);
rand=random(40,110); //The vertical range is limited to 45 deg also for better cat action.
migrate(vert, rand);
}
void loop() {
randomPosition();
delay(0);
}
what i would like to do is connect a pir sensor and a day night sensor
the pir when activated should move the two servos after a two minute delay
after 20 minutes the sketch should reset but the pir should not be able to activate again for an hour
the day night switch should only allow the program to work durring the day.this is to be a laser toy for my cat when and if i get it done..lol
dont know how or which ports to use or how to right the programe. sorry mate iam a total nood with electronics and programming,i may have bitten off more than i can chew with this project appreciate the help
thanks for the feed back being a total nood with electronics and programming all the help get is appreciated
adding the code is where the probem lies i have no idea where to start
went out and bought an arduino programming guide hopefully i can get a bit wiser..lol
Ok, so during the day you would like to move a servo to a random position when the pir senses something, but no more frequntly than at 20 minute intervals.
if(it is daytime and it has been more than 20 minutes since the last move and the pir senses something) {
move the servo;
make a note of what time this move occurred;
}
So something like
const unsigned long MOVE_INTERVAL = 20L * 60L * 1000L;
unsigned long lastMoveTime;
void setup() {
lastMoveTime = -MOVE_INTERVAL;
}
boolean isDaytime() {
// code to sense daytime goes here
// return true if it's daytime, false otherwise
}
boolean isDelayTimeElapsed() {
return millis() - lastMoveTime >= MOVE_INTERVAL;
}
boolean isMotionBeingSensed() {
// code to work the PIR goes here
// return true if there's movement, false otherwise
}
void loop() {
if(isDayTime() && isDelayTimeElapsed() && isMotionBeingSensed()) {
// code to move the servo goes here
lastMoveTime = millis();
}
}
helping you build the code while you learn is not going to be difficult but we need enough information to know where to start and what to teach you.
If you have a arduino which one did you buy. (we need to know what pins we can use)
Do you know which sensors you bought. Do you have any idea how to wire them or do you need advice on that as well. Post links to web pages where you bought the sensors or give us the part numbers.
As long as you are willing to learn people are willing to show you.
if you have the parts connected then post a drawing like tom said so we can get this project started.
hey TomGeorge
[co#include <Servo.h>
Servo vert;
Servo hor;
void setup() {
vert.attach(10);
hor.attach(11);
}
void migrate(Servo &myServo, int newPos) {
int wait=random(1,20); //randomize the wait to make it more interesting
int pos = myServo.read(); //Read the current servo position
if (pos < newPos) {
for (int i=pos; i < newPos; i++) {
myServo.write(i);
delay(wait);
}
} else {
for (int i=pos; i > newPos; i--) {
myServo.write(i);
delay(wait);
}
}
}
void randomPosition() {
int rand=random(10,100); //The range is limited to 60 deg for better cat action
migrate(hor, rand);
rand=random(40,110); //The vertical range is limited to 45 deg also for better cat action.
migrate(vert, rand);
}
void loop() {
randomPosition();
delay(0);
}de]
\
dont have cad drawings mate all i have is this bit of code with two servos one to pin 10 and one to pin 11
this is written to read by someone rather than to be cleaver code. I tried to use long names that make sense but that doesn't mean they will make sense to you so ask questions if you do not understand it
P.s I didn't test it but it looks ok
#include <Servo.h>
Servo vert;
Servo hor;
const byte pirInputPin = 4;
const byte lightSensorPin = 5;
unsigned long runDelayStartTime=0;
unsigned long startTime=0;
unsigned long setTime=1200000; //20 minutes in millis is 1000 * 60seconds * 20 minutes
unsigned long runDelayTime=3600000; //1hour in millis is 1000*60secs*60mins
byte runLaserShow=0;
byte runDelay=0;
void setup() {
vert.attach(10);
hor.attach(11);
pinMode (pirInputPin, INPUT);
pinMode (lightSensorPin, INPUT);
}
void migrate(Servo &myServo, int newPos) {
int wait = random(1, 20); //randomize the wait to make it more interesting
int pos = myServo.read(); //Read the current servo position
if (pos < newPos) {
for (int i = pos; i < newPos; i++) {
myServo.write(i);
delay(wait);
}
} else {
for (int i = pos; i > newPos; i--) {
myServo.write(i);
delay(wait);
}
}
}
void randomPosition() {
int rand = random(10, 100); //The range is limited to 60 deg for better cat action
migrate(hor, rand);
rand = random(40, 110); //The vertical range is limited to 45 deg also for better cat action.
migrate(vert, rand);
}
void loop() {
//state of day/night switch relay equals Day,Output OFF
unsigned long currentTime = millis();
byte pirsensor = digitalRead(pirInputPin);
byte dayNightSensor = digitalRead (lightSensorPin);
if (dayNightSensor == LOW) { // its daytime
if (pirsensor == HIGH) { //movement sensed
//we will not add a 2 minute delay as thats avilable on hardware
runLaserShow = 1;//this is a flag saying that we want the laser light to start
}
}
if (runLaserShow == 0 && runDelay == 0) {//this keeps resetting a timer used to measure 20 mins
startTime = currentTime;//update starttime to millis
}
if (runDelay == 0) {// 1 hour lock out if rundelay==1
if (runLaserShow == 1) {//we want to have a light show
if (currentTime - startTime >= setTime) {
//times up
runLaserShow = 0;//remove flag
runDelay = 1;//set a flag to stop the show for 1 hour
runDelayStartTime = currentTime;//take a time stamp so we can measure a hour
} else {
randomPosition();//timers running so do this until time is up
}
}
}
if (runDelay == 1) {//has i hour passed since last laser show
if(currentTime - runDelayStartTime >= runDelayTime) {
runDelay = 0;//one hour is up remove flag
}
}
}
connect the pir to arduino pin 4 and light sensor switch to pin 5 (im sure you could have worked that out by reading the code but I forgot to add a note there)
thnx alot for your help mate your program is spot on i have saved it and am going to try to replicate it as a learning curve
one last question mate if you dont mind
the day night sensors got me a bit bugged,i know i have to connect it to pin 5 but witch wire goes to pin 5 and where do the other wires go, the sensor has 4 wires