Hello, I am working on a project that will be integrated within matlab to analyze some data that I will get using alternate components. I am using an arduino board to turn a stepper motor a predetermined distance which will then gather more data at each position. Trying to run the arduino board using matlab took too long for the program to send all commands to the arduino board and it needed to run for 23 iterations. I then decided to program an arduino board to wait for a "trigger" from another arduino board that will be connected to the matlab code. That way when we send a command using to the first arduino board, it only sends a simple code to it that will then communicate to the other arduino using the SoftwareSerial library.
I am not very strong in coding arduino, but I managed to get information from various examples I have found and pieced them together to create my current examples. I am not looking to have someone do the project for me I would just like some help or guidance to know whether what i am doing is correct and why it will or will not work.
To simplify the explanation I will call the arduino board communicating with matlab as board 1 and the arduino board running the stepper motor as board 2.
For board 1, I am trying to program it using arduino IDE just to create a loop, that way I will know if what I am attempting in terms of using it as a "trigger" will work. If I can get it working properly, then I will integrate it into matlab with the same idea. Here is the code:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2,3); // RX, TX
String incoming = " ";
void setup() {
// open serial communications and wait for signal
Serial.begin(57600);
// wait for connection from serial port
while(!Serial){
;
}
// set the data rate for the SoftwareSerial port
mySerial.begin(4800);
Serial.println("begin");
}
void loop() {
// Write to the serial port which will be read by second arduino board
if (Serial.available())
{
incoming = Serial.readString();
if (incoming == "begin");
{
mySerial.write("turn");
}
}
delay(10000);
Serial.println("begin");
}
For board 2, I am trying to have it read from the serial port that I created in board 1 and recognize what was read to trigger the stepper motion. Here is the code:
#include <SoftwareSerial.h> // including Serial Port Library
#include <Stepper.h> // including stepper motor library
// defining pins section
int stepIN1Pin = 8;
int stepIN2Pin = 9;
int stepIN3Pin = 10;
int stepIN4Pin = 11;
int stepsPerRotation = 1000; // amount of steps per revolution
String ComIn = " ";
SoftwareSerial mySerial(2, 3); // RX, TX
Stepper myStepper (stepsPerRotation, stepIN1Pin, stepIN2Pin, stepIN3Pin, stepIN4Pin);
void setup()
{
// Open serial communications
Serial.begin(57600);
// set the data rate for the SoftwareSerial port
mySerial.begin(4800);
// Set stepper motor speed
myStepper.setSpeed(25);
}
void loop()
{ // run over and over
if (mySerial.available())
{
ComIn = mySerial.readString();
if (ComIn == "turn")
{
myStepper.step(stepsPerRotation);
mySerial.write("Moving\n");
}
}
}
I hope someone understands what I am attempting to do and is able to help. Thank you so much for your time.