Hello,
I have been working on an obstacle avoidance robot with TF-Mini.
I have completed 80% of my goals for said project.
Currently i want the servo to move 180 degrees continously e.g. sweep
But when i add the servo code to the main code it stops the L298N code and the actual movement of the robot.
Any help is appreciated.
Code Below:
#include <SoftwareSerial.h> //header file of software serial port
#include <Servo.h>
#include <NewPing.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define TRIGGER_PIN A1
#define ECHO_PIN A0
#define MAX_DISTANCE 200
Servo servo;
SoftwareSerial Serial1(8,9); //define software serial port name as Serial1 and define pin8 as RX and pin9 as TX
SoftwareSerial mySerial(11, 10);
//Ultrasonic Variables
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
boolean object;
float time = 0;
long duration, cm;
int distance = 0; //set distance value to 0
int pos = 0;
// Motor A connections
int enA = 7;
int in1 = 5;
int in2 = 6;
// Motor B connections
int enB = 2;
int in3 = 3;
int in4 = 4;
// Lidar Variables
int dist; //actual distance measurements of LiDAR
int strength; //signal strength of LiDAR
int check; //save check value
int i;
int uart[11]; //save data measured by LiDAR
const int HEADER=0x59; //frame header of data package
void setup()
{
Serial.begin(9600);
mySerial.begin(9600);
Serial1.begin(115200); //set bit rate of serial port connecting LiDAR with Arduino
servo.attach(12);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(TRIGGER_PIN,OUTPUT); //sets TRIGGER_PIN to OUTPUT
pinMode(ECHO_PIN,INPUT); //sets ECHO_PIN to INPUT
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
void loop()
{
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
servo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
servo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
//Lidar Code - TF-Mini
if (Serial1.available()) //check if serial port has data input
{
if (Serial1.read() == HEADER) //assess data package frame header 0x59
{
uart[0] = HEADER;
if (Serial1.read() == HEADER) //assess data package frame header 0x59
{
uart[1] = HEADER;
for (i = 2; i < 9; i++) //save data in 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)) //verify the received data as per protocol
{
dist = uart[2] + uart[3] * 256; //calculate distance value
strength = uart[4] + uart[5] * 256; //calculate signal strength value
Serial.print("distance = ");
Serial.print(dist); //output measure distance value of LiDAR
Serial.print('\t');
Serial.print("strength = ");
Serial.print(strength); //output signal strength value
Serial.print('\n');
//Bluetooth Module - HC-05
if(Serial1.available())
{
mySerial.println(dist);
}
}
}
}
}
//Get Ultrasonic Distance
float duration, distance;
digitalWrite(TRIGGER_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIGGER_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER_PIN, LOW);
duration = pulseIn(ECHO_PIN, HIGH);
distance = (duration / 2) / 29.1 ; // calculate distance in CM
if(distance >= 30) //Move Forward
{
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
analogWrite(enA, 130); //enA speed variable - Right Wheel
analogWrite(enB, 130); //enB speed variable - Left Wheel
delay(200);
}
if(distance <= 25) //Move Right
{
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
analogWrite(enA, 170); //enA speed variable - Right Wheel
analogWrite(enB, 130); //enB speed variable - Left Wheel
delay(200);
}
if(distance < 20) //Move Backward
{
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
analogWrite(enA, 130); //enA speed variable - Right Wheel
analogWrite(enB, 130); //enB speed variable - Left Wheel
delay(200);
}
}