Okay I pasted the code, could you check it out and help me find out specifically where i caused the error. It's happening when i use the "scanArea" function.
#include <Servo.h>
#include <NewPing.h>
const int maxDist = 300;
Servo myServo;
NewPing sonar(A0, A1, 300);
//motor A:
const int enA = 10;
const int ln1 = 9;
const int ln2 = 8;
//motor B:
const int enB = 5;
const int ln3 = 7;
const int ln4 = 6;
//directions for servo and motor:
const int go = 0;
const int rght = 1;
const int left = 2;
const int back = 3;
const int stay = 4;
const int middle = 95;
//other variables:
int cm;
int Ldist;
int Rdist;
void setup() {
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(ln1, OUTPUT);
pinMode(ln2, OUTPUT);
pinMode(ln3,OUTPUT);
pinMode(ln4, OUTPUT);
myServo.write(4);
analogWrite(enA, 200);
analogWrite(enB, 200);
}
void setlines(int line_1, int line_2 , int line_3, int line_4 ) {
digitalWrite(ln1, line_1);
digitalWrite(ln2, line_2);
digitalWrite(ln3, line_3);
digitalWrite(ln4, line_4);
}
void directions(int dir) {
switch (dir){
case 0: //go
setlines(LOW, HIGH, HIGH, LOW);
break;
case 1: //right
setlines(LOW, HIGH, LOW, HIGH);
break;
case 2: //left
setlines(HIGH, LOW, HIGH, LOW);
break;
case 3://back
setlines(HIGH, LOW, LOW, HIGH);
break;
case 4://stay
setlines(LOW, LOW, LOW, LOW);
break;
}
}
void scanArea(int pos , int delTime){
int result;
myServo.write(pos);
delay(delTime);
result = sonar.ping_cm();
return result;
}
void turn(int turnDirection) {
directions(turnDirection);
analogWrite(enA, 200);
analogWrite(enB, 200);
myServo.write(middle);
directions(stay);
delay(500);
}
void loop(){
cm = scanArea(middle, 50);
if (cm > 30) {
directions(go);
analogWrite(enA, 200);
analogWrite(enB, 200);
}
else {
analogWrite(enA, 150);
analogWrite(enB, 150);
directions(back);
delay(150)
directions(stay);
Ldist = scanArea(20,500);
Rdist = scanArea(170,500);
if (Ldist > Rdist) {
turn(left);
}
else if (Rdist > Ldist) {
turn(right);
}
}
}