06-04-2018 09:02 AM
Hi everyone !
I am trying to control the speed of a rotative system actionned by a DC motor.
Here is a little explanation of what I am trying to do :
So for now, with the code in the arduino and the labview program I can change the PWM value and get informations from the sensor (optical sensor with an encoder).
Here is the arduino code :
volatile byte counter;
int rpm;
long timeold;
int interruptPin = 0, pinPWM = 5;
int duration;
String command = "128";
void setup()
{
pinMode ( interruptPin, INPUT );
pinMode ( pinPWM, OUTPUT );
counter = 0;
rpm = 0;
timeold = millis();
attachInterrupt ( interruptPin, interruptCounter, FALLING );
Serial.begin ( 9600 );
Serial.setTimeout( 85 ); //to avoid waiting 1000ms between each change of the pwm value on labview
Serial.println ( "let's go" );
}
void loop()
{
if( counter >= 1 ){
detachInterrupt ( interruptPin );
/******************* TREATMENT OF THE ROTATION SPEED *******************/
rpm = 60000 / ( millis() - timeold );
Serial.write( rpm ); //this is supposed to be rpm but maybe for another post 😉
/******************* GETTING READY FOR ANOTHER ROUND ******************/
timeold = millis();
counter = 0;
attachInterrupt ( interruptPin, interruptCounter, FALLING );
}
else{
Serial.write( "no" ); //Not important, just because labview is waiting to read something
}
/******************* *** ACTUALISING THE PWM **********************/
command = Serial.readString();
analogWrite ( 5, command.toInt() );
}
void interruptCounter()
{
++counter;
}
What I want now is sending the value of the speed to labview by bluetooth but I am supposed to send a string and the value is an integer so I am a bit lost.
Should I send the datas like this : Serial.write( rpm ); so I read the ascii value and convert it (how ?) or maybe there is another way ? Maybe I could convert the value directly in the arduino so that the String is composed of the ascii characters corresponding to the right numbers ? Have I got to separe both bytes of the resulting string to treat them and then, add them ?
I am having a hard time to do it as I discovered labview and arduino 3 weeks ago.
The values must be on 16 bits (-32768 to 32767 or 0 to 65536 as the higher value is 2500 rpm)
The VI is attached.
Thanks for your help 🙂
Solved! Go to Solution.
06-04-2018 10:59 AM
Well to begin with the sequence structure you have is superfluous, the error cluster will control the data flow. Delite the flat sequence and your program will run exactly the same.
@nitneukThe values must be on 16 bits (-32768 to 32767 or 0 to 65536 as the higher value is 2500 rpm)
I would separate the 16 bit (unsigned)integer into two 8 bit integers (High byte/Low byte)
Then convert each 8bit value to the corresponding string and concatenate them.
Now send the "two letter string" to your Arduino.
In your Arduino code convert the string to its numeric value using the "word()" command.
06-06-2018 03:54 AM
Thank you very much for your answer, but it's exactly the opposite that I want to do.
I haven't got any problem to convert the value incoming from labview into an integer.
But what I can't do is converting the string incoming from arduino into an integer.
The optical sensor generates an interruption in the arduino at each step of the encoder.
When the counter reach a certain value, I detach the interruption, calculate the rpm value and then... I don't know what to do. The final goal is to get the rpm (integer) value in labview to use a pid to generate the next pwm command.
I don't even know in what format I should send the value. Because if the value is 2000, I have to send 4 characters string (which is not a problem) and then split the resulting string in 4 and multiplying each char by a value (1000, 100, 10, 1). But I could split the integer directly in the arduino into two U8, change them into chars and send those. So 1500 would become SI NUL (ASCII 015 is shift and 000 is NUL). Then, I split the 2 chars string, convert the characters, multiply them and add them together. Is that the equivalence of what you suggested ? If so, how can I do ?
I can't split the string in the arduino and send the characters one after one because it would take to much time...
Then, could you just explain to me the best way to do please ?
06-06-2018 08:00 AM
So your question is how, in LabVIEW, to convert an ASCII string of the decimal representation of an integer (e.g. the string "2000", received from your Arduino) into an actual integer value? The most straightforward is the Decimal String to Number function.
06-06-2018 08:26 AM
Yes, that's it thanks ! It is so simple now that it works.
The problem is that I tried it with Serial.write() instead of Serial.print() and of course it didn't work...
Thank you very much !