robot project

hi, I’m working on an obstacle avoiding robot but there’s an error in my code which displays:
“void value not ignored as it ought to be”
could anyone please tell me how to fix this error? I have attached my code.

Smart_Robot_Program.ino (1.93 KB)

Without looking at your code (why did you not post it here instead of attaching it ?

"void value not ignored as it ought to be"

You have a function that is void, ie no value will be returned from it, but you try to return one

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);
}

}
}

void scanArea(int pos , int delTime){
  int result;
  myServo.write(pos);
  delay(delTime);
  result = sonar.ping_cm();
  return result;
  }

Exactly as UKHeliBob said. You have declared the function void but try to return an int.

Please use code tags when posting code. See the post 'How to use this forum' at the top of each board

void scanArea(int pos , int delTime)  //this line promises not to return a value
{
  int result;
  myServo.write(pos);
  delay(delTime);
  result = sonar.ping_cm();
  return result;    //this line tries to return a value
}

Note the use of code tags (</> top/left of the editor) to make the code easy to deal with here.

oh, so then i basically cant use "return" if my function involves entering values right?

oh, so then i basically cant use "return" if my function involves entering values right?

Wrong. You do not HAVE to use void as the return type. digitalRead() doesn't. analogRead() doesn't.

oh okay, i edited my function:

int scanArea(int pos , int delTime){
int result;
myServo.write(pos);
delay(delTime);
result = sonar.ping_cm();
return result;
}

this seemed to work since i did’nt get the error message when i verified my code

msa99:
oh okay, i edited my function:

int scanArea(int pos , int delTime){
int result;
myServo.write(pos);
delay(delTime);
result = sonar.ping_cm();
return result;
}

this seemed to work since i did'nt get the error message when i verified my code

Do you understand why that compiles OK and previously didn't ?

yeah i did, its because when i declare a function as void, it shouldn't return a value

managed to get the robot working after making the modifications, thank you all so much!