Using mills as a pause

Hi,

So i got this thing going, where i am trying to turn on a motor for x amount of time CW then CCW when i get an input from a sensor. The code seems to work, but i happens way to fast and i cant get my head around on why. When that is done, i'm gonna try to figure out a way to block it from happening again for around 120 sec

Here is the diagram

And here is the code

Best regards
Tod


int sensorPin = 8;
int pushSpray = 2;
int releaseSpray = 3;
int oldState;
unsigned long startTime;

void setup() {
 
 pinMode (pushSpray, OUTPUT);
 pinMode (releaseSpray, OUTPUT);
 pinMode (sensorPin, INPUT);

}

void loop() {

 {
   int sensorState = digitalRead(sensorPin);
   if (sensorState == LOW)

   {
     if (oldState == HIGH)
     {
       digitalWrite (pushSpray, HIGH);
       startTime = millis ();
     }
   }
     oldState = sensorState;
   if (digitalRead (pushSpray == HIGH))
   {
      if (millis() - startTime >= 5000);
      {
        digitalWrite (pushSpray, LOW);
      }
   }
   

  
     // SECOND PART
  
   int secondsensorState = digitalRead(sensorPin);
   if (secondsensorState == LOW)
   {
     if (oldState == HIGH)
     {
       digitalWrite (releaseSpray, HIGH);
       startTime = millis ();
     }
   }
   oldState = secondsensorState;
   if (digitalRead (releaseSpray == HIGH))
   {
      if (millis() - startTime >= 20000);
      {
        digitalWrite (releaseSpray, LOW);
      }
   }
   
 }
}
  

leds without current limiting resistors is always a bad start
I know that {} come cheap by the dozen... but it's not a reason to have them everywhere... but you need them (and not a semicolon) where it's needed like

what are you requirements (what should the code do)?

Hi,

I want a motor (thats on a separate circuit, it only needs high input and it will turn the motor CW or CCW on two separate wires) to turn when it gets a signal from the sensor. It need to run CW for amount of tim then CCW for x amount of time when. But all of that needs to hapen x amount of time after the first triger from the sensor.

I was considering to use a statment to count triger from the sensor and when i have two triggers to set of the millis things.

so you want to detect a trigger on the sensor and wait a bit and then do the CW and CCW movement regardless of the status of the sensor?

the code coud be

loop
  await sensor trigger
  wait a bit
  move CW for some time
  move CCW for some time

no need for millis()

if you want to use millis, then you need to maintain the state (a state machine approach) - are you waiting for the trigger or in the CW or CCW part of the movement?

1 Like

Yes, thats correct. I will do some research how to write that. Thank you for your guidance! :grin:

//Tod

Hi again,

Change the code, but on my serial monitor the IR sensor counts up as long as some thing is in front. So if i pass my finger in front of it, it will count of to about 50 or so. It doesn't see each pass as one signal. So I'm not sure if i should change sensor or do something with the code. Any suggestions?

#define outoftheBox 2
int motorCw = 3;       //Pushes Plunger down
int motorCcw = 4;      //Pulls Plunger back
int sensorPin = 6;     //IR sensor, Activated = LOW


void setup() {
 pinMode (sensorPin, INPUT);
 pinMode (motorCw, OUTPUT);
 pinMode (motorCcw, OUTPUT);
 Serial.begin(9200);
}

void loop(){

 int catjumpsCount = 0; //Counts how many times the cat passed the IR sensor
 int catJumps = false;  // if true the cat jumped in and out of the box   

 if (digitalRead(sensorPin) == LOW && catJumps == false){
   catjumpsCount =+ 1;
        Serial.println(catjumpsCount);
   if (catjumpsCount >= outoftheBox){
     delay(3000);
     digitalWrite(motorCw, HIGH);
     delay(400);
     digitalWrite(motorCw, LOW);
     digitalWrite(motorCcw, HIGH);
     delay(450);
     digitalWrite(motorCcw, LOW);
     catJumps = true;
     if (catJumps = true){
       catjumpsCount = 0;
       catJumps = false;
     }
   }  

  }
}

look at the state change detection example in the IDE

You meant to write:
if (digitalRead (pushSpray) == HIGH)

In this case no harm done because no current will flow through the LEDs anyway! The breadboard ground rails are not connected. Same goes for the 5V rail, so the sensor will not work and the input pin will float.

2 Likes

LOL. Indeed

1 Like

I read more about how this specific sensor worked, so i fixed it and i leaned a lot of things about coding as well, so thank you:)!
As for the Az delivery IR sensor you just need to use the enable function on one of the pins and do a delay of 210ms because of the clock speed on it.

And if ant one wondering in the future what this is. Is one of those scented sprays with this 3 setting on how frequent it will spray. I by passed the circuit and and use this code to detect when the cat had been in the cat toilet and then spray.

I might implement a counter in the future to tell me when the spray is low so i can replace it.

Here is the final code :slight_smile:

#define outoftheBox 2
int motorCw = 3;       //Pushes Plunger down
int motorCcw = 4;      //Pulls Plunger back
int sensorRead = 6;     //IR sensor, Activated = LOW
int sensorEnable = 5;

void setup() {
 pinMode (sensorRead, INPUT);
 pinMode (sensorEnable, OUTPUT);
 pinMode (motorCw, OUTPUT);
 pinMode (motorCcw, OUTPUT);
}
void loop(){
 int catjumpsCount = 0; //Counts how many times the cat passed the IR sensor
 int catJumps = false;  // if true the cat jumped in and out of the box   
   digitalWrite (sensorEnable, HIGH);
   delay(210); 
 if (digitalRead(sensorRead) == LOW && catJumps == false){
      catjumpsCount ++;
   if (catjumpsCount == outoftheBox){
     catJumps = true;
       delay(2000);
     digitalWrite(motorCw, HIGH);
       delay(800);
     digitalWrite(motorCw, LOW);
     digitalWrite(motorCcw, HIGH);
       delay(600);
     digitalWrite(motorCcw, LOW);
     
     if (catJumps = true){
       catjumpsCount = 0;
       catJumps = false;
       digitalWrite (sensorEnable, LOW);
     }
   }  

  }

}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.