LusoRobótica - Robótica em Português

Sistemas específicos => Arduino / AVR => Tópico iniciado por: joaopedrocmp em 11 de Maio de 2011, 21:41

Título: Fazer um time counter
Enviado por: joaopedrocmp em 11 de Maio de 2011, 21:41
Boas pessoal, tenho um time counter que vinha num projecto que saquei ha um tempo, agora estou a ver se entendo como funciona para fazer um, mas isto está complicado. será que o codigo do countr que tenho é facil de preceber, ou ha mais facil?

tenho isto, entre outro codigo:

Código: [Seleccione]
void countdown(){
 
    //Make sure tether hasn't been removed
    pinStatus = digitalRead(tether);
   
       if (pinStatus == HIGH) { //if the tether has been removed,
           selectLineOne();
           delay(100);
           Serial.print("Cabo Removido"); //make it known
           delay(1000);
           clearLCD();
           bombState = DETONATED; //and set off bomb.
       }
 
  static unsigned long lastTick = 0; // set up a local variable to hold the last time we decremented one second
// (static variables are initialized once and keep their values between function calls)

// decrement one second every 1000 milliseconds
  if (second > 0) {
      if (millis() - lastTick >= 1000) {
          lastTick = millis();
          second--;
          serialOutput();
      }
  }

 // decrement one minute every 60 seconds
  if (minute > 0) {
      if (second <= 0) {
          minute--;
          second = 60; // reset seconds to 60
      }
  }

// decrement one hour every 60 minutes
  if (hour > 0) {
      if (minute <= 0) {
          hour--;
          minute = 60; // reset minutes to 60
      }//closes if
  }//closes if
 
} //close countdown();



..............................................................

void serialOutput() {
//clearLCD();
  backlightOn();
//Print time on each line
  selectLineTwo();
  delay(100);
  Serial.print("ARMADA : ");
  Serial.print(hour, DEC); // the hour, sent to the screen in decimal format
  Serial.print(":"); // a colon between the hour and the minute
  Serial.print(minute, DEC); // the minute, sent to the screen in decimal format
  Serial.print(":"); // a colon between the minute and the second
  Serial.println(second, DEC); // the second, sent to the screen in decimal format
//termination condition
  if (second == 0 && minute == 0 && hour == 0) {
      clearLCD();
      backlightOn();
      bombState = DETONATED; //clear LCD and set bomb state to "detonated" when timer runs out
  }
}//close serialOutput();



uma ajudinha please ;)
Título: Re: Fazer um time counter
Enviado por: senso em 11 de Maio de 2011, 21:46
Qual é a duvida?
Título: Re: Fazer um time counter
Enviado por: joaopedrocmp em 11 de Maio de 2011, 21:49
è mais fácil utilizar esse counter para aprender a fazer um, ou ha algum tipo mais facil?

Como se estrutura?
Título: Re: Fazer um time counter
Enviado por: senso em 11 de Maio de 2011, 21:56
Chama-lhe contador que estamos em portugal lol.

Se esse funciona é usar esse, se não é contar milisegundos que no arduino é só chamar o millis e está feito, que é o que todos os contadores fazem porque no arduino se mexes nos timers rebentas com aquilo tudo.
Título: Re: Fazer um time counter
Enviado por: joaopedrocmp em 11 de Maio de 2011, 21:59
pronto, entao é assim, ja pus esse contador no código que tenho, mas quando chega a altura de contar, fica parado...

:S

Como lhe dou corda? xD
Título: Re: Fazer um time counter
Enviado por: senso em 11 de Maio de 2011, 22:04
Declaras o tempo inicial em vez de a zeros com os segundos e minutos que queres
Título: Re: Fazer um time counter
Enviado por: joaopedrocmp em 11 de Maio de 2011, 22:14
Isso está declarado. este é o codigo que tenho:

Código: [Seleccione]
int sensorpin = 4;              // switch is connected to pin 4
int holdpin = 8;            // define botão de iniciar  para pin 8
int sensorpinstatus = 0;//guarda valor do sensorpin
int holdpinstatus = 0; //guarda o valor do holdpin
int second=0, minute=10, hour=0; // declare time variables

long previousTime = 0; //holds previous time in ms
long interval = 60000; //60 second delay

