Trying to change servo sweep speed based on number of people in a room

Thanks for the input guys!

I have tried to understand millis using some of the info you gave me, but I am really struggling!

At this moment in time , I have managed to make my servo move without interruption from the LDRs. But I am now having loads of issues with the people counter. Currently the counter doesn't actually count anymore, and it only serial.writes a few times when the laser connection is broken.

So one thing has made progress and one has messed up a load :confused:
The way the two lasers and LDRs used to work was when one was activated, it would add +1 to the people count, then delay for a few seconds to allow the person to fully move through the lasers.

By trying to replace the delay with millis, I have been unsuccessful in replicating the way this worked.
I must be clearly doing something horribly wrong to cause my working code to not work at all?

I haven't even got round to trying to make my servo change speeds based on the number of people yet, because I clearly don't understand how to make the millis work!! haha

Any advice on getting my code back on track?

Thanks! :slight_smile:

#include <Servo.h>

const int servoMinDegrees = 0;
const int servoMaxDegrees = 180;

Servo myservo;

int servoPosition = 90;     // the current angle of the servo - starting at 90.
int servoSlowInterval = 80; // millisecs between servo moves
int servoFastInterval = 10;
int servoInterval = servoSlowInterval; // initial millisecs between servo moves
int servoDegrees = 2;       // amount servo moves at each step 
                           //    will be changed to negative value for movement in the other direction
unsigned long currentMillis = 0;    // stores the value of millis() in each iteration of loop
unsigned long previousServoMillis = 0; // the time when the servo was last moved
long interval = 3000;
long activatedMillis = 0;

int sensA; 
int sensB; 
int people = 0;
int thresh;


void setup() {
  Serial.begin(9600);
  Serial.println("---Starting People Detection---");
  pinMode(4, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(13, OUTPUT);
  
  myservo.attach(9);
  myservo.write(servoPosition); // sets the initial position
}

void loop(){

  currentMillis = millis();   // capture the latest value of millis()

  peoplecounter();
  servoSweep();
 
}
void peoplecounter(){
 {
  sensA = analogRead(A0);                    
  sensB= analogRead(A1);
  thresh = 900;
  digitalWrite(4, HIGH);
  digitalWrite(2, HIGH);
  people = constrain (people, 0, 10);
  
  if(sensA<thresh && sensB>thresh)
    {
      activatedMillis = currentMillis;
      if(currentMillis - activatedMillis > interval) {
    digitalWrite(13, HIGH);
    people=people+1;
    
    } 
    else{
      people=people;
      digitalWrite(13, LOW);
  }

  if(sensA>thresh && sensB<thresh)
    {
        activatedMillis = currentMillis;
      if(currentMillis - activatedMillis > interval) {
    digitalWrite(13, HIGH);
    people=people-1;
    
    } 
    else{
      people=people;
      digitalWrite(13, LOW);
  }}
 
 Serial.print("sensA is :  ");                          
 Serial.print(sensA   ); 
 Serial.print("      sensB is :  ");
 Serial.print(sensB    );
 Serial.print("     people :  "); 
 Serial.print(people); 
 Serial.print("  thresh val : ");
 Serial.println(thresh);
 }

}
}
void servoSweep() {

     // this is similar to the servo sweep example except that it uses millis() rather than delay()

     // nothing happens unless the interval has expired
     // the value of currentMillis was set in loop()
 if (currentMillis - previousServoMillis >= servoInterval) {
       // its time for another move
   previousServoMillis += servoInterval;
   
   servoPosition = servoPosition + servoDegrees; // servoDegrees might be negative

   if (servoPosition <= servoMinDegrees) {
         // when the servo gets to its minimum position change the interval to change the speed
      if (servoInterval == 80) {
        servoInterval = 10;
      }
      else {
       servoInterval = 80;
      }
   }
   if ((servoPosition >= servoMaxDegrees) || (servoPosition <= servoMinDegrees))  {
         // if the servo is at either extreme change the sign of the degrees to make it move the other way
     servoDegrees = - servoDegrees; // reverse direction
         // and update the position to ensure it is within range
     servoPosition = servoPosition + servoDegrees; 
   }
       // make the servo move to the next position
   myservo.write(servoPosition);
       // and record the time when the move happened
 }
}