nnob needs help with programming

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

appreciate the help

regards
kevin
[code][/code]

Please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

That code won't compile because there is no loop() function.

Have you any code that will work?

...R

one step at a time.

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);
}

sorry this is my first time trying to program a full project that incorporates different sensors

All that loop does now is call randomPosition() and then do a stupid delay(). (It makes NO sense to call delay() with a value of 0.)

Add some code to read the light sensor. If the value is above (or below, as appropriate) some threshold, then read the PIR sensor. If not, do nothing.

If the PIR sensor sensed motion, then call randomPosition().

hey gpop1

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

regards
kevin

hey PaulS

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();
  }
}

hi PaulMurrayCbr

i would like the servos to move randomly when motion is sensed and stop after 20 minutes

so my cat can play chasing the dot for 20 min

thanks for your input mate really appreciate it

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Can you please post a copy of your sketch, using code tags?
They are made with the </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Thanks... Tom.... :slight_smile:

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 gpop1

thanks for your help

bought an arduino uno and the sensors i got from ebay are as follows

motion sensor:

New HC-SR501 Infrared PIR Motion Sensor Module
http://www.ebay.com.au/itm/New-HC-SR501-Infrared-PIR-Motion-Sensor-Module-for-Arduino-Raspberry-pi-HCSR501-/171125346925?hash=item27d7dd926d&_uhb=1

Working voltage range: DC 4.5-20V

Static current: <50uA

High level output: 3.3 V / low 0V

Trigger mode: L unrepeatable trigger /H repeated trigger (Default Repeating trigger)

Delay time: 0.5-200S (adjustable) can be produced range 0.1 seconds - a few minutes

Blocking time: 2.5S (the default) can be produced range 0.1 seconds, tens of seconds

Size: 32mm*24mm circuit board

Induction angle: <100 degree cone angle

Working temperature: -15-+70 degrees

Induction of lens size: diameter: 23mm (default)

daynight switch

Red Long: Input +

Red Short: Output +

Black Short: Output -

Black Long: Input -

Specifications:

Color of Cover: White

Dimensions:49x27x15mm

Working Voltage: DC 5V~18V

Maximum current: 3A

Current consumption: about 3mA

Workting Mode:Day Off, Night Work

Adjustable Sensitivity:Yes

LED Status:

Day,Output OFF: Green

Dark,Output On: Red

Wiring:

Long Red Cable: Input +

Long Black Cable: Input -

Short Red Cable: Output +

Short Black Cable: Output -

hey gpop1

sorry mate forgot the link to day night switch here it is

http://www.ebay.com.au/itm/Day-Off-Night-On-DC-5V-9V-12V-15V-Light-Control-Switch-Controller-/301681854043?hash=item463da3c65b&_uhb=1

thanks again for all your help

 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

absolute nood no electronic or programming skills

thanks for your help

Hi,
Your code tags missed it by "that much" mate.
Could you please go back and re-edit the code tags.

Thanks.... Tom.... :slight_smile:

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
    }
  }
}

ops forgot to add

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)

TomGeorge

seems i got it right this time,sorry for that mate

thnx gpop1 mate i will go through and see if it makes sense

thanks alot for all your help mate,truly appreciated

if the code I posted works and you understand how it works go back a few posts and try paulmurrays code and fill in the blanks.

hey gpop1 mate

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

Red Long: Input +

Red Short: Output +

Black Short: Output -

Black Long: Input -

sorry for all the questions mate