I moved all variable initializations to a .cpp file in my library. This is what I have now:
my sketch:
#include <AFMotor.h>
#include <RobotArm.h>
void setup() {
for(int i_motors = 0; i_motors < NUM_MOTORS; i_motors++)
motor[i_motors].init(i_motors+1,1,MOTOR12_64KHZ);
for(int i_motor = 0; i_motor < NUM_MOTORS; i_motor++)
motor[i_motor].setSpeed(0);
}
void loop() {
motor[0].run(FORWARD); // turn it on going forward
delay(10);
motor[0].run(BACKWARD); // the other way
delay(10);
}
RobotArm.h:
#include <Arduino.h>
#ifndef RobotArm
#define RobotArm
/////////////////////////// MACROS ///////////////////////////////////////////////
#define MAX_SPEED 1
#define MIN_DV_M1 6.0
#define MIN_DV_M2 6.0
#define MIN_DV_M3 6.0
#define MIN_DV_M4 6.0
#define MOTOR_SUPPLY_VOLTAGE 9.0
#define NUM_MOTORS 4
//////////////////////////////////////////////////////////////////////////////////
/////////////////////////// Struct Types ////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
struct position_s
{
int max[NUM_MOTORS];
int min[NUM_MOTORS];
int center[NUM_MOTORS];
};
struct speed_s
{
int max[NUM_MOTORS];
int min[NUM_MOTORS];
};
struct motors_s
{
int fb[NUM_MOTORS];
int fb_pins[NUM_MOTORS];
};
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
#endif
RobotArm.cpp
#include <RobotArm.h>
/////////////////////////////// SYSTEM STRUCTS ////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
position_s position =
{
{245,245,245,245},
{10,10,10,10},
{255/2,255/2,255/2,255/2}
};
speed_s speed =
{
{1},
{(MIN_DV_M1/MOTOR_SUPPLY_VOLTAGE*255),(MIN_DV_M2/MOTOR_SUPPLY_VOLTAGE*255),(MIN_DV_M3/MOTOR_SUPPLY_VOLTAGE*255),(MIN_DV_M4/MOTOR_SUPPLY_VOLTAGE*255)},
};
motors_s motors =
{
{0},{A0,A1,A2,A3}
};
//////////////////////////////////////////////////////////////////////////////////
////////////////////////////// GLOBAL VARIABLES //////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
AF_DCMOTOR motor[NUM_MOTORS];
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
the relevant part of the modified library AFMotor.cpp (no compilation errors)
AF_DCMotor::AF_DCMotor()
{
}
AF_DCMotor::AF_DCMotor(uint8_t num, uint8_t freq) {
motornum = num;
pwmfreq = freq;
MC.enable();
switch (num) {
case 1:
latch_state &= ~_BV(MOTOR1_A) & ~_BV(MOTOR1_B); // set both motor pins to 0
MC.latch_tx();
initPWM1(freq);
break;
case 2:
latch_state &= ~_BV(MOTOR2_A) & ~_BV(MOTOR2_B); // set both motor pins to 0
MC.latch_tx();
initPWM2(freq);
break;
case 3:
latch_state &= ~_BV(MOTOR3_A) & ~_BV(MOTOR3_B); // set both motor pins to 0
MC.latch_tx();
initPWM3(freq);
break;
case 4:
latch_state &= ~_BV(MOTOR4_A) & ~_BV(MOTOR4_B); // set both motor pins to 0
MC.latch_tx();
initPWM4(freq);
break;
}
}
void AF_DCMotor::init(uint8_t num, uint8_t freq) {
motornum = num;
pwmfreq = freq;
MC.enable();
switch (num) {
case 1:
latch_state &= ~_BV(MOTOR1_A) & ~_BV(MOTOR1_B); // set both motor pins to 0
MC.latch_tx();
initPWM1(freq);
break;
case 2:
latch_state &= ~_BV(MOTOR2_A) & ~_BV(MOTOR2_B); // set both motor pins to 0
MC.latch_tx();
initPWM2(freq);
break;
case 3:
latch_state &= ~_BV(MOTOR3_A) & ~_BV(MOTOR3_B); // set both motor pins to 0
MC.latch_tx();
initPWM3(freq);
break;
case 4:
latch_state &= ~_BV(MOTOR4_A) & ~_BV(MOTOR4_B); // set both motor pins to 0
MC.latch_tx();
initPWM4(freq);
break;
}
}
void AF_DCMotor::run(uint8_t cmd) {
uint8_t a, b;
switch (motornum) {
case 1:
a = MOTOR1_A; b = MOTOR1_B; break;
case 2:
a = MOTOR2_A; b = MOTOR2_B; break;
case 3:
a = MOTOR3_A; b = MOTOR3_B; break;
case 4:
a = MOTOR4_A; b = MOTOR4_B; break;
default:
return;
}
I'm now getting errors:
sketch_mar14a.cpp: In function 'void setup()':
sketch_mar14a:6: error: 'motor' was not declared in this scope
sketch_mar14a:9: error: 'motor' was not declared in this scope
sketch_mar14a.cpp: In function 'void loop()':
sketch_mar14a:14: error: 'motor' was not declared in this scope
I tried moving the array of classes declaration to my sketch instead of the .cpp file and I got the additional error:
sketch_mar14a:3: error: 'AF_DCMOTOR' does not name a type