Writing data from serial communication on a digital pin

Hello,

I nned to write the data obtained by arduino via serial communication on a digital pin. The digital pin will be connected to run a stepper motor. Can anyone help me with the code? I have attached two seperate codes - one for serial communication and other one to run a stepper motor. I dont know how to combine these two. Please help.

Stepper_motor_control_1.ino (758 Bytes)

SM_arduino_latest_28dec.ino (581 Bytes)

You should embed your code using code tags instead of attaching. Most ppl will not download the code and review it.

I don't think your Serial program is a suitable starting place. Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

Also, if you want to receive data and make a motor move it would be best not to use delay() or delayMicroseconds(). Have a look at the second example in Simple Stepper Code and, more generally, have a look at how millis() is used to manage timing without blocking in Several Things at a Time.

And see Using millis() for timing. A beginners guide if you need more explanation.

It is also not clear from your Original Post what message you want to send to the Arduino or precisely what it is to do with the data it receives

...R

Let me explain in detail.

I am using a cRIO controller and program it in LabVIEW to give me the angle with which the stepper motor should rotate. This value of angle is sent to the arduino via a serial communication and that will further be sent to the stepper motor to rotate.

At present, I am able to receive the angle data from cRIO to arduino accurately. I am confused with how to transfer this data to the Pulse Pin of the stepper motor.

In your Arduino Sketch you will need to convert the received angle to a number of ticks for the servo. Something like this:

int angle = receiveNewAngle();
int ticks = ConvertAngleToTicks(angle);
void SendTicksToServo(ticks);

Most likely you will need to keep track of the current angle of the servo, because the servo uses relative positioning (ticks) instead of absolute (angle).

np_iitm: At present, I am able to receive the angle data from cRIO to arduino accurately. I am confused with how to transfer this data to the Pulse Pin of the stepper motor.

Post the program that receives the angle and give us some examples of the angles you want to work with.

Have you some code to initialize the position of the stepper motor - to make it go to the HOME or ZERO position when the Arduino starts?

Do you know how many steps are needed to achieve the angle you want?

...R

The stepper motor works with 1.8degree/steps and number of steps are 200 by default. I need to work with angles 5 degrees to 16 degrees.

I am using the DIRECTION and ENABLE control directly from cRIO control. Only the pulses are programmed in arduino.

The code that receives the angle values is as below:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX, TX

void setup() {
// Open serial communications and wait for port to open:

Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}

mySerial.begin(9600);

}

void loop() {

if (mySerial.available()) {
Serial.write(mySerial.read());
}
if (Serial.available()) {
mySerial.write(Serial.read());
}

delay (500);
}

np_iitm:
The stepper motor works with 1.8degree/steps and number of steps are 200 by default. I need to work with angles 5 degrees to 16 degrees.

A stepper motor that works in steps of 1.8° cannot move exactly 5° or 16°. The nearest it will get is 5.4 and 16.2. Microstepping would get you a bit closer.

Your Serial program is not saving the received value in a variable. Indeed it is not actually receiving the numbers at all it is just displaying characters on the Serial Monitor.

Study the links I gave you in Reply #2 - especially the first one.

…R

The accuracy in the angle can be compromised, so i did not involve microstepping in it. Can u tell me how to modify the code to receive the angle value and write to the stepper motor.

np_iitm: Can u tell me how to modify the code to receive the angle value and write to the stepper motor.

Have you studied my examples?

Have you tried to incorporate one of them into your stepper program?

...R

Yes, I did go through the codes and tutorials.

  1. Regarding receiving the data

The code you have explained uses a serial communication whereas i use softwareserial code i.e. different RX TX ports. So, I am not sure where do I put the arguement.

#include <SoftwareSerial.h>

char angle;

SoftwareSerial mySerial(10, 11); // RX, TX

void setup() {

Serial.begin(9600);
mySerial.begin(9600);

}

void loop() {

if (mySerial.available()) {
Serial.write(mySerial.read());
}
if (Serial.available()) {
angle = mySerial.write(Serial.read());
}

delay (500);
}

I am very new to arduino, so please pardon me for silly doubts.

np_iitm: The code you have explained uses a serial communication whereas i use softwareserial code i.e. different RX TX ports. So, I am not sure where do I put the arguement.

It will work perfectly well with SoftwareSerial if you make appropriate changes - change Serial to mySerial

...R