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

Hey guys,

I feel I am nearly done.

To clarify what i am trying to do again:

The aim is to create two tripwires using lasers and LDRs which work together to count the number of people entering and leaving a room. The Arduino records this number as people.

What I want to achieve is to make a constantly moving servo to adapt it's sweeping speed dependent on the number of people in the room.

Example: 0 people in the room = servo speed minimum
1 person in the room = servo speed increases
5 people in the room = servo speed increased greatly
10 people in the room = servo speed max
3 people in the room = servo speed decreased greatly
(the numbers of people would obviously not be recorded like that. It would be sequential)

This is what I am struggling with. I would use delays to easily change the servo speed but I am avoiding delays so that the whole setup operates constantly.
Does anyone have tips on how to make the servo speed change based on people number?

Thank you. :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 servoInterval = 80; // 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 = 0;


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

void loop(){

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

  peoplecounter();
  servoSweep();
  
  sensA = analogRead(A0);                    
  sensB= analogRead(A1);
  
  digitalWrite(4, HIGH);
  digitalWrite(2, HIGH);
  people = constrain (people, 0, 10);
  
 Serial.print("current    ");
 Serial.print(currentMillis);
 Serial.print("   activated   ");
 Serial.print(activatedMillis); 
 Serial.print("   Servo Speed   ");
 Serial.print(servoDegrees);
 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 peoplecounter(){
 

  
  if(sensA<thresh && sensB>thresh)
    {
        activatedMillis = currentMillis;
        people=people+1; 
        digitalWrite(13, HIGH);
        thresh = 0;
    }
    else{
      if(currentMillis - activatedMillis >= interval)
      {thresh = 800;
      }
     else{
      people=people;
      digitalWrite(13, LOW);
   
     }
    } 
    
        
  if(sensA>thresh && sensB<thresh)
    {
        activatedMillis = currentMillis;
        people=people-1; 
        digitalWrite(13, HIGH);
        thresh = 0;
    }
    else{
      if(currentMillis - activatedMillis >= interval)
      {thresh = 800;
      }
     else{
      people=people;
      digitalWrite(13, LOW);
   
     }
    } 
  }


void servoSweep() {
 
 if (currentMillis - previousServoMillis >= servoInterval) {
       // its time for another move
   previousServoMillis += servoInterval;
   servoPosition = servoPosition + servoDegrees; // servoDegrees might be negative

 if ((servoPosition >= servoMaxDegrees) || (servoPosition <= servoMinDegrees)) {
    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);
 }
  }