invalid conversion from void to int

i have wrote this code for a object advoidance robot but cannot get it to compile the error code is for invalid conversion from void(*) to int the code is as followes

#include <Servo.h>
int motor_reset = 2; //digital pin 2 assigned to motor reset
Servo scanServo; // create servo object to control the scanning servo
int pos = 0; // variable to store the servo position
int aDelay = 250; // Delay for the servo and let the IR to return results
int readDistance1 ; // Variable to keep IR right scan value
int readDistance2 ; // Variable to keep IR forward scan value
int readDistance3 ; // Variable to keep IR left scan value
int minDistanceLimit = 4 ; // Distance to set as Obstacle
int pingPin = 8;

void setup()
{
  pinMode(motor_reset, OUTPUT);
  scanServo.attach(11); // attaches the servo on pin 11 to the servo object
  Serial.begin(9600);
  digitalWrite(motor_reset, LOW);
  delay(10);
  digitalWrite(motor_reset, HIGH);
  delay(10);
}
void cm(){
  long duration, cm;
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
  cm = microsecondsToCentimeters(duration);
}

void loop()
{
//  long duration, cm;
//  pinMode(pingPin, OUTPUT);
//  digitalWrite(pingPin, LOW);
//  delayMicroseconds(2);
//  digitalWrite(pingPin, HIGH);
//  delayMicroseconds(5);
//  digitalWrite(pingPin, LOW);
//  pinMode(pingPin, INPUT);
//  duration = pulseIn(pingPin, HIGH);
//  cm = microsecondsToCentimeters(duration);

  for (pos = 35; pos <= 89; pos ++) //scan IR sensor from 35 to 165 degs from left to right
  { 
    scanServo.write (pos); //writes the position to scanning servo
    delay(3); //give it time to get there
  } 
  readDistance1 = cm; //take an IR reading at 89 - ahead

  if (readDistance1 > minDistanceLimit){ 
    reverse; // if obstacle distance is closer than threshold go in opposite direction
  }
  else
    forward(); //continue going forward

    for (pos = 90; pos <= 165; pos ++) //scan IR sensor from 35 to 165 degs from left to right
  { 
    scanServo.write (pos); //writes the position to scanning servo
    delay(3); //give it time to get there
  } 
  readDistance2 = cm; //take an IR reading at 165 - rightside

  if (readDistance2 > minDistanceLimit){  // if obstacle distance is closer than threshold go in opposite direction
    rotateLeft;
  }
  else
    forward(); //continue going forward

    for (pos = 165; pos >=90; pos --) //scans right to left
  {

    scanServo.write (pos);
    delay (3);
  } 
  readDistance1 = cm;

  if (readDistance1 > minDistanceLimit){
    reverse;

  }
  else
    forward();
  for (pos = 89; pos >= 35; pos --) //scan IR sensor from 35 to 165 degs from left to right
  { 
    scanServo.write (pos); //writes the position to scanning servo
    delay(3); //give it time to get there
  } 

  readDistance3 = cm; //take an IR reading at 35 - leftside

  if (readDistance3 > minDistanceLimit){ 
    rotateRight; // if obstacle distance is closer than threshold go in opposite direction
  }
  else
    forward(); //continue going forward
}  
long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}
void moveMotor (unsigned char motor, unsigned char speed)
{
  //left motor
  unsigned char buff3[6];
  buff3[0]=0x80;
  buff3[1]=0x00;
  buff3[2]=0x00;
  buff3[3]=0x00;
  for(int i=0; i<4; i++) {
    Serial.print(buff3[i], BYTE);
  }
  //right motor
  unsigned char buff4[6];
  buff4[0]=0x80;
  buff4[1]=0x00;
  buff4[2]=0x02;
  buff4[3]=0x00;
  for(int i=0; i<4; i++) {
    Serial.print(buff4[i], BYTE);
  }
}
void forward()
{
  moveMotor (1, 0x7A);//right motor
  moveMotor (3, 0x7A);//left motor
}

void rotateRight()
{
  moveMotor (1, 0x50);
  moveMotor (2, 0x40);
}

void rotateLeft()
{
  moveMotor (0, 0x40);
  moveMotor (3, 0x50);
}
void reverse()
{
  moveMotor (0, 0x45);
  moveMotor (2, 0x45);
}

void leftTurn()
{
  moveMotor (1, 0x20);
  moveMotor (3, 0x20);
}

void rightTurn()
{
  moveMotor (1, 0x30);
  moveMotor (3, 0x30);
}

the error occurs on this line and others like it: readDistance1 = cm; //take an IR reading at 89 - ahead
please help me learn what im doing wrong

1 Like

In the function cm() the variable 'cm' is local so it does not exist outside the function. If you want to use the value in another function you must make it global (declared outside any function).

Perhaps you would be better served by returning a value from the cm() function.

long cm(){
  long duration;
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
  return microsecondsToCentimeters(duration);
}

Then you can use "readDistance1 = cm();", etc.

1 Like

At the scope where you are trying to assign a vairable to the contents of "cm", the symbol "cm" refers to the function "cm()" which is declared "long".

This "cm" is the pointer (void *) to the function.

(void *) is not (int).

To call the function and return a value you must suffix it with () to indicate that you are wanting to call the function, and not manipulate the address of the function.

1 Like