void setup()
{
  Serial.begin(9600);
  clearLCD();
  //backlightOn();
  pinMode(sensorpin, INPUT);    // Define o switch pin como Input
  pinMode(holdpin, INPUT);
}
bailout:
void loop(){
           
        sensorpinstatus = digitalRead(sensorpin);   
        if (sensorpinstatus == HIGH) {       
           //  clearLCD(); 
            delay(100);
             selectLineOne();
             Serial.print("    Inserir     ") ;         
             selectLineTwo();
             Serial.print("     Lamela     ");
           }
           
           
        sensorpinstatus = digitalRead(sensorpin);   
        if (sensorpinstatus == LOW) {
   
          delay(100);
     
      holdpinstatus = digitalRead(holdpin);   
     
     if (holdpinstatus == HIGH) {
     
           //clearLCD();
           delay(100);
           selectLineOne();
           Serial.print("    Iniciar    ");
           selectLineTwo();
           Serial.print("     Exame      ");
        }
 
      holdpinstatus = digitalRead(holdpin);   
           if (holdpinstatus == LOW)
       {
           //clearLCD();       
           countdown; 
               }
    }
}


       
/***********************************************************
* Main countdown timer                                     *
*                 countdown()                              *
************************************************************/
void countdown(){
 
   
    holdpinstatus = digitalRead(holdpin);
   
       if (holdpinstatus == HIGH) { //if the tether has been removed,
           selectLineOne();
           delay(100);
           goto bailout;
       }
 
  static unsigned long lastTick = 0; // set up a local variable to hold the last time we decremented one second
// (static variables are initialized once and keep their values between function calls)

// decrement one second every 1000 milliseconds
  if (second > 0) {
      if (millis() - lastTick >= 1000) {
          lastTick = millis();
          second--;
          serialOutput();
      }
  }

 // decrement one minute every 60 seconds
  if (minute > 0) {
      if (second <= 0) {
          minute--;
          second = 60; // reset seconds to 60
      }
  }

// decrement one hour every 60 minutes
  if (hour > 0) {
      if (minute <= 0) {
          hour--;
          minute = 60; // reset minutes to 60
      }//closes if
  }//closes if
 
} //close countdown();


/****************************************************************
*                     serialOutput();                           *
*    prints the values of the timer over the serial connection  *
*         and onto the LCD                                      *
*****************************************************************/
void serialOutput() {
  //clearLCD();
  //backlightOn();
//Print time on each line
  selectLineOne();
  Serial.print("   Em Analise   ");
  selectLineTwo();
  delay(100);
  Serial.print("Duracao:");
  Serial.print(hour, DEC); // the hour, sent to the screen in decimal format
  Serial.print(":"); // a colon between the hour and the minute
  Serial.print(minute, DEC); // the minute, sent to the screen in decimal format
  Serial.print(":"); // a colon between the minute and the second
  Serial.println(second, DEC); // the second, sent to the screen in decimal format
//termination condition
  if (second == 0 && minute == 0 && hour == 0)
      clearLCD();
      backlightOn();
      Serial.print("Codigo 2293045");     
}
//close serialOutput();


/*********************************************************************
*       Serial LCD disagnostic and general use tools                 *
*   selectLineOne(); | selectLineTwo(); | goTo(); | clearLCD();      *
*      backlightOn(); | backlightOff(); | serCommand();             *
**********************************************************************/
void selectLineOne(){  //puts the cursor at line 0 char 0.
   Serial.print(0xFE, BYTE);   //command flag
   Serial.print(128, BYTE);    //position
}
void selectLineTwo(){  //puts the cursor at line 0 char 0.
   Serial.print(0xFE, BYTE);   //command flag
   Serial.print(192, BYTE);    //position
}
void goTo(int position) { //position = line 1: 0-15, line 2: 16-31, 31+ defaults back to 0
if (position<16){ Serial.print(0xFE, BYTE);   //command flag
              Serial.print((position+128), BYTE);    //position
}else if (position<32){Serial.print(0xFE, BYTE);   //command flag
              Serial.print((position+48+128), BYTE);    //position
} else { goTo(0); }
}

void clearLCD(){
  Serial.print(0xFE, BYTE);   //command flag
  Serial.print(0x01, BYTE);   //clear command.
}
void backlightOn(){  //turns on the backlight
    Serial.print(0x7C, BYTE);   //command flag for backlight stuff
    Serial.print(157, BYTE);    //light level.
}
void backlightOff(){  //turns off the backlight
    Serial.print(0x7C, BYTE);   //command flag for backlight stuff
    Serial.print(128, BYTE);     //light level for off.
}
void serCommand(){   //a general function to call the command flag for issuing all other commands   
  Serial.print(0xFE, BYTE);
}



O Objectivo, é simular uma maq. de analise de dna, em que inicialmente tem que se introduzir uma lamela, e depois pressionar um botão durante 10 mins para fazer a analise. se a lamela for retirada, volta para o estado inicial, e se o botão for largado, volta para "iniciar teste".
Título: Re: Fazer um time counter
Enviado por: senso em 11 de Maio de 2011, 22:56
           countdown; 
Não estás a chamar a função, mete antes
            countdown(); 
Título: Re: Fazer um time counter
Enviado por: joaopedrocmp em 11 de Maio de 2011, 22:58
Fooogo.

Que cegueta meu xD

Obrigadão, ja está a funcionar :P