'task' is not declared in this scope.

Here is the starting part of my program

#include <Servo.h>
char incomingByte; // for incoming serial data
Servo dirServo;
Servo forServo;
Servo upServo;
Servo gripperServo;

void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
forServo.attach(4);
dirServo.attach(5);// attaches the servo on pin 9 to the servo object
upServo.attach(6);
gripperServo.attach(7); // attaches the servo on pin 9 to the servo object

}

void loop() {
if (Serial.available() > 0) {
incomingByte = Serial.read();
Serial.write(incomingByte+1);
if(incomingByte==‘g’) task(120);
Serial.println(‘C’);
if(incomingByte==‘b’) task(120);
Serial.println(‘C’);
if(incomingByte==‘r’) task(120);
Serial.println(‘C’);
}
}

When I execute it, it shows an error message: ‘task’ is not declared in this scope.

What should I do?

What should I do?

Declare something called “task” that accepts a single integer.
Make sure you get its scope correct.

And use code tags when posting code.

When I execute it

It didn’t execute, because it didn’t compile.

include whatever it is that defines task. It is not standard C++, nor one of the arduino library functions.

Or, of course, create your own task function.

gripperServo.attach(7);  // attaches the servo on pin 9 to the servo object

Comments that don't match the code are a complete waste of time.

Which is why in "The elements of programming style" Kernigham and Plauger suggest writing all comments on the right hand side so that you can debug without reading them.

Hmm, does any IDE allow you to collapse all comments?

KeithRB: Hmm, does any IDE allow you to collapse all comments?

Eclipse does.

I get this program from my teacher and those comments are done by him. I just forget to delete those comments and so please, can anyone answer my question?

timfung0729: I get this program from my teacher and those comments are done by him. I just forget to delete those comments and so please, can anyone answer my question?

You got the best answer anyone can give at this point in reply #1.

Where did your teacher define the function "task"? Can't you ask him/her where it is?

Include this in your program:

void task(int arg)
{
   serial.println(arg);
   return;
}

This will compile work. But I can guarantee you that this is not what task() was supposed to do. I am guessing that the assignment was for you to write your own task() that does something.

KeithRB:
Include this in your program:

void task(int arg)

{
  Serial.println(arg);
  return;
}




This will compile work. But I can guarantee you that this is not what task() was supposed to do. I am guessing that the assignment was for you to write your own task() that does something.

It is okay now for task, but when I execute it, it returns Error compiling for board Arduino/Genuino Uno when I declare #include<serial.h>.

eturns Error compiling for board Arduino/Genuino Uno when I declare #include<serial.h>.

So don’t do that - see reply #10 (you may think reply #10 just quotes reply #9, but it doesn’t)

What error message did it give? Post the current form of the code.

it returns Error compiling for board Arduino/Genuino Uno when I declare #include<serial.h>.

Why do you think you need to include that file? Do you actually have a file with that name?

But, it says 'serial' is not declared in this scope

See reply #10

AWOL: See reply #10

You keep saying that, but I must be missing something. I don't see that you added anything in reply #10.

Edit: Never mind. I see what changed.

It is solved now, because I typed the Serial as serial, sorry for having silly mistakes. I am just a noob.

And, now, I have another problem about if and else

void rotate() { if(incomingByte=='r') dirServo.write(0); delay(1500); else if(incomingByte=='b') dirServo.write(60); delay(1500); else if(incomingByte=='g') dirServo.write(120); delay(1500); }

can anyone fix this? It says there is no 'if' before the second 'else'

It says there is no 'if' before the second 'else'

There's no if before the first else either.

Try using some of these { }

because I typed the Serial as serial, sorry for having silly mistakes.

No sweat - you weren't the only one to make that mistake.