Hi,
As someone new to programming, I've hit an obstacle on my current project.
The project is to build up a robot chassis. I have a 4-wheel chassis, an arduino Mega256 and a SMART 2-channel Motor Shield.
I wrote a sketch to provide various movements, and got it working as expected.
I then created a library of the same functions.
However, when I run the example sketch, it does not work as expected.
The code is:
.h file:
// ensure this library description is only included once
#ifndef Motorz_h
#define Motorz_h
// library interface description
class Motorz
{
// user-accessible "public" interface
public:
Motorz(int speedL, int brkL, int dirL, int speedR, int brkR, int dirR);
void EStop();
void FwdSlow();
void FwdFast();
void BwdSlow();
void BwdFast();
void FwdLeft();
void FwdRight();
void BwdLeft();
void BwdRight();
void SpinLeft();
void SpinRight();
void Accel();
void DeAccel();
private:
int _speedL;
int _dirL;
int _brkL;
int _speedR;
int _dirR;
int _brkR;
};
#endif
.cpp file:
// include this library's description file
#include "Arduino.h"
#include "Motorz.h"
// Constructor
Motorz::Motorz(int speedL, int brkL, int dirL, int speedR, int brkR, int dirR)
{
// Left motor
pinMode(speedL, OUTPUT);
_speedL = speedL;
pinMode(brkL, OUTPUT);
_brkL= brkL;
pinMode(dirL, OUTPUT);
_dirL = dirL;
// Right motor
pinMode(speedR, OUTPUT);
_speedR = speedR;
pinMode(brkR, OUTPUT);
_brkR= brkR;
pinMode(dirR, OUTPUT);
_dirR = dirR;
// Left motor pin config
int speedL = 3;
int brkL = 9;
int dirL = 12;
// Right motor pin config
int speedR = 11;
int brkR = 8;
int dirR = 13;
*/
}
void Motorz::EStop()
{
analogWrite(_speedL, 0); //Stops Motor L
analogWrite(_speedR, 0); //Stops Motor R
digitalWrite(_brkL, HIGH); //Engage the Brake for Motor L
digitalWrite(_brkR, HIGH); //Engage the Brake for Motor R
}
void Motorz::FwdSlow()
{
//Move both motors forward @ slow speed
digitalWrite(_dirL, HIGH); //Establishes forward direction of Motor L
digitalWrite(_dirR, HIGH); //Establishes forward direction of Motor R
digitalWrite(_brkL, LOW); //Disengage the Brake for Motor L
digitalWrite(_brkR, LOW); //Disengage the Brake for Motor R
analogWrite(_speedL, 170); //Spins Motor L at slow speed
analogWrite(_speedR, 170); //Spins Motor R at slow speed
}
void Motorz::FwdFast()
{
//Move both motors forward @ fast speed
digitalWrite(_dirL, HIGH); //Establishes forward direction of Motor L
digitalWrite(_dirR, HIGH); //Establishes forward direction of Motor R
digitalWrite(_brkL, LOW); //Disengage the Brake for Motor L
digitalWrite(_brkR, LOW); //Disengage the Brake for Motor R
analogWrite(_speedL, 254); //Spins Motor L at fast speed
analogWrite(_speedR, 254); //Spins Motor R at fast speed
}
void Motorz::BwdSlow()
{
//Move both motors backward @ slow speed
digitalWrite(_dirL, LOW); //Establishes backward direction of Motor L
digitalWrite(_dirR, LOW); //Establishes backward direction of Motor R
digitalWrite(_brkL, LOW); //Disengage the Brake for Motor L
digitalWrite(_brkR, LOW); //Disengage the Brake for Motor R
analogWrite(_speedL, 170); //Spins Motor L at slow speed
analogWrite(_speedR, 170); //Spins Motor R at slow speed
}
void Motorz::BwdFast()
{
//Move both motors backward @ fast speed
digitalWrite(_dirL, LOW); //Establishes backward direction of Motor L
digitalWrite(_dirR, LOW); //Establishes backward direction of Motor R
digitalWrite(_brkL, LOW); //Disengage the Brake for Motor L
digitalWrite(_brkR, LOW); //Disengage the Brake for Motor R
analogWrite(_speedL, 254); //Spins Motor L at fast speed
analogWrite(_speedR, 254); //Spins Motor R at fast speed
}
void Motorz::FwdLeft()
{
//Slow Left turn
digitalWrite(_dirL, HIGH); //Establishes forward direction of Motor L
digitalWrite(_dirR, HIGH); //Establishes forward direction of Motor R
digitalWrite(_brkL, LOW); //Disengage the Brake for Motor L
digitalWrite(_brkR, LOW); //Disengage the Brake for Motor R
analogWrite(_speedL, 85); //Spins Motor L at very slow speed
analogWrite(_speedR, 170); //Spins Motor R at slow speed
}
void Motorz::FwdRight()
{
//Slow Left turn
digitalWrite(_dirL, HIGH); //Establishes forward direction of Motor L
digitalWrite(_dirR, HIGH); //Establishes forward direction of Motor R
digitalWrite(_brkL, LOW); //Disengage the Brake for Motor L
digitalWrite(_brkR, LOW); //Disengage the Brake for Motor R
analogWrite(_speedL, 170); //Spins Motor L at slow speed
analogWrite(_speedR, 85); //Spins Motor R at very slow speed
}
void Motorz::BwdLeft()
{
//Slow Left turn
digitalWrite(_dirL, LOW); //Establishes backward direction of Motor L
digitalWrite(_dirR, LOW); //Establishes backward direction of Motor R
digitalWrite(_brkL, LOW); //Disengage the Brake for Motor L
digitalWrite(_brkR, LOW); //Disengage the Brake for Motor R
analogWrite(_speedL, 85); //Spins Motor L at fast speed
analogWrite(_speedR, 170); //Spins Motor R at fast speed
}
void Motorz::BwdRight()
{
//Slow Left turn
digitalWrite(_dirL, LOW); //Establishes backward direction of Motor L
digitalWrite(_dirR, LOW); //Establishes backward direction of Motor R
digitalWrite(_brkL, LOW); //Disengage the Brake for Motor L
digitalWrite(_brkR, LOW); //Disengage the Brake for Motor R
analogWrite(_speedL, 170); //Spins Motor L at fast speed
analogWrite(_speedR, 85); //Spins Motor R at fast speed
}
void Motorz::SpinLeft()
{
//Slow Left turn
digitalWrite(_dirL, LOW); //Establishes backward direction of Motor L
digitalWrite(_dirR, HIGH); //Establishes forward direction of Motor R
digitalWrite(_brkL, LOW); //Disengage the Brake for Motor L
digitalWrite(_brkR, LOW); //Disengage the Brake for Motor R
analogWrite(_speedL, 85); //Spins Motor L at fast speed
analogWrite(_speedR, 85); //Spins Motor R at fast speed
}
void Motorz::SpinRight()
{
//Slow Left turn
digitalWrite(_dirL, HIGH); //Establishes forward direction of Motor L
digitalWrite(_dirR, LOW); //Establishes backward direction of Motor R
digitalWrite(_brkL, LOW); //Disengage the Brake for Motor L
digitalWrite(_brkR, LOW); //Disengage the Brake for Motor R
analogWrite(_speedL, 85); //Spins Motor L at fast speed
analogWrite(_speedR, 85); //Spins Motor R at fast speed
}
void Motorz::Accel()
{
//Slow Left turn
digitalWrite(_dirL, HIGH); //Establishes forward direction of Motor L
digitalWrite(_dirR, LOW); //Establishes backward direction of Motor R
digitalWrite(_brkL, LOW); //Disengage the Brake for Motor L
digitalWrite(_brkR, LOW); //Disengage the Brake for Motor R
for (int j = 0; j < 255; j += 10)
{
analogWrite(_speedL, j); //Spins Motor L accelerating to speed
analogWrite(_speedR, j); //Spins Motor R accelerating to speed
delay (100);
}
}
void Motorz::DeAccel()
{
//Slow Left turn
digitalWrite(_dirL, HIGH); //Establishes forward direction of Motor L
digitalWrite(_dirR, LOW); //Establishes backward direction of Motor R
digitalWrite(_brkL, LOW); //Disengage the Brake for Motor L
digitalWrite(_brkR, LOW); //Disengage the Brake for Motor R
for (int j = 255; j < 0; j -= 10)
{
analogWrite(_speedL, j); //Spins Motor L deaccelerating to stop
analogWrite(_speedR, j); //Spins Motor R deaccelerating to stop
delay (100);
}
}
and finally, the example sketch:
// Link to the Motorz library
#include <Motorz.h>
Motorz Motorz(3, 9, 12, 11, 8, 13);
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
Motorz.Accel();
Motorz.FwdFast();
Motorz.DeAccel();
Motorz.FwdLeft();
Motorz.FwdFast();
Motorz.EStop();
}
What I think is happening is that the sketch calls the first function (currently Motorz.Accel(); ), and then "moves" to the library, where it executes the relevant function.
Once there, it has no way to "get back" to the sketch to see what it should do next.
I'm not sure if I need something at the end of each function within the library, or if I need to call each function in a different way from within the sketch.
Can anyone offer any insights that might help?
Thanks
Andrew