Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Tricat Lvl 3
Message 1 of 1

A problem with millis() in Arduino.

Hi, I am doing a accountant with arduino, i want write in Serial "Start" and the arduino count the "Minutes;Seconds;Miliseconds", and i write "Stop" and the arduino stop the count and show me the time between "Start" and "Stop" in "Minutes;Seconds;Miliseconds". And if i write "Start"again, the arduino restart the count. I am doing this with millis(), but I don't make the program work.

 

Example:

>>>Start

Ronda Iniciada.

0;0;1

...

1;3;345

>>>Stop

Ronda Finalizada: 1;3;345

>>>Start

Ronda Iniciada.

0;0;1

...

 

I did a program but without the format "Minutes;Seconds;Miliseconds" in this program the arduino show me the time in minutes only example (4,5), but this program not either work.

 

I attach my programs. Help plsHeart.

PD: Sorry for my English, I am learning.

 

Program Whith Format "Minutes;Seconds;Miliseconds":

unsigned long TimerPrevio = 0;
unsigned long TiempoRonda;
int Tiempo[2];
int TiempoActual[2];
boolean Count = false;
void Comandos(){
  if(Serial.readString() == "Start"){
    TimerPrevio = millis();
    Count = true;
    Serial.println("Ronda Iniciada.");
  }
  else if(Serial.readString() == "Stop"){
    TiempoRonda = millis() - TimerPrevio;
    Convert(TiempoRonda,Tiempo);
    Count = false;
    Serial.print("Ronda Finalizada: ");
    for(int x=0;x <= 2;x++){
      Serial.print(Tiempo[x]);
      Serial.print(";");
    }
    Serial.print("\n");
  }
}

void Convert(unsigned long Entrada,int *Salida){
  int Minuto = Entrada / 60000;
  int Restante = Entrada % 60000;
  int Segundo = Restante / 1000;
  int Milisegundo = Restante % 1000;
  Salida[0] = Minuto;
  Salida[1] = Segundo;
  Salida[2] = Milisegundo;
}
void setup() {
Serial.begin(9600);
}

void loop() {
if (Serial.available()){
  Comandos();
}
if(Count == true){
  Convert(millis(),TiempoActual);
  for(int x=0;x <= 2;x++){
    Serial.print(TiempoActual[x]);
    Serial.print(";");
  }
  Serial.print("\n");
}
delay(1);
}

 Program Without the Format:

boolean Count = false;
unsigned long TimerActual;
unsigned long TimerPrevio;
unsigned long TiempoRonda;
unsigned long TiempoTranscurrido;
float _TiempoRonda;
float _TiempoTranscurrido;
void setup() {Serial.begin(9600);}
void loop() {
  TimerActual = millis();
  if(Serial.available()){
    if(Serial.readString() == "Start"){
      TimerPrevio = TimerActual;
      Count = true;
      Serial.println("Ronda Iniciada.");
     }
    else if(Serial.readString() == "Stop"){
      TiempoRonda = TimerActual - TimerPrevio;
      _TiempoRonda = TiempoRonda/60000;
      Count = false;
      Serial.print("Ronda Finalizada: ");
      Serial.println(_TiempoRonda);
    }
  }
  if(Count == true){
    TiempoTranscurrido = TimerActual - TimerPrevio;
    _TiempoTranscurrido = TiempoTranscurrido/60000;
    Serial.println(_TiempoTranscurrido);
  }
  delay(1);
}