Hi,
I would like to use the Stepper library within a library I am creating to no avail. My aim is to define a class called Braitenberg for a vehicle and use two instances of Stepper (left and right motor). I am not so familiar with C++ syntax, coming more from python and JS...
I've tried following:
Braitenberg.h
/*
* Braitenberg.h
*/
// ensure this library description is only included once
#ifndef Braitenberg_h
#define Braitenberg_h
#include "Stepper.h";
// library interface description
class Braitenberg {
//Stepper leftStepper;
public:
// constructors:
Braitenberg(int number_of_steps, int left_motor_pin_1, int left_motor_pin_2, int left_motor_pin_3, int left_motor_pin_4, int right_motor_pin_1, int right_motor_pin_2, int right_motor_pin_3, int right_motor_pin_4 ) {
Stepper leftStepper(int left_motor_pin_1, int left_motor_pin_2, int left_motor_pin_3, int left_motor_pin_4);
};
int version(void);
void forward(void);
enum State {STOP, FORWARD, ROTATE_RIGHT, ROTATE_LEFT};
enum Sensors {NONE, SENSOR_RIGHT, SENSOR_LEFT, BOTH};
int number_of_steps;
int left_motor_pin_1;
int left_motor_pin_2;
int left_motor_pin_3;
int left_motor_pin_4;
int right_motor_pin_1;
int right_motor_pin_2;
int right_motor_pin_3;
int right_motor_pin_4;
private:
Stepper leftStepper;
};
#endif
My Braitenberg.cpp
#include "Arduino.h"
#include "Stepper.h"
#include "Braitenberg.h"
/*
* constructor for four-pin version
* Sets which wires should control the motor.
*/
Braitenberg::Braitenberg(int number_of_steps, int left_motor_pin_1, int left_motor_pin_2, int left_motor_pin_3, int left_motor_pin_4, int right_motor_pin_1, int right_motor_pin_2, int right_motor_pin_3, int right_motor_pin_4)
{
this->left_motor_pin_1 = left_motor_pin_1;
this->left_motor_pin_2 = left_motor_pin_2;
this->left_motor_pin_3 = left_motor_pin_3;
this->left_motor_pin_4 = left_motor_pin_4;
this->right_motor_pin_1 = right_motor_pin_1;
this->right_motor_pin_2 = right_motor_pin_2;
this->right_motor_pin_3 = right_motor_pin_3;
this->right_motor_pin_4 = right_motor_pin_4;
this->leftStepper(200,8,9,10,11);
}
void Braitenberg::forward(void) {
this->leftStepper.step(200);
}
And my app.ino
#include <Braitenberg.h>
const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
// for your motor
// initialize the stepper library on pins 8 through 11 (left motor) and right motor (4 to 7):
Braitenberg myVehicle(stepsPerRevolution, 8, 9, 10, 11, 4, 5, 6, 7);
int stepCount = 0; // number of steps the motor has taken
void setup() {
// nothing to do inside the setup
}
void loop() {
// read the sensor value:
int sensorReading = analogRead(A0);
// map it to a range from 0 to 100:
int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
// set the motor speed:
if (motorSpeed > 0) {
// step 1/100 of a revolution:
myVehicle.forward();
}
}
I am getting plenty of compile errors:
Arduino/libraries/Braitenberg/Braitenberg.h:16:222: error: no matching function for call to 'Stepper::Stepper()'
Braitenberg(int number_of_steps, int left_motor_pin_1, int left_motor_pin_2, int left_motor_pin_3, int left_motor_pin_4, int right_motor_pin_1, int right_motor_pin_2, int right_motor_pin_3, int right_motor_pin_4 ) {
Arduino.app/Contents/Java/libraries/Stepper/src/Stepper.h:89:5: note: candidate: Stepper::Stepper(int, int, int, int, int, int)
Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2,
Arduino.app/Contents/Java/libraries/Stepper/src/Stepper.h:89:5: note: candidate expects 6 arguments, 0 provided
/Applications/Arduino.app/Contents/Java/libraries/Stepper/src/Stepper.h:87:5: note: candidate: Stepper::Stepper(int, int, int, int, int)
Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2,
Seems like there's a problem with the Stepper instance leftStepper. Also, it seems that the arguments don't really get passed onto the Stepper (note: candidate expects 6 arguments, 0 provided). I can't get my head around the initialisation of a class within another class. Some forum entries were talking about using pointers instead (which I've also tried to no avail).
Any ideas? Thanks for your help&time.