2008年11月10日 星期一

presice timing with arduino

Arduino Forum - presice timing with arduino

int analogPin = 3;     // potentiometer wiper (middle terminal) connected to analog pin 3
                       // outside leads to ground and +5V
int val = 0;           // variable to store the value read
int sta = 0;           // sta 0 =low 1 = high;
int counter = 0 ;        // conter
int time = 0 ;     
int i = 1;
int ii = 0;
int sample = 50;
void setup()
{
  Serial.begin(115200);          //  setup serial
}

void loop()
{
 //  Serial.print(i);
   val = analogRead(analogPin);    // read the input pin 
  
   if (val >= 400){
     if((i-ii) >= 5){
  //   Serial.print(" ");
  //   Serial.print(val);    // debug value  
   ii = i ; 
   counter++ ;
   }

  // sta = 1;
  
   }else{
 // Serial.println(10);
  // sta = 0;
   //  delay(1000);  1 sec
}
//Serial.println();
delay(50); // delay 0.05 sec

if (i >= 400) {        // passs 20 sec;
 time = counter * 3 ;
 Serial.println(time);
  i = 1;
  ii = 1;
  counter = 0;
 }
i++;
}

沒有留言: