Sending RPM from arduino to C#

Hi All

I am currently designing a data logger (C#) that reads data from an arduino UNO by recieving it serially. It consists of several sensor including a VR sensor for reading the pulses to be able of showing RPM on the C# GUI. The problem that I have is that i am not seeing and values of RPM in the C# software (always a value of 0). I am using an interrupt to enable the RPM counts. I was reading that the UNO will disable the interrupt of digital pin 2 whenever the data is sent serially ( is this true), also is there any way of calculating the VR sensor counts without using interrupts. I have posted all the code of that i have been worked on for ages now.

BTW the transferring part of the other analog sensors works perfectly. I'll appreciate all your help!!

////////////////////////////////////////////////////////////

//Setup message bytes byte inputByte_0; byte inputByte_1; //////// used for sending data as a number of bytes byte inputByte_2;

// read RPM byte half_revolutions; volatile byte rpm; unsigned long lastmillis = 0;

void setup() {

Serial.begin(9600); attachInterrupt(0, rpm_fan, FALLING); }

void loop() {

if (Serial.available()> 2) { inputByte_0 = Serial.read(); delay(10); inputByte_1 = Serial.read(); delay(10); inputByte_2 = Serial.read(); delay(10);

} //Check for start of Message if(inputByte_0 == 4) { //Detect Command type switch (inputByte_1) { case 8: //Set PIN and value switch (inputByte_2) { case 16:

Serial.print("Zoqtek");

break; {

case 82: // show RPM {

getRPM();

Serial.flush(); Serial.print (rpm);

break;

}

}

//Clear Message bytes inputByte_0 = 0; inputByte_1 = 0; inputByte_2 = 0;

}

}

} void rpm_fan(){ half_revolutions++; }

byte getRPM() {

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

return rpm; noInterrupts(); half_revolutions = 0; // Restart the RPM counter lastmillis = millis(); // Uptade lasmillis interrupts(); attachInterrupt(0, rpm_fan, FALLING); //enable interrupt

} }

Please read Nick Gammon's two posts at the top of this Forum, especially the section on using code tags. It will help us help you. Also, your switch statement is a little strange.

Thanks for the reply. I am new to this forum and I'll arange all my mistakes. Regarding the switch statement it send mulitple bytes containg several sensor readings. Some switch cases are not listed in the code above, and if necessary I will post them too

    Arduino Forum
    > Using Arduino
    > Programming Questions
    > Sending RPM from arduino to C#
    Unread Posts Updated Topics

Reply Mark unread Print No Alerts or Emails
Go Down
Pages: [1]
Topic: Sending RPM from arduino to C# (Read 23 times) previous topic - next topic
albzoq

    avatar_albzoq
    *
    Newbie
    Posts: 2
    Karma: 0  [add]
    Logged

Sending RPM from arduino to C#
Today at 01:37 pm
Hi All

I am currently designing a data logger (C#) that reads data from an arduino UNO by recieving it serially. It consists of several sensor including a VR sensor for reading the pulses to be able of showing RPM on the C# GUI. The problem that I have is that i am not seeing and values of RPM in the C# software (always a value of 0). I am using an interrupt to enable the RPM counts. I was reading that the UNO will disable the interrupt of digital pin 2 whenever the data is sent serially ( is this true), also is there any way of calculating the VR sensor counts without using interrupts. I have posted all the code of that i have been worked on for ages now.

BTW the transferring part of the other analog sensors works perfectly. I'll appreciate all your help!!

 ////////////////////////////////////////////////////////////


//Setup message bytes
byte inputByte_0;
byte inputByte_1;  //////// used for sending data as a number of bytes
byte inputByte_2;


// read RPM
 byte half_revolutions;
 volatile byte rpm;
 unsigned long lastmillis = 0;


 void setup() {



   Serial.begin(9600); 
   attachInterrupt(0, rpm_fan, FALLING);
}


void loop() {




  if (Serial.available()> 2)
  {
    inputByte_0 = Serial.read();
    delay(10);   
    inputByte_1 = Serial.read();
    delay(10);     
    inputByte_2 = Serial.read();
    delay(10);   


} 
  //Check for start of Message
  if(inputByte_0 == 4)
  {       
       //Detect Command type
       switch (inputByte_1)
       {
          case 8:
             //Set PIN and value
             switch (inputByte_2)
            {
              case 16:
               
               Serial.print("Zoqtek");
               
                break;
              {

case 82: // show RPM
{
   

 getRPM();

 Serial.flush();
 Serial.print (rpm);

 break;



  }


}

           
           
        //Clear Message bytes
        inputByte_0 = 0;
        inputByte_1 = 0;
        inputByte_2 = 0;
       
       }




}

}
 void rpm_fan(){
  half_revolutions++;
 }


byte getRPM()
{

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


 return rpm;

Several problems here. First, that should be >= 10, not == 10. If you miss a millisecond, you will never be exactly 10 milliseconds apart ever again.

Second, the return statement says "We are done; skip the rest of this shit." I'm sure that is NOT what you expect to do. Detaching the interrupt handler is just plain silly. Do NOT do that. You can suspend all interrupts, as you try to do later, but detaching and reattaching the handler is just plain wrong.

Thanks for all your help PualS. I've managed to solve my problem and now the value of RPM is being displayed correctly on the C# application. You saved me a lot of time as i need to display my project for my final thesis next week. thanks alot:)