Speed rotation measurement - Arduino

Ứng dụng Arduino trong điều khiển thiết bị điện, điện tử.
Xêm thêm tại: http://kythuatvatlieu.org/magazine/arduino.html
Post Reply
User avatar
support
Posts: 211
Joined: 05 Jan 2017, 09:34

Speed rotation measurement - Arduino

Post by support » 05 Oct 2017, 20:00

Xem thêm:

Code: Select all

// read RPM

volatile int rpmcount = 0;//see http://arduino.cc/en/Reference/Volatile
int rpm = 0;
unsigned long lastmillis = 0;

void setup(){
  Serial.begin(9600);
  attachInterrupt(0, rpm_fan, FALLING);//interrupt cero (0) is on pin two(2).
}

void loop(){

  if (millis() - lastmillis == 10000){ /*Uptade every one second, this will be equal to reading frecuency (Hz).*/
  detachInterrupt(0); //Disable interrupt when calculating

  rpm = rpmcount * 6; /* Convert frecuency to RPM, note: this works for one interruption per full rotation. For two interrups per full rotation use rpmcount * 30.*/
  
  Serial.print("RPM =\t"); //print the word "RPM" and tab.
  Serial.print(rpm); // print the rpm value.
  Serial.print("\t Hz=\t"); //print the word "Hz".
  Serial.println(rpmcount/10); /*print revolutions per second or Hz. And print new line or enter.*/
  
  rpmcount = 0; // Restart the RPM counter
  lastmillis = millis(); // Uptade lasmillis
  attachInterrupt(0, rpm_fan, FALLING); //enable interrupt
  }
}

void rpm_fan(){ /* this code will be executed every time the interrupt 0 (pin2) gets low.*/
  rpmcount++;
}

Code: Select all

int sensorPin = 2;
int counter = 0;
boolean sensorState = false;

void setup() 
{
  // setup serial - diagnostics - port
  Serial.begin(9600);
  pinMode(sensorPin, INPUT);
  digitalWrite(sensorPin, HIGH);
}

void loop() 
{
  if(magnetPresent(sensorPin) && !sensorState)
  {
    sensorState = true;
    printMessage("Magnet Present");
  }
  else if(!magnetPresent(sensorPin) && sensorState)
  {
    sensorState = false;
    printMessage("Magnet Gone");
  }
}

void printMessage(String message){
  counter++;

  Serial.print(counter);
  Serial.print(" ");
  Serial.println(message);
//  delay(1000);
}

boolean magnetPresent(int pin){
  return digitalRead(pin) == LOW;
}


Post Reply