I have built the insect robot described in "Arduino Bots and Gadgets" and having got the base movement code working I decided to rewrite it in a more modular form, but it seems that only the first four lines of the code of my "Move::forward()" function are actually executing based on the serial debugging output. In addition the values passed the the function are not correct. Why would this be?
main.cpp:
#include "WProgram.h"
#include "LED.h"
#include "Move.h"
// Walking speed, between 1 (slow) and 5 (fast).
int walkSpeed = 3;
// Mechanical centre trim positions of the servo's.
int frontServoCenter = 97;
int backServoCenter = 100;
LED *led;
Move *insect;
void blink() {
led->blink(1, 1);
}
int main() {
init();
Serial.begin(115200);
*led = LED(13);
*insect = Move(2, 3, frontServoCenter, backServoCenter);
while (true) {
insect->forward(walkSpeed);
blink();
}
//Unreachable code but it's required by the compiler
return 0;
}
Move.cpp:
#include "Move.h"
const char Move::format[] = "%s: %d,%d";
Move::Move(int frontServoPin, int backServoPin, int frontServoCenter, int backServoCenter) {
frontServo.attach(frontServoPin);
backServo.attach(backServoPin);
frontCenterPos = frontServoCenter;
backCenterPos = backServoCenter;
frontRightUp = frontCenterPos - 18;
frontLeftUp = frontCenterPos + 18;
backRightForward = backCenterPos - 15;
backLeftForward = backCenterPos + 15;
}
void Move::forward(int walkSpeed) {
position("forward-1", frontRightUp, backLeftForward);
motionDelay(125);
center("forward-2c");
motionDelay(65);
position("forward-3", frontLeftUp, backRightForward);
motionDelay(125);
center("forward-4c");
motionDelay(65);
speed(walkSpeed);
}
void Move::position(const char* action, int front, int back) {
log(action, front, back);
frontServo.write(front);
backServo.write(back);
}
void Move::center(const char* action) {
position(action, frontCenterPos, backCenterPos);
}
void Move::speed(int walkSpeed) {
if (walkSpeed < 1) walkSpeed = 1;
if (walkSpeed > 5) walkSpeed = 5;
int millisDelay = 300 - (50 * walkSpeed);
motionDelay(millisDelay);
}
void Move::motionDelay(int millis) {
Serial.println(millis);
delay(millis);
}
void Move::log(const char* message, int x, int y) {
char buf[256];
snprintf(buf, sizeof(buf), format, message, x, y);
Serial.println(buf);
}
Serial debugging output:
forward-1: 79,0
125
forward-2c: 97,100
65
forward-1: 79,0
125
forward-2c: 97,100
65
forward-1: 79,0
125
forward-2c: 97,100
65
forward-1: 79,0
125
forward-2c: 97,100
65
forward-1: 79,0
125
forward-2c: 97,100
65
forward-1: 79,0
125
forward-2c: 97,100
65
forward-1: 79,0
125
forward-2c: 97,100
65
forward-1: 79,0
125
forward-2c: 97,100
65