[ create a new paste ] login | about

Link: http://codepad.org/XXrythgN    [ raw code | fork ]

C, pasted on Nov 9:
void setup() {
  Serial.begin(9600);
  pinMode(2,INPUT_PULLUP);
  pinMode(13,OUTPUT);
  attachInterrupt(digitalPinToInterrupt(2), do_count, FALLING);
  delay(1000);
}

int num_stops = 4;

volatile boolean led_state=0;
volatile int trigs = 0;
volatile long time_first_trig = 0;
volatile long time_last_trig = 0;

void do_count() {
  trigs++;
  digitalWrite(13,led_state=!led_state);
  long time_now = micros();
  if (!time_first_trig) time_first_trig = time_now;
  time_last_trig = time_now;
}

void loop() {
  Serial.print("pin="); Serial.print(digitalRead(2)); Serial.print(" ");
  Serial.print("trigs="); Serial.print(trigs); Serial.print(" ");

  noInterrupts();
  long td = (time_last_trig - time_first_trig);
  double rps = (trigs-1)/(td/1000000.0)/num_stops;
  interrupts();
  Serial.print("td="); Serial.print(td); Serial.print(" ");
  Serial.print("rps="); Serial.print(rps); Serial.print(" ");
  Serial.print("rpm="); Serial.print(rps*60); Serial.print(" ");
  Serial.println(""); 
  trigs=0;
  time_last_trig=0;
  time_first_trig=0;
  delay(1000);
}


Create a new paste based on this one


Comments: