I'm a very inexperienced coder with only two classes in High School as my background. I found two codes on the internet, one pertaining to a stepper motor and one for a Lidar. I am using an Arduino Uno and I am having trouble combining the two codes. I did some research and found that it could be either the SoftwareSerial, the baud rate or my terrible coding. I don't believe I can use a Serial1 in my code since I am using an Uno but I do not know how to combine the two without using it. I want the code to take a Lidar reading every time the stepper motor moves(every time it clicks) and the Lidar is really lagging behind. After a couple tests I realized the code was barely getting past the first if statement of the Lidar code. If someone could look at my code and give a suggestion or two, that would be a great help!
#include<SoftwareSerial.h>// soft serial port header file
SoftwareSerial Serial1(8, 9); // define the soft serial port as Serial1, pin8 as RX, and pin9 as TX
int dist;// LiDAR actually measured distance value
int strength;// LiDAR signal strength
int check;// check numerical value storage
int i;
int uart[9];// store data measured by LiDAR
const int HEADER = 0x59; // data package frame header
//Declare pin functions on Redboard
#define stp 2
#define dir 3
#define MS1 4
#define MS2 5
#define EN 6
//Declare variables for functions
char user_input;
int x;
int y;
int state;
void setup() {
pinMode(stp, OUTPUT);
pinMode(dir, OUTPUT);
pinMode(MS1, OUTPUT);
pinMode(MS2, OUTPUT);
pinMode(EN, OUTPUT);
resetEDPins(); //Set step, direction, microstep and enable pins to default states
Serial.begin(9600); //Open Serial connection for debugging
Serial.println("Begin motor control");
Serial.println();
Serial.println("Press s to start");
Serial.println();
// Serial1.begin(115200);//set the Baud rate of LiDAR and Arduino serial port
}
void loop() {
// put your main code here, to run repeatedly:
while (Serial.available()) {
user_input = Serial.read();
digitalWrite(EN, LOW); //Pull enable pin low to allow motor control
if (user_input == 's')
{
ForwardBackwardStep();
}
else
{
Serial.println("Invalid option entered.");
}
resetEDPins();
}
}
void resetEDPins()
{
digitalWrite(stp, LOW);
digitalWrite(dir, LOW);
digitalWrite(MS1, LOW);
digitalWrite(MS2, LOW);
digitalWrite(EN, HIGH);
}
//Forward/reverse stepping function
void ForwardBackwardStep()
{
for (x = 1; x < 3; x++)
// the amount of reverses the motor makess
{
//Read direction pin state and change it
state = digitalRead(dir);
if (state == HIGH)
{
digitalWrite(dir, LOW);
}
else if (state == LOW)
{
digitalWrite(dir, HIGH);
}
for (y = 1; y < 10 ; y++) //y<24
//amount of ticks
{
digitalWrite(stp, HIGH); //Trigger one step
delay(150);
digitalWrite(stp, LOW); //Pull step pin low so it can be triggered again
delay(150);
if (Serial1.available())//check whether the serial port has data input
{
if(Serial1.read()==HEADER)// determine data package frame header 0x59
{
uart[0]=HEADER;
if(Serial1.read()==HEADER)//determine data package frame header 0x59
{
uart[1]=HEADER;
for(i=2;i<9;i++)// store data to array
{
uart[i]=Serial1.read();
}
check=uart[0]+uart[1]+uart[2]+uart[3]+uart[4]+uart[5]+uart[6]+uart[7];
if(uart[8]==(check&0xff))// check the received data as per protocols
{
dist=uart[2]+uart[3]*256;// calculate distance value
Serial.print("dist = ");
Serial.print(dist);// output LiDAR tests distance value
Serial.print('\t');
Serial.print('\n');
}
}
}
} //end of Lidar code
}
}
}