60 seconds Counter

I am working a project that uses Arduino Uno and WiFi Shield, 7 segment and a Digital distance sensor. Basically the problem i ran into is that i am require to let the counter run for 60 seconds however as you can see from the code, i will need to connect to the WiFi first and by using millis(), it takes away a few seconds or more than that if required from the 60 seconds. What can i do to start the time running after the WiFi connects? Any help would be appreciated. Thanks.

#include <SPI.h>
#include <WiFi.h>

// Arduino digital pins used to light up corresponding segments on the seven segment display
#define A 2
#define B 3
#define C 4
#define D 5
#define E 6
#define F_SEG 8
#define G 9

// Push button connected to pin 14 & Sensor connected to pin 15
#define PB 18
#define digitalDistanceSensor 17

// Pins driving common anodes
#define CA1 10
#define CA2 14

const int ledPin = 15;
const int speakerPin = 16;

char wifissid[] = "*****";
char wifipass[] = "****";
int keyIndex = 0;

char devid[] = "****"; // Device ID for PushingBox scenario

char serverName[] = "api.pushingbox.com";
//int count = digitalRead(digitalDistanceSensor); // Reading the input of the PB
int count = 0;
boolean pinDevid1State = false; // To save last state of the pin for DEVID1
int status = WL_IDLE_STATUS; // Status of the Wifi
WiFiClient client;
char postmsg[100];


// Pins for A B C D E F G in sequence
const int segments[7] = {A, B, C, D, E, F_SEG, G};

// Segments that make each number
const byte numbers[10] = {0b1000000, 0b1111001, 0b0100100, 0b0110000, 0b0011001, 0b0010010, 0b0000010, 0b1111000, 0b0000000, 0b0010000}; //logic 0 to turn on segment (common anode)

void setup()
{
  Serial.begin(9600);
  delay(1000);

  pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(C, OUTPUT);
  pinMode(D, OUTPUT);
  pinMode(E, OUTPUT);
  pinMode(F_SEG, OUTPUT);
  pinMode(G, OUTPUT);
  pinMode(PB, INPUT);
  pinMode(digitalDistanceSensor, INPUT_PULLUP);
  pinMode(CA1, OUTPUT);
  pinMode(CA2, OUTPUT);
  pinMode(speakerPin, OUTPUT);
  pinMode(ledPin, OUTPUT);

  Serial.println("Attempting to connect to WEP network...");
  status = WiFi.begin(wifissid, keyIndex, wifipass);
  if ( status != WL_CONNECTED) {
    Serial.println("Couldn't get a wifi connection");
    while (true);
  }
  else {
    Serial.println("Connected to network");
  }
} 

int unitDigit = 0;
int tenthDigit = 0;
//int currentState = LOW;
//int previousState = LOW;
unsigned long interval = 60000; //60 seconds
unsigned long timer = 0;
//int currentState = digitalRead(digitalDistanceSensor); // read from sensor


void loop() {

  if (digitalRead(digitalDistanceSensor) == HIGH && pinDevid1State == true)
  {
    unitDigit = (unitDigit + 1) % 10;
    if (unitDigit == 0) {
      tenthDigit = (tenthDigit + 1) % 10;
    }
    Serial.print("Number of Counts : ");
    count++;
    Serial.print(count);
    Serial.println();
    pinDevid1State = false;
    sendingRequest();
  }
  else if (digitalRead(digitalDistanceSensor) == LOW && pinDevid1State == false)
  {
    Serial.println("Sensor Mode : Sensing and Counting...");
    pinDevid1State = true;
  }


  unsigned long startTime = millis();
  for (unsigned long elapsed = 0; elapsed < 600; elapsed = millis() - startTime)
  {
    tenthsDigit(numbers[tenthDigit]);
    delay(5);
    unitsDigit(numbers[unitDigit]);
    delay(5);
  }

  // reset count
  int button = digitalRead(PB);
  if (button == HIGH)
  {
    unitDigit = 0;
    tenthDigit = 0;
    digitalWrite(speakerPin, LOW);
    digitalWrite(ledPin, LOW);
    button = LOW;
  }
}


void unitsDigit(byte number)
{
  digitalWrite(CA1, LOW); // set the LED off
  digitalWrite(CA2, HIGH); // set the LED on
  lightSegments(number);
}

void tenthsDigit(byte number)
{
  digitalWrite(CA1, HIGH); // set the LED on
  digitalWrite(CA2, LOW); // set the LED off
  lightSegments(number);
}

void lightSegments(byte number)
{
  for (int i = 0; i < 7; i++)
  {
    int bit = bitRead(number, i);
    digitalWrite(segments[i], bit);
  }
}

void sendingRequest()
{
  unsigned long currentMillis = millis();
  if ((currentMillis - timer) > (interval - 10000) && (currentMillis - timer) < interval)
  {
    digitalWrite(ledPin, HIGH);
  }

  else if ((currentMillis - timer) > interval)// && (millis() - timer) < (interval + 5000))
  {
    //digitalWrite(speakerPin, LOW);
    //delay(1000);

    digitalWrite(ledPin, LOW);
    //digitalWrite(speakerPin, HIGH);
    //delay(1000);
    //digitalWrite(speakerPin, LOW);
    delay(1000);

    if (client.connect(serverName, 80)) {
      Serial.println("connected");
      Serial.println("sending request");
      digitalWrite(ledPin, HIGH);
      delay(1000);
      sprintf(postmsg, "GET /pushingbox?devid=*****&count=%d HTTP/1.1", count);
      client.println(postmsg);
      client.print("Host: ");
      client.println(serverName);
      client.println("User-Agent: Arduino");;
      client.println();

      Serial.println(postmsg);
      Serial.println("Host: api.pushingbox.com");
      Serial.println("Connection: close");
      Serial.println();
      digitalWrite(ledPin, LOW);
      delay(1000);
      count = 0;
    }
    else {
      Serial.println("Connection Failed");
      client.stop();
      count = 0;
    }
  }
}

Set the value of the variable startTime after the WiFi connects ?

Or have I missed something?

…R

Robin2:
Set the value of the variable startTime after the WiFi connects ?

Or have I missed something?

...R

Thanks for the quick reply. May I know how to do that? Because the connecting to the WiFi is in the Void Setup
and startTime is in the loop and I am not sure what do you mean.

I'm not sure if it would meet the needs of how your program should work (i.e. whether the timing would be correct) but you could create startTime as a global variable and set it in setup()

...R

Robin2:
I'm not sure if it would meet the needs of how your program should work (i.e. whether the timing would be correct) but you could create startTime as a global variable and set it in setup()

...R

I had sort of the same idea just moments ago as well. Thank you so much for the help!!