Passing Larger Value to my Arduino Function

Mod edit space added to VB .NET

i need My UDP from VB .NET to pass a Larger Number like 11223 into my Function in the Arduino in which now its only talking 1 2 3 4 5 ... it would be nice to take the Argument MoveAtVelocity($RPM);
RPM would come from my Converted Math in VB .NET Can anyone help me out, Thanks so much

void loop() {

    motor.EnableRequest(true);
    // SerialPort.SendLine("Motor Enabled");

    int packetSize = Udp.parsePacket();
    if (packetSize > 0) {
        Serial.print("Remote IP: ");
	Serial.print(Udp.remoteIP());
        Serial.print(" ");
        Serial.print(Udp.remotePort());
        Serial.print(" ");
         
        int bytesRead = Udp.read(packetReceived, MAX_PACKET_LENGTH);
        Serial.write(packetReceived, bytesRead);
        Serial.println();

        Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
	for (int i = 0; i < bytesRead; i++) {
	  Udp.write(toupper(packetReceived[i]));
    // Udp.write(toupper(SETRPM)); 
	}
        Udp.endPacket();
  
	if (packetReceived[0] == '0') digitalWrite(LED_BUILTIN, LOW);
  if (packetReceived[0] == '0') MoveAtVelocity(0);
  // if (packetReceived[0] == '3') MoveAtVelocity(22450); //674 RPM
  // if (packetReceived[0] == '3') MoveAtVelocity(11225); //337 RPM
  //if (packetReceived[0] == '3') MoveAtVelocity(9225); //227 RPM
  if (packetReceived[0] == '3') MoveAtVelocity(8333);  //250 RPM
  if (packetReceived[0] == '4') MoveAtVelocity(11667); //350 RPM
  if (packetReceived[0] == '5') MoveAtVelocity(12667); //380 RPM
  if (packetReceived[0] == '6') MoveAtVelocity(13667); //410 RPM
  if (packetReceived[0] == '7') MoveAtVelocity(15000); //450 RPM
  // if (packetRecieved[0] == '3') Serial.print(SETRPM);
	if (packetReceived[0] == '1') digitalWrite(LED_BUILTIN, HIGH);
    }

    delay(10);
}

/*------------------------------------------------------------------------------
 * MoveAtVelocity
 *
 *    Command the motor to move at the specified "velocity", in steps/second.
 *    Prints the move status to the USB serial port
 *
 * Parameters:
 *    int velocity  - The velocity, in step steps/sec, to command
 *
 * Returns: None
 *------------------------------------------------------------------------------*/
bool MoveAtVelocity(int32_t velocity) {
    // Check if an alert is currently preventing motion
    if (motor.StatusReg().bit.AlertsPresent) {
        SerialPort.SendLine("Motor status: 'In Alert'. Move Canceled.");
        return false;
    }
    SerialPort.Send("Commanding velocity: ");
    SerialPort.SendLine(velocity);
    // Command the velocity move
    motor.MoveVelocity(velocity);
    // Waits for the step command to ramp up/down to the commanded velocity. 
    // This time will depend on your Acceleration Limit.
    SerialPort.SendLine("Ramping to speed...");
    while (!motor.StatusReg().bit.AtTargetVelocity) {
        continue;
    }
    SerialPort.SendLine("At Speed");
    return true;
}
//------------------------------------------------------------------------------

Not sure I understand what you are asking.

Are you saying that currently you receive a single byte (0-9) in the UDP packet and you then use that to call your routine to set the velocity.

But what you would like is to pass multiple bytes representing an RPM (say "380") and you would like to pass that to you routine?

Is your problem how to get the multiple bytes from the packet, and turn them into a number, which you can then use?

the code you have is receiving an UDP packet and sending it back after shifting to upper case the text bytes

Who is sending in the commands and can you send something else than '0', '1', '2', ... ?
➜ Could you actually send "11223" from the VB side ?

Mod edit space added to VB .NET

So my VB .NET looks like this , i am only taking 2, 3, 4, 5 etc so what I'm trying to accomplish is a user puts in 350 or 120 as in RPM it Goes into my Formula rpmx = tbSend.Text / 60 * 2000
in which will get me my velocity to UDP into the Arduino now when the Arduino receives this value it needs to pass that value into the Arduino

if (packetReceived[0] == '4') MoveAtVelocity(12667); //350 RPM

'   VB.NET
   Private Sub sendBut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sendBut.Click
       Dim toSend As String = tbSend.Text  'tbSend is a textbox,
       ' Formula 500RPM take 500/60*2000 

       Dim data() As Byte = Encoding.ASCII.GetBytes(toSend) 'Convert string to bytes
       sendingClient.Send(data, data.Length)               'Send bytes
   End Sub

the Arduino is only seeing the First Number of the Value

  if (packetReceived[0] == '4') MoveAtVelocity(12667); //350 RPM

FULL Arduino UDP Code

void loop() {

    motor.EnableRequest(true);
    // SerialPort.SendLine("Motor Enabled");

    int packetSize = Udp.parsePacket();
    if (packetSize > 0) {
        Serial.print("Remote IP: ");
	Serial.print(Udp.remoteIP());
        Serial.print(" ");
        Serial.print(Udp.remotePort());
        Serial.print(" ");
         
        int bytesRead = Udp.read(packetReceived, MAX_PACKET_LENGTH);
        Serial.write(packetReceived, bytesRead);
        Serial.println();

        Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
	for (int i = 0; i < bytesRead; i++) {
	  Udp.write(toupper(packetReceived[i]));
    // Udp.write(toupper(SETRPM)); 
	}
        Udp.endPacket();
  
	if (packetReceived[0] == '0') digitalWrite(LED_BUILTIN, LOW);
  if (packetReceived[0] == '0') MoveAtVelocity(0);
  // if (packetReceived[0] == '3') MoveAtVelocity(22450); //674 RPM
  // if (packetReceived[0] == '3') MoveAtVelocity(11225); //337 RPM
  //if (packetReceived[0] == '3') MoveAtVelocity(9225); //227 RPM
  if (packetReceived[0] == '3') MoveAtVelocity(8333);  //250 RPM
  if (packetReceived[0] == '4') MoveAtVelocity(12667); //350 RPM
  if (packetReceived[0] == '5') MoveAtVelocity(15667); //350 RPM
  if (packetReceived[0] == '7') MoveAtVelocity(17667); //380 RPM
  if (packetReceived[0] == '8') MoveAtVelocity(19667); //410 RPM
  if (packetReceived[0] == '450') MoveAtVelocity(15000); //450 RPM
  if (packetReceived[0] == '16000') MoveAtVelocity(16000); 
    Serial.print (packetReceived[0]); 
  if (packetReceived[0] == '19000') MoveAtVelocity(19000);  
  // if (packetRecieved[0] == '3') Serial.print(SETRPM);
	if (packetReceived[0] == '1') digitalWrite(LED_BUILTIN, HIGH);
    }

    delay(10);
}

/*------------------------------------------------------------------------------
 * MoveAtVelocity
 *
 *    Command the motor to move at the specified "velocity", in steps/second.
 *    Prints the move status to the USB serial port
 *
 * Parameters:
 *    int velocity  - The velocity, in step steps/sec, to command
 *
 * Returns: None
 *------------------------------------------------------------------------------*/
bool MoveAtVelocity(int32_t velocity) {
    // Check if an alert is currently preventing motion
    if (motor.StatusReg().bit.AlertsPresent) {
        SerialPort.SendLine("Motor status: 'In Alert'. Move Canceled.");
        return false;
    }
    SerialPort.Send("Commanding velocity: ");
    SerialPort.SendLine(velocity);
    // Command the velocity move
    motor.MoveVelocity(velocity);
    // Waits for the step command to ramp up/down to the commanded velocity. 
    // This time will depend on your Acceleration Limit.
    SerialPort.SendLine("Ramping to speed...");
    while (!motor.StatusReg().bit.AtTargetVelocity) {
        continue;
    }
    SerialPort.SendLine("At Speed");
    return true;
}

i would like to format this like so if possible

MoveAtVelocity('packetReceived' )

This is not the full code. Please post the entire sketch... you have left out important detail like the variable declarations.

Full Code Project , Arduino

#include "ClearCore.h"
#include "MotorManager.h"
#include <Ethernet.h>
#include <EthernetUdp.h>
/* LED Through UDP */

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(192, 168, 111, 177);

unsigned int localPort = 8888;
unsigned int SETRPM;
unsigned int vol;
bool MoveAtVelocity(int32_t velocity);
#define MAX_PACKET_LENGTH 100
#define SETRPM
#define vol 

#define motor ConnectorM0
#define SerialPort ConnectorUsb
int32_t accelerationLimit = 8000; // 100000
char packetReceived[MAX_PACKET_LENGTH];

EthernetUDP Udp;
bool usingDhcp = false;

