Go Down

Topic: invalid conversion from 'int' to 'void' (Read 2167 times) previous topic - next topic

Hi..I'm new to arduino..i want to developed an automatic valve using a servo motor that will react upon a flow sensor.. but my problem is when I want to verify the code, it keep saying "invalid conversion from 'int' to 'void'" at line -attachInterrupt(0, rpm, RISING); //and the interrupt is attached-

Code: [Select]
#include <Servo.h>

Servo myservo;
volatile int NbTopsFan; //measuring the rising edges of the signal
int Calc; 
int potPin = 0; //analog pin ued to connect the sensor
int ledPin = 9;  //select pin for the servo
int val = 0;  //variable to read the value from the analog pin
int rpm;   //This is the function that the interupt calls
void setup()
{
  NbTopsFan++;  //This function measures the rising and falling edge of the
{
  myservo.attach(9); //set up the servo as usual

  pinMode(ledPin, OUTPUT);  //variable to store the value coming from the sensor
  pinMode(potPin, INPUT); //initializes digital pin 0 as an input
  Serial.begin(9600); //for watching the speeds in the serial monitor
  attachInterrupt(0, rpm, RISING); //and the interrupt is attached
}
void loop(){
  NbTopsFan = 0;      //Set NbTops to 0 ready for calculations
  sei();            //Enables interrupts
val = analogRead(potPin);    // read the value from the sensor
  val = map(val,0,1023,0,179);
  cli();            //Disable interrupts
  Calc = (NbTopsFan * 60 / 7.5); //(Pulse frequency x 60) / 7.5Q, = flow rate
   if (val>79) myservo.writeMicroseconds(0);  //set servo to mid-point
 
   else if (val<80) myservo.writeMicroseconds(1500); 

  Serial.print (Calc, DEC); //Prints the number calculated above
  Serial.print (" L/hour\r\n"); //Prints "L/hour" and returns a  new line
  Serial.println(val);
delay(10);

}


please someone help me..thank you very much

UKHeliBob

You don't have a function called rpm for the interrupt to call, you have a variable called rpm
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

majenko

Quote
Code: [Select]

int rpm;   //This is the function that the interupt calls

No it isn't - it's an integer variable that can store between -32768 and 32767.

MarkT

Your curly braces are also mis-nested - you need to indent your code properly to make it readable.
[ I won't respond to messages, use the forum please ]

thank you.. so what is the problem with this line? since its highlighted.. i still don't get it..

Code: [Select]
attachInterrupt(0, rpm, RISING); //and the interrupt is attached-

what its relation to this line:-
Code: [Select]
int rpm;   //This is the function that the interupt calls

(in case if I have change the word 'function' to 'variable')


Your curly braces are also mis-nested - you need to indent your code properly to make it readable.


sorry Markt.. can you show me where the code I need to indent? sorry..still learning..

Arrch


sorry Markt.. can you show me where the code I need to indent? sorry..still learning..


In the Arduino IDE, press Ctrl+T

sketch_may03a.ino: In function 'void setup()':
sketch_may03a:19: error: invalid conversion from 'int' to 'void (*)()'
sketch_may03a:19: error: initializing argument 2 of 'void attachInterrupt(uint8_t, void (*)(), int)'
sketch_may03a:22: error: a function-definition is not allowed here before '{' token
sketch_may03a:39: error: expected `}' at end of input


These messages show my error..really dont know how to edit the code..

Arrch


sketch_may03a.ino: In function 'void setup()':
sketch_may03a:19: error: invalid conversion from 'int' to 'void (*)()'
sketch_may03a:19: error: initializing argument 2 of 'void attachInterrupt(uint8_t, void (*)(), int)'
sketch_may03a:22: error: a function-definition is not allowed here before '{' token
sketch_may03a:39: error: expected `}' at end of input


These messages show my error..really dont know how to edit the code..


Each left curly brace should have a corresponding right curly brace.



sketch_may03a.ino: In function 'void setup()':
sketch_may03a:19: error: invalid conversion from 'int' to 'void (*)()'
sketch_may03a:19: error: initializing argument 2 of 'void attachInterrupt(uint8_t, void (*)(), int)'
sketch_may03a:22: error: a function-definition is not allowed here before '{' token
sketch_may03a:39: error: expected `}' at end of input


These messages show my error..really dont know how to edit the code..


Each left curly brace should have a corresponding right curly brace.


you mean every '{' must be pair with this code '}' also right?

This is my code after editing, i have change the int to void for the rpm
Code: [Select]
void rpm()   //integer function that can store between -32768 and 32767.

this is my full code
Code: [Select]
#include <Servo.h>

Servo myservo;
volatile int NbTopsFan; //measuring the rising edges of the signal
int Calc; 
int potPin = 0; //analog pin ued to connect the sensor
int ledPin = 9;  //select pin for the servo
int val = 0;  //variable to read the value from the analog pin
void rpm()   //integer function that can store between -32768 and 32767.

{
  NbTopsFan++;  //This function measures the rising and falling edge of the
}
{
  myservo.attach(9); //set up the servo as usual

  pinMode(ledPin, OUTPUT);  //variable to store the value coming from the sensor
  pinMode(potPin, INPUT); //initializes digital pin 0 as an input
  Serial.begin(9600); //for watching the speeds in the serial monitor
  attachInterrupt(0, rpm, RISING); //and the interrupt is attached
}
void loop()
{
  NbTopsFan = 0;      //Set NbTops to 0 ready for calculations
  sei();            //Enables interrupts
  val = analogRead(potPin);    // read the value from the sensor
  val = map(val,0,1023,0,179);
  cli();            //Disable interrupts
  Calc = (NbTopsFan * 60 / 7.5); //(Pulse frequency x 60) / 7.5Q, = flow rate
   
   if (val>79) myservo.writeMicroseconds(0);  //set servo to mid-point
 
   else if (val<80) myservo.writeMicroseconds(1500); 

  Serial.print (Calc, DEC); //Prints the number calculated above
  Serial.print (" L/hour\r\n"); //Prints "L/hour" and returns a  new line
  Serial.println(val);
delay(10);

}


BUT I still got this error message at this line,
Code: [Select]
{
  NbTopsFan++;  //This function measures the rising and falling edge of the
}
{

it says
sketch_may03a:14: error: expected unqualified-id before '{' token

hmm...how do i fix this error?


JimboZA

I think you've dived in a bit too deep, too soon with this project.

Interrupts and functions should be "phase II" of your Arduino journey if you are, as you say, new to this.

Phase I ought to be the core learning examples here and then have a look at functions
"Could you do the egg bacon spam and sausage without the spam then? "

No PMs for help please.
DO NOT power servos from Arduino 5V: give them their own power and connect the grounds.

UKHeliBob

As Jimbo says, you may have dived in too deep but you can fix the code.
First I suggest that you move the rpm function to the very end of your code as the current layout is confusing both to read and to the compiler.
Code: [Select]
void rpm()   //integer function that can store between -32768 and 32767.
{
  NbTopsFan++;  //This function measures the rising and falling edge of the
}
The comments are wrong but that won't affect operation of the program.

Once you have done that you will see more easily that you have no setup() function.  Well, you have the code for it but no function name.  Add the function name and the code will compile.  Whether or not it works or does what you want I do not know.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Helo guys..thanks for the help before this..
this is my code after editing and it seems doesnt have any error
Code: [Select]
#include <Servo.h>

Servo myservo;
volatile int NbTopsFan; //measuring the rising edges of the signal
int Calc;                               
int hallsensor = 0;    //analogue pin of the sensor
int ledPin = 9;  //select pin for the servo

int val = 0;  //variable to read the value from the analog pin
void rpm()
{
  NbTopsFan++;  //This function measures the rising and falling edge of the
 
}
void setup()
{

  myservo.attach(9); //set up the servo as usual
 
   
  pinMode(hallsensor, INPUT); //initializes digital pin 0 as an input
  pinMode(ledPin, OUTPUT);  //variable to store the value coming from the sensor
  Serial.begin(9600); //for watching the speeds in the serial monitor
  attachInterrupt(0, rpm, RISING); //and the interrupt is attached
}

void loop()
{
  NbTopsFan = 0;      //Set NbTops to 0 ready for calculations
  sei();            //Enables interrupts
  delay (1000);      //Wait 1 second
  cli();            //Disable interrupts
  Calc = (NbTopsFan * 60 / 7.5); //(Pulse frequency x 60) / 7.5Q, = flow rate
  val = analogRead(hallsensor); // read the value from sensor
  val = map(val,0,1023,0,179);
  if (val>79)myservo.writeMicroseconds(0);  //set servo to mid-point
 
  else if (val<80)myservo.writeMicroseconds(1500);
 
  Serial.print (Calc, DEC); //Prints the number calculated above
  Serial.println(val);
  Serial.print (" L/hour\r\n"); //Prints "L/hour" and returns a  new line
delay(10);
}


BUT I still got one problem, why the when I open the serial monitor there are no value for the 'L/hour' although I had write the 
Code: [Select]
Serial.print (Calc, DEC); //Prints the number calculated above

and
Code: [Select]
Serial.print (" L/hour\r\n"); //Prints "L/hour" and returns a  new line

the serial monitor just look like this:
( Here I attach the serial monitor pic)


Is this the suitable code? any suggestion on what the suitable code for me if I want to control the servo motor degree of rotation due to the flow rate calculate by the flow sensor..

Go Up