In void loop trying to get servo to move and reset once only

Building a puzzle geocache, which broadcasts a wifi access point, the idea is when 5 smart phones connect the servo unlocks a door and resets ( servo).

Sketch basically runs, but once number of connections( for now 2) is made the servo opens and closes continuously. It needs to only move once to open and return to position to reset the door. For now I introduced a long delay sort of works but in bring about other obvious issues.

if (nb > 1) is the area i am having a challenge with. Another issue is the servo moving on start up.

Here is all the code:

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
#include <ESP8266WiFi.h>
#include <Servo.h>
#define SERVOPIN 13
Servo servo;

/* Your WiFi Soft Access Point settings /
const char
ssid = “Geocaching Network”; //this will be the network name
const char* password = “”; //this will be the network password

void setup() {
int pos = 0;
servo.attach(SERVOPIN);
servo.write(0); // Degree position

// initialize the LCD
lcd.begin();

// Turn on the blacklight and print a message.
lcd.backlight();
lcd.print(“Welcome to”);
lcd.setCursor(0,1);
delay(1500);
lcd.print(“A Connected Team”);
lcd.setCursor(0,0);
delay(2000);
lcd.clear();
lcd.print(“Cache unlocks”);
lcd.setCursor(0,1);
lcd.print(“when connections”);
delay(2000);
lcd.clear();
lcd.print(“equals 5”);
delay(2000);
lcd.clear();
lcd.setCursor(0,0);

pinMode (2, OUTPUT);
delay(1000);
Serial.begin(115200);
Serial.println();
Serial.println();
Serial.print(“Configuring WiFi access point…”);

delay(2000);

/* You can remove the password parameter if you want the AP to be open. */
boolean result = WiFi.softAP(ssid);
if(result==true) {
IPAddress myIP = WiFi.softAPIP();

Serial.println(“done!”);
Serial.println("");
Serial.print("WiFi network name: ");
Serial.println(ssid);
Serial.print("WiFi network password: ");
Serial.println(password);
Serial.print(“Host IP Address: “);
Serial.println(myIP);
Serial.println(””);
}

else {
Serial.println(“error! Something went wrong…”);
}
}

void loop() {
Serial.printf(“Number of connected devices (stations) = %d\n”, WiFi.softAPgetStationNum());
lcd.setCursor(0,0);
lcd.print("# of Connected");
lcd.setCursor(5,1);
lcd.printf(“Devices = %d\n”, WiFi.softAPgetStationNum());
delay(3000);

// WiFi.softAPgetStationNum == 2;{
// DoSomething();
int nb = WiFi.softAPgetStationNum();

if (nb > 1){

//Move servo 90 degrees to pull on latch
//for(pos = 0; pos < 90; pos += 2)

servo.write(90);

}

// wait 1/2 second for door to open
delay(400);

// Move servo back to initial position
//for(pos = 90; pos>=1; pos-=2)

servo.write(0);
delay(10);

}

Hi, Welcome to the forum.

Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code. It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom... :)

I did not bother to compile as I am missing the lcd lib
but it should be closer to what you need.

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
#include <ESP8266WiFi.h>
#include <Servo.h>
#define SERVOPIN      13
Servo servo;




/* Your WiFi Soft Access Point settings */
const char* ssid = "Geocaching Network";          //this will be the network name
const char* password = "";  //this will be the network password

uint32_t  lastattempt = 0; //-(POLL_INTERVAL * 1000L); // neg on purpose!
bool      dooropen = false;
uint32_t  dooropentime = 0;

void setup() {
int pos = 0;
  servo.attach(SERVOPIN);
  servo.write(0); // Degree position

  // initialize the LCD
  lcd.begin();

  // Turn on the blacklight and print a message.
  lcd.backlight();
  lcd.print("Welcome to");
  lcd.setCursor(0,1);
  delay(1500);
  lcd.print("A Connected Team");
  lcd.setCursor(0,0);
  delay(2000);
  lcd.clear();
  lcd.print("Cache unlocks");
  lcd.setCursor(0,1);
  lcd.print("when connections");
  delay(2000);
  lcd.clear();
  lcd.print("equals 5");
  delay(2000);
  lcd.clear();
  lcd.setCursor(0,0);
  
  pinMode (2, OUTPUT);
  delay(1000);
  Serial.begin(115200);
  Serial.println();
  Serial.println();
  Serial.print("Configuring WiFi access point...");
  delay(2000);
  
  /* You can remove the password parameter if you want the AP to be open. */
  boolean result = WiFi.softAP(ssid);
  if(result==true) {
    IPAddress myIP = WiFi.softAPIP();
  
    Serial.println("done!");
    Serial.println("");
    Serial.print("WiFi network name: ");
    Serial.println(ssid);
    Serial.print("WiFi network password: ");
    Serial.println(password);
    Serial.print("Host IP Address: ");
    Serial.println(myIP);
    Serial.println("");
  } else {
    Serial.println("error! Something went wrong...");
  }
}

void loop() {
  if (dooropen){
    if((millis() - dooropentime) > 400){ // wait 1/2 second for door to open  
      dooropen = false;
      servo.write(0);// Move servo back to initial position
    }
  }
  if ((millis() - lastattempt  ) < 3000){return;}
  lastattempt = millis();
  int nb = WiFi.softAPgetStationNum();

  Serial.printf("Number of connected devices (stations) = %d\n", nb);
  lcd.setCursor(0,0);
  lcd.print("# of Connected");
  lcd.setCursor(5,1);
  lcd.printf("Devices = %d\n", nb);

  if (nb > 1){ 
   servo.write(90);  
   dooropen = true; 
   dooropentime = millis();
  } //Move servo 90 degrees to pull on latch
}

Wow Thanks for the reply, got it compiled servo is not triggering. Thanks for an intro into a new level of coding. We keep tinkering.