void setup() {
   //-------------------------------------------------------------------
    // step and direction applications.
    MotorMgr.MotorInputClocking(MotorManager::CLOCK_RATE_NORMAL);
    // Sets all motor connectors into step and direction mode.
    MotorMgr.MotorModeSet(MotorManager::MOTOR_ALL,Connector::CPM_MODE_STEP_AND_DIR);
    motor.HlfbCarrier(MotorDriver::HLFB_CARRIER_482_HZ);
    // Set the maximum acceleration for each move
    motor.AccelMax(accelerationLimit);
   //-------------------------------------------------------------------
    Serial.begin(115200);

    delay(3000); // wait so serial port comm is open

    if (usingDhcp) {
        int dhcpSuccess = Ethernet.begin(mac);
        if (dhcpSuccess) {
            Serial.print("DHCP ");
	    Serial.println(Ethernet.localIP());
        }
        else {
            Serial.println("DHCP failed");
            Serial.println("Try again using a manual configuration...");
            while (true) {
                // UDP will not work without a configured IP address.
                continue;
            }
        }
    }
    else {
        Ethernet.begin(mac, ip);
        Serial.print("IP   ");
	Serial.println(Ethernet.localIP());
    }

    while (Ethernet.linkStatus() == LinkOFF) {
        Serial.println("The Ethernet cable is unplugged...");
        delay(500);
    }

    Udp.begin(localPort);
    pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {

    motor.EnableRequest(true);
    // SerialPort.SendLine("Motor Enabled");

    int packetSize = Udp.parsePacket();
    if (packetSize > 0) {
        Serial.print("Remote IP: ");
	Serial.print(Udp.remoteIP());
        Serial.print(" ");
        Serial.print(Udp.remotePort());
        Serial.print(" ");
         
        int bytesRead = Udp.read(packetReceived, MAX_PACKET_LENGTH);
        Serial.write(packetReceived, bytesRead);
        Serial.println();

        Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
	for (int i = 0; i < bytesRead; i++) {
	  Udp.write(toupper(packetReceived[i]));
    // Udp.write(toupper(SETRPM)); 
	}
        Udp.endPacket();
  
 // MoveAtVelocity(packetReceived); 

	if (packetReceived[0] == '0') digitalWrite(LED_BUILTIN, LOW);
  if (packetReceived[0] == '0') MoveAtVelocity(0);
  // if (packetReceived[0] == '3') MoveAtVelocity(22450); //674 RPM
  // if (packetReceived[0] == '3') MoveAtVelocity(11225); //337 RPM
  //if (packetReceived[0] == '3') MoveAtVelocity(9225); //227 RPM
  if (packetReceived[0] == '3') MoveAtVelocity(8333);  //250 RPM
  if (packetReceived[0] == '4') MoveAtVelocity(12667); //350 RPM
  if (packetReceived[0] == '5') MoveAtVelocity(15667); //350 RPM
  if (packetReceived[0] == '7') MoveAtVelocity(17667); //380 RPM
  if (packetReceived[0] == '8') MoveAtVelocity(19667); //410 RPM

    Serial.print (packetReceived[0]); 
 
  // if (packetRecieved[0] == '3') Serial.print(SETRPM);
	if (packetReceived[0] == '1') digitalWrite(LED_BUILTIN, HIGH);
    }

    delay(10);
}

/*------------------------------------------------------------------------------
 * MoveAtVelocity
 *
 *    Command the motor to move at the specified "velocity", in steps/second.
 *    Prints the move status to the USB serial port
 *
 * Parameters:
 *    int velocity  - The velocity, in step steps/sec, to command
 *
 * Returns: None
 *------------------------------------------------------------------------------*/
bool MoveAtVelocity(int32_t velocity) {
    // Check if an alert is currently preventing motion
    if (motor.StatusReg().bit.AlertsPresent) {
        SerialPort.SendLine("Motor status: 'In Alert'. Move Canceled.");
        return false;
    }
    SerialPort.Send("Commanding velocity: ");
    SerialPort.SendLine(velocity);
    // Command the velocity move
    motor.MoveVelocity(velocity);
    // Waits for the step command to ramp up/down to the commanded velocity. 
    // This time will depend on your Acceleration Limit.
    SerialPort.SendLine("Ramping to speed...");
    while (!motor.StatusReg().bit.AtTargetVelocity) {
        continue;
    }
    SerialPort.SendLine("At Speed");
    return true;
}

Thanks.

packetReceived is 100 bytes, but currently are you just using 1 byte?

What does this produce in the monitor?

    Serial.write(packetReceived, bytesRead);
    Serial.println();

You could convert an incoming string into a value if you want to pass RPM.

Below the 2 lines above...

    packetReceived[bytesRead] = '\0';   // string terminator
    uint16_t RPMValue = atoi(packetReceived);  // Convert to integer

It's not meaningful in C. Single quotes are only valid with single characters. What are you trying to do?

well my RPM value is Converted from VB.NET my Motor Class in Arduino can only take a Velocity Movement, so if the User Enters in 360 as an RPM it Gets Converted to Velocity

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim rpmx As Int16
        rpmx = tbSend.Text / 60 * 2000
        Label5.Text = rpmx

Mod edit space added to VB .NET

What is being passed to the Arduino?
a). Velocity?
b). RPM?
c). A single byte. You are currently just looking at the first byte. '0' , '1', '2', '3' etc.

here is a Crude diagram ,
ClearMotor.drawio

I don't think you should look at any other problems until you fix your C syntax and logic.

  1. A byte can only hold a maximum value of 255
  2. Single quotes indicate a single ASCII character

Yeah it's Fixed, i know that is wrong i was just testing it but Doesn't work for the fact im only looking at if (packetReceived[0] == '19000') MoveAtVelocity(19000); // Value (1)

so you send an ASCII string

       Dim data() As Byte = Encoding.ASCII.GetBytes(toSend) 'Convert string to bytes
       sendingClient.Send(data, data.Length)               'Send bytes

that's what you need to receive and compare - multiple bytes not just one

Any time you update your code, you should re-post it in a new message.

1 Like

Is your code working? You can mark the thread "solved".

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.