I have been making this project for college and I would love some feedback on if the code would work at the moment and what I could add? The project includes rear wheel drive with a servo to steer the front wheels, as well as one to rotate a stalker function I have named as "objectFollow" in my code.
#include <Servo.h>. //include the servo library
Servo steeringServo; //label the steering servo
Servo sensorServo; //label the sensor config servo
const int joyDrive = A3; //label the input pin for fwd/bckw drive
const int joySteer = A2; //label the input pin for steering
const int joyHorn = A1; //label the input pin for the horn
const int objFollow = 8; //label the input pin for object following
const int buzzer = 7; //label the output pin for the horn
int sound = 1500; //state the value of sound
const int objFollowLED = 6; //label the pin for the object follow indicator LED
const int sensorEcho = 9; //label the pin for US Sensor echo
const int sensorTrig - 10; //label the pin the US Sensor trigger
const int motors = 5; //label the output pin for the motors
int driveVal; //state variable name for fwd/ backw drive
int steerVal; //state variable name for the steering
int echoVal; //state the variable to store value of distance
void setup() {
steeringServo.attach(12); //attach steering servo to pin 12
sensorServo.attach(11); //attach sensor config servo to pin 11
pinMode(joyDrive, INPUT); //declare the joyX as input
pinMode(joySteer, INPUT); //declare the joyY as input
pinMode(joyHorn, INPUT); //declare the joyButton as input
pinMode(objFollow, INPUT_PULLUP); //declare the object follow button as input
pinMode(sensorEcho, INPUT); //declare the echo pin as input
pinMode(buzzer, OUTPUT); //declare the buzzer as output
pinMode(objFollowLED, OUTPUT); //declare the object follow indicator as output
pinMode(sensorTrig, OUTPUT); //declare the trigger pin as an output
driveCheck(); //call the drive check function
}
void loop() {
driveSeq(); //call the driving sequence
int driveVal = map(analogRead(joyDrive), 0. 1023. 0. 180); //set and map the value of joyX
int steerVal = map(analogRead(joySteer), 0, 1023, 0, 180); //set and map the value of joyY
long durationindigit, distanceincm; //calculate distance for object following
digitalWrite(trigPin, LOW); //loop the sequence to measure distance
delayMicroseconds(2); //********
digitalWrite(trigPin, HIGH); //******
delayMicroseconds(10); //****
digitalWrite(trigPin, LOW); //**
int durationInDigit = pulseIn(sensorEcho, HIGH); //state the rebound duration
int distanceIncm = (durationInDigit * 0.034) / 2; //state the distance in cm from the rebound duration
{
if (digitalRead(objFollow == HIGH)) { //if the the object follow button is pushed
if (distanceIncm >= 30) { //and the distance to the object is out of range
digitalWrite(motors, LOW); //don't drive motors
} else if (distanceIncm < 30 && distanceIncm > 10) { //if the distance to the object is in range
digitalWrite(motors, HIGH); //drive motors
} else if (distnaceIncm < 10) { //if the distance is too close
digitalWrite(motors, LOW); //don'd drive the motors
}
} else if (digitalRead(objFollow == LOW)) { //if the object follow button is not pushed
driveSeq(); //carry out the drive sequence
}
}
}
void driveCheck() {
digitalWrite(motors, HIGH); //drive the motors
delay(1000); //wait 1 sec
digitalWrite(motors, LOW); //***reverse the motors***
delay(2000); //wait 2 secs
steeringServo.write(90); //turn the steering servo left
sensorServo.write(90) //turn the sensor servo left
delay(1500); //wait 1.5 secs
steeringServo.write(0); //turn the steering servo right
sensorServo.write(0); //turn the sensor servo right
delay(1000); //wait 1 sec
tone(buzzer); //sound the horn
delay(500); //wait 0.5 secs
noTone(buzzer); //stop sounding the horn
delay(1000); //wait 1 sec
digitalWrite(objFollowLED, HIGH); //turn on the object follow indicator
delay(500); //wait 0.5 secs
digitalWrite(objFollowLED, LOW); //turn off the object follow indicator
}
void driveSeq() {
{
if (analogRead(joyHorn) == HIGH) { //if the horn button is pressed
tone(buzzer); //sound the horn
} else { //otherwise
noTone(buzzer); //dont sound the horn
}
}
{
if (driveVal > 512) { //if the joyX value is positive
digitalWrite(motors, HIGH); //drive the motors
} else if (driveVal < 512) { //if the joyX value is negative
digitalWrite(motors, LOW); //***reverse the motors***
}
}
steeringServo.write(steerVal); //write the value of the joyY to the steering servo
}
Again, any and all feedback is welcome!