Multiple files project, ‘analogWrite’ was not declared in this scope

I'm trying to split my code in several files.
I have main.ino file, ex.h and ex.c.

main.imo contains

#include "ex.h"
void setup()
{
// Some init here
}
void loop()
{}

ex.h contains

void forward(int speed1, int speed2);

ex.c contains

#include "ex.h"
void forward(int speed1, int speed2)
{
  analogWrite(3, 0);
  analogWrite(5, speed1);
  analogWrite(6, 0);
  analogWrite(11, speed2);
}

I get this error when compile:
"ex.cpp: In function ‘void forward(int, int)’:
ex.cpp:8: error: ‘analogWrite’ was not declared in this scope"

What should I include to make it work?
I tried Arduino.h, but it gives me another error:
"arduino-1.5.6-r2/hardware/arduino/avr/cores/arduino/main.cpp:30: undefined reference to `setup'"

You'll need to

#include "Arduino.h"

Depending on the IDE version, there can be some differences in what needs to be included. By convention, you'll see something like:

#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h" 
#include "pins_arduino.h"
#endif

Put this at the top of ex.cpp and see how that works.

Yeah, I tried that.
Get this error:
"main.cpp.o: In function main': arduino-1.5.6-r2/hardware/arduino/avr/cores/arduino/main.cpp:30: undefined reference to setup'
arduino-1.5.6-r2/hardware/arduino/avr/cores/arduino/main.cpp:33: undefined reference to `loop'"

Give the extra files the extension .ino ???

...R

Robin2:
Give the extra files the extension .ino

Is it possible to make h, c/c++ work?

shamdor:

Robin2:
Give the extra files the extension .ino

Is it possible to make h, c/c++ work?

Yes, just create files with the appropriate extension.

PeterH:

shamdor:

Robin2:
Give the extra files the extension .ino

Is it possible to make h, c/c++ work?

Yes, just create files with the appropriate extension.

I don't understand what you mean. Can you look at the code I provided and say what is wrong?

shamdor:
Can you look at the code I provided and say what is wrong?

The code you posted does not #include Arduino.h, which is why you get errors when you have calls to Arduino functions.

If you think you have fixed that and still get errors and want suggestions on how to fix them, you need to post your updated code. I'm not going to speculate about code you haven't posted.

PeterH:
If you think you have fixed that and still get errors and want suggestions on how to fix them, you need to post your updated code. I'm not going to speculate about code you haven't posted.

Modified code:

main.imo contains

#include "ex.h"
void setup()
{
// Some init here
}
void loop()
{}

ex.h contains

void forward(int speed1, int speed2);

ex.c contains

#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h" 
#include "pins_arduino.h"
#endif

#include "ex.h"
void forward(int speed1, int speed2)
{
  analogWrite(3, 0);
  analogWrite(5, speed1);
  analogWrite(6, 0);
  analogWrite(11, speed2);
}

Now I get this errors:
main.cpp.o: In function main': arduino-1.5.6-r2/hardware/arduino/avr/cores/arduino/main.cpp:30: undefined reference to setup'
arduino-1.5.6-r2/hardware/arduino/avr/cores/arduino/main.cpp:33: undefined reference to `loop'

The Arduino is programmed using C++, not C. If you want to put some code in another file, put it in a cpp file, NOT a c file.

Do NOT call your sketch main!

PaulS:
The Arduino is programmed using C++, not C. If you want to put some code in another file, put it in a cpp file, NOT a c file.

Do NOT call your sketch main!

That helped.

Thanks!

shamdor:
main.imo contains

The code you posted compiles fine for me, with the sketch saved in a .ino file. I don't know whether .imo is a typo in your post, but if that's the actual file name you're using that is wrong.