#include <Servo.h>
#include <NewPing.h>
#define TRIGGER_PIN A4
#define ECHO_PIN A4
#define MAX_DISTANCE 400
#define TURNLEFT 1 //Defines variable used in void turning
#define FINALTURNLEFT 2
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
Servo rightservo; // create servo object to control right servo arm
Servo leftservo; //creates servo object to control left servo arm
int sidesensor;
int frontsensor;
int rsopen = 160;
int lsopen = 30;
int rsclose = 65;
int lsclose = 110;
int BASE = 200;
int RMOTOR;
int LMOTOR;
double KP = 5.5;
int SETPOINT = 215;
int fSETPOINT = 100;
int sensorPin = A3;
int linesensorPin = A2;
int linesensor;
int flag = 0;
int trueturn = 0;
int count;
int dump = 0;
int buttonpin = 7;
long interval = 500;
long pinginterval = 33;
void setup() {
Serial.begin(115200);
pinMode(12, OUTPUT); //Initiates Motor Channel A pin - Right Motor
pinMode(9, OUTPUT); //Initiates Brake Channel A pin - Right Motor
pinMode(13, OUTPUT); //Initiates Motor Channel B pin - Left Motor
pinMode(8, OUTPUT); //Initiates Brake Channel B pin - Left Motor
pinMode(4, OUTPUT); //Initiates the lift motor
pinMode(10, OUTPUT); //Initiates the lift motor
pinMode(buttonpin, INPUT);
rightservo.attach(5); // attaches the right arm servo on pin 5
rightservo.write(160); // tells servo to go to position 160
leftservo.attach(6); // attaches the left arm servo on pin 6
leftservo.write(30); // tells the servo to go to position 30
delay(500); // waits 500ms for the servo to reach the position
}
void loop() {
while (flag == 0){
unsigned long currentMillis = millis();
int frontsensor = sonar.ping_cm();
while(millis() - currentMillis < pinginterval) {
int linesensor = analogRead(linesensorPin);
drive();
if (++count > 300 && linesensor > 700) //Compares lastOpen to 500 and ballcounter > 0 if true itll execute code
{
trueturn = trueturn + 1;
count = 0;
}
if (frontsensor >= 165 && frontsensor <= 175){ //Activates right servo arm is front sensor between 180 and 170cm or turnarm equals 1
flag = 1;
}
if (frontsensor >= 100 && frontsensor <= 105){ //Increases counter variable if front sensor is between 105 and 95cm
flag = 2;
}
if (frontsensor <= 63 && frontsensor > 30){ //Makes left turn if front sensor less than 60cm and ballcounter is greater than 1
flag = 4;
}
}
while(flag==1){
unsigned long currentMillis = millis();
while(millis() - currentMillis < interval) {
drive();
rightservo.write(rsclose); // tells right servo to go to position 70
}
flag = 3;
}
while(flag==2){
unsigned long currentMillis = millis();
while(millis() - currentMillis < interval) {
drive();
rightservo.write(rsclose); //Tells the right servo to go to position 70
leftservo.write(lsclose); //Tells the left servo to go to position 130
}
flag = 3;
}
while(flag==3){
unsigned long currentMillis = millis();
while(millis() - currentMillis < interval) {
drive();
rightservo.write(rsopen); //Tells the right servo to go to position 70
leftservo.write(lsopen); //Tells the left servo to go to position 130
}
flag = 0;
}
while(flag==4){
unsigned long currentMillis = millis();
int frontsensor = sonar.ping_cm();
while(millis() - currentMillis < pinginterval) {
int sidesensor = analogRead(sensorPin);
turning(TURNLEFT);
if (sidesensor > 195 && sidesensor < 210 && frontsensor > 140){
flag = 0;
}
}
}
while (trueturn == 4){
int linesensor = analogRead(linesensorPin);
int frontsensor = sonar.ping_cm();
drive();
if (frontsensor >= 95 && frontsensor <= 105){
flag = 5;
}
while(flag == 5){
stopdrive();
unsigned long currentMillis = millis();
while(millis() - currentMillis < interval) {
rightservo.write(rsclose); //Tells the right servo to go to position 70
leftservo.write(lsclose); //Tells the left servo to go to position 130
}
currentMillis = millis();
while(millis() - currentMillis < interval) {
rightservo.write(rsopen); //Tells the right servo to go to position 70
leftservo.write(lsopen); //Tells the left servo to go to position 130
}
flag = 6;
}
while(flag == 6){
int linesensor = analogRead(linesensorPin);
reverse();
if (linesensor >= 700){
flag = 7;
}
}
while(flag == 7 ){
int frontsensor = sonar.ping_cm();
finalturning(FINALTURNLEFT);
if (frontsensor >= 10 && frontsensor <= 50){
flag = 8;
}
}
while(flag == 8){
unsigned long currentMillis = millis();
int frontsensor = sonar.ping_cm();
while(millis() - currentMillis < pinginterval) {
finaldrive();
if (frontsensor <= 10 && frontsensor >=1){
flag = 9;
}
}
}
while(flag == 9){
stopdrive();
digitalWrite(4, HIGH); //Initiates Motor Channel B pin - Left Motor
digitalWrite(10, LOW);
if(digitalRead(buttonpin) == HIGH){
flag = 10;
}
}
while(flag == 10){
digitalWrite(4, HIGH); //Initiates Motor Channel B pin - Left Motor
digitalWrite(10, HIGH);
stopdrive();
}
}
}
}
void drive() {
int sidesensor = analogRead(sensorPin);
RMOTOR = BASE - KP * (SETPOINT - sidesensor);
LMOTOR = BASE + KP * (SETPOINT - sidesensor);
if (RMOTOR > 255){
RMOTOR = 255;
}
if (LMOTOR > 255){
LMOTOR = 255;
}
if (RMOTOR < 0){
RMOTOR = 0;
}
if (LMOTOR < 0){
LMOTOR = 0;
}
digitalWrite(12, LOW); //Establishes forward direction of Channel A - Right Motor
analogWrite(3, RMOTOR); //Spins the motor on Channel A at full speed - Right Motor
digitalWrite(13, HIGH); //Initiates Motor Channel B pin - Left Motor
analogWrite(11, LMOTOR); //Spins the motor on Channel B at 100 speed - Left Motor
digitalWrite(4, HIGH); //Initiates Motor Channel B pin - Left Motor
digitalWrite(10, HIGH);
}
void turning(int leftturn) {
switch (leftturn) {
case TURNLEFT:
digitalWrite(12, LOW); //Establishes forward direction of Channel A - Right Motor
analogWrite(3, 200); //Spins the motor on Channel A at full speed - Right Motor
digitalWrite(13, LOW); //Establishes reverse direction of Channel B - Left Motor
analogWrite(11, 200); //Spins the motor on Channel B at full speed - Left Motor
break;
}
}
void finalturning(int finalleftturn) {
switch (finalleftturn) {
case FINALTURNLEFT:
digitalWrite(12, LOW); //Establishes forward direction of Channel A - Right Motor
analogWrite(3, 255); //Spins the motor on Channel A at full speed - Right Motor
digitalWrite(13, LOW); //Establishes reverse direction of Channel B - Left Motor
analogWrite(11, 100); //Spins the motor on Channel B at full speed - Left Motor
break;
}
}
void stopdrive() {
digitalWrite(12, LOW); //Establishes forward direction of Channel A - Right Motor
analogWrite(3, 0); //Spins the motor on Channel A at full speed - Right Motor
digitalWrite(13, HIGH); //Initiates Motor Channel B pin - Left Motor
analogWrite(11, 0); //Spins the motor on Channel B at 100 speed - Left Motor
}
void reverse(){
digitalWrite(12, HIGH); //Establishes forward direction of Channel A - Right Motor
analogWrite(3, 225); //Spins the motor on Channel A at full speed - Right Motor
digitalWrite(13, LOW); //Initiates Motor Channel B pin - Left Motor
analogWrite(11, 225); //Spins the motor on Channel B at 100 speed - Left Motor
}
void finaldrive() {
int sidesensor = analogRead(sensorPin);
RMOTOR = BASE - KP * (fSETPOINT - sidesensor);
LMOTOR = BASE + KP * (fSETPOINT - sidesensor);
if (RMOTOR > 255){
RMOTOR = 255;
}
if (LMOTOR > 255){
LMOTOR = 255;
}
if (RMOTOR < 0){
RMOTOR = 0;
}
if (LMOTOR < 0){
LMOTOR = 0;
}
digitalWrite(12, LOW); //Establishes forward direction of Channel A - Right Motor
analogWrite(3, RMOTOR); //Spins the motor on Channel A at full speed - Right Motor
digitalWrite(13, HIGH); //Initiates Motor Channel B pin - Left Motor
analogWrite(11, LMOTOR); //Spins the motor on Channel B at 100 speed - Left Motor
}