LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Make changes in Arduino code to use Serial communication

Hello Community I am using following Arduino code to run NEMA 23 stepper motor.

What I want to do is to change the code of Arduino to get spd values from LabVIEW using VISA serial communication. Right now spd values are getting from potentiometer at analogue pin 0 at Arduino.  Can anyone help me?

 

// Defin pins

int reverseSwitch = 2; // Push button for reverse
int driverPUL = 7; // PUL- pin
int driverDIR = 6; // DIR- pin
//int spd = A0; // Potentiometer

// Variables

int pd = 500; // Pulse Delay period
boolean setdir = LOW; // Set Direction

// Interrupt Handler

void revmotor (){

setdir = !setdir;

}


void setup() {

pinMode (driverPUL, OUTPUT);
pinMode (driverDIR, OUTPUT);
attachInterrupt(digitalPinToInterrupt(reverseSwitch), revmotor, FALLING);

}

void loop() {
long int spd = Serial.parseInt();
pd = map((analogRead(spd)),0,1023,2000,50);
digitalWrite(driverDIR,setdir);
digitalWrite(driverPUL,HIGH);
delayMicroseconds(pd);
digitalWrite(driverPUL,LOW);
delayMicroseconds(pd);

}

0 Kudos
Message 1 of 3
(118 Views)

Change your Arduino sketch such that it stores a speed value in a variable and uses that to update the stepper motor.

Have the serial reads use a message-handler-type arrangement to update that variable.

 

Here's an (unrelated to motors, but hopefully illuminating) example for power supplies I have:

// String for serial inputs via events
String inputString = "";
bool stringComplete = false;

void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag so the main loop can
    // do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    }
  }
}

void handleInput(String inputStr, PowerSupplyPins ps, PowerSupplyPins ps2) {
  int input = inputStr.toInt();
  //digitalWrite(lines[input], !lineState[input]);
  //lineState[input] = !lineState[input];
  switch(input) {
    case 0:
      // Toggle power output
      setPowerEnable(ps);
      break;
    case 1:
    case 2:
    case 3:
    case 4:
      // Set preset
      setPreset(ps, input-1);
      break;
    // ... bunch more cases here
    default:
      Serial.println("Unexpected input...");
      break;
  }
}

void setup() {
  // put your setup code here, to run once:
  
  // Initialize the main serial port
  Serial.begin(9600);
  inputString.reserve(200); // 200 bytes reserved
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  if (stringComplete) {
    // Handle command
    handleInput(inputString, psADC, psRotEnc);
    
    // Reset the string for the next command
    inputString = "";
    stringComplete = false;
  }
}

 

You'd want to add more in the loop() function to handle your speeds, but hopefully this gives you an idea.


GCentral
0 Kudos
Message 2 of 3
(73 Views)

 

I would avoid the Serial.parseint function. I don't know how it determines if a new number is sent or reading from the previous iteration of your LabVIEW serial write. Use the readStringUnitl(13) instead to set carriage return as your termination character.

 

 

void loop() {

if (Serial.available() > 0) {                         //Serial data available
  String spd_string = Serial.readStringUntil(13);       //Read until 13 (carriage return)
  long int spd_int = spd_string.toInt();                //Convert string to number
  pd = map((analogRead(spd_int)),0,1023,2000,50);       //Only update when new value is sent
  }

digitalWrite(driverDIR,setdir);
digitalWrite(driverPUL,HIGH);
delayMicroseconds(pd);
digitalWrite(driverPUL,LOW);
delayMicroseconds(pd);

}

 

On the LabVIEW side concatenate the string you send with the carriage return constant from the string palette just before the serial write. You could also put that code in an event structure to avoid unnecessary serial writes.

0 Kudos
Message 3 of 3
(53 Views)