This sketch will turn pin4 high correctly, but after the initial high, the led on pin13 toggles on/off at the "pause" rate (5 sec). Would you please advise where the error is?
void loop()
{
if(digitalRead(pirPin) == HIGH){
digitalWrite(enable,HIGH);
motor(); //WORKS
lastMotion = millis();
digitalWrite(ledPin, HIGH);
if(lockLow){
lockLow = false;
delay(50);
}
takeLowTime = true;
}
if(digitalRead(pirPin) == LOW){
digitalWrite(ledPin, LOW); //the led visualizes the sensors output pin state
if(takeLowTime){
lowIn = millis(); //save the time of the transition from high to LOW
takeLowTime = false; //make sure this is only done at the start of a LOW phase
}
if(!lockLow && millis() - lowIn > pause){
lockLow = true;
delay(50);
}
}
motor();
}
Please, just post the whole lot - we can't see what goes on in "motor", and we can't see what all the values are.motor(); //WORKS means not a great deal, unless "WORKS" is defined.
/*______________________________opto switch____________________*/
int switch1 = 5;
int switch2 = 6;
/*_____________________________motor___________________________*/
int randNumberSpeed; // variable to store the random speed value
int randNumberDir; // variable to store the random direction value
int motorSpeed = 11; // motor speed
int motorDir = 12; // motor direction
long lastMotion;
int enable = 3;
/*----------------------------PIR-------------------------------*/
long unsigned int lowIn;
int calibrationTime = 30;
long unsigned int pause = 5000;
boolean lockLow = true;
boolean takeLowTime;
int pirPin = 4; //the digital pin connected to the PIR sensor's output
int ledPin = 13;
/*---------------------------Motor--------------------------------*/
void setup()
{
//time = millis();
Serial.begin(9600);
randomSeed(millis());
pinMode(motorSpeed,OUTPUT);
pinMode(motorDir,OUTPUT);
pinMode(enable,OUTPUT);
/*----------------------------PIR------------------------------*/
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(pirPin, LOW);
//give the sensor some time to calibrate
for(int i = 0; i < calibrationTime; i++){
delay(1000);
}
/*________________________optoSwitch___________________________*/
pinMode(switch1,INPUT);
pinMode(switch2,INPUT);
}
/*---------------------------PIR-------------------------------*/
void loop()
{
if(digitalRead(pirPin) == HIGH){
digitalWrite(enable,HIGH);
motor(); //WORKS
lastMotion = millis();
digitalWrite(ledPin, HIGH);
if(lockLow){
lockLow = false;
delay(50);
}
takeLowTime = true;
}
if(digitalRead(pirPin) == LOW){
digitalWrite(ledPin, LOW); //the led visualizes the sensors output pin state
if(takeLowTime){
lowIn = millis(); //save the time of the transition from high to LOW
takeLowTime = false; //make sure this is only done at the start of a LOW phase
}
if(!lockLow && millis() - lowIn > pause){
lockLow = true;
delay(50);
}
}
motor();
}
/*----------------------------Motor---------------------------*/
void motor(){
if (millis() - lastMotion < 10000){
//randomSeed(millis()); // sets millis() as seed
randNumberDir = random(255); // random number from 0-255
analogWrite(motorDir, randNumberDir); // outputs PWM signal
delay(500); // pauses for half a second
randNumberSpeed = random(255); // random number from 0-255
analogWrite(motorSpeed, randNumberSpeed); // outputs PWM signal
delay(500); // pauses for half a second
//motorStop();
}
else{
digitalWrite(motorSpeed,LOW);
digitalWrite(motorDir,LOW);
digitalWrite(enable,LOW);
}
}
/*_______________________opto switch_______________________________*/
void optoSwitch(){
if(switch1 == HIGH){
digitalWrite(motorDir,HIGH);
digitalWrite(motorSpeed,LOW);
delay(500);
motor();
}
if(switch2 == HIGH){
digitalWrite(motorDir,LOW);
digitalWrite(motorSpeed,HIGH);
delay(500);
motor();
}
}