Well I don't think it is a memory problem. I tried two different memory tests down in the innermost loop. One showed 638 bytes free, the other, 697.
For reference, here's my code, I'd sure appreciate the help. I wonder if the NECIRrcv library is colliding with servoTimer1 in some way. When I uncomment the ir.begin() line, things go bonkers.
#include <ServoTimer1.h>
#include <NECIRrcv.h>
int eyePin = 0;
int servoPin = 10;
int bumperPin = 7;
int motor_leftdig=4;
int motor_leftpwm=5;
int motor_rightdig=12;
int motor_rightpwm=11;
int irPin = 8;
/* Object constants */
#define DANGER_DISTANCE 500
#define MAX_SAFE_DISTANCE 400
/* Servo motor contstants */
#define SERVO_STEP_SIZE 5
#define NUM_SERVO_STEPS 36
#define STRAIGHT_AHEAD 18
/* Motor speeds. PWM output pins turn 0-255 into 0-5v */
#define NO_SPEED 0
#define HALF_SPEED 127
#define FULL_SPEED 255
#define ON 0
#define OFF 1
int view[NUM_SERVO_STEPS];
ServoTimer1 servo;
NECIRrcv ir(irPin);
void setup() {
pinMode(motor_leftdig, OUTPUT);
pinMode(motor_rightdig, OUTPUT);
servo.attach(servoPin);
moveServo(STRAIGHT_AHEAD);
// Serial.begin(9600); /* For debugging purposes only */
// ir.begin();
}
void loop() {
static int remoteControl = OFF;
if (remoteControl == ON) {
stopMoving();
}
else { // Remote control = OFF
/* If we hit something with the bump switch, back up and turn around */
if (digitalRead(bumperPin) == HIGH) {
stopMoving();
reverse();
delay(1000);
stopMoving();
turnAround(bestDirection());
}
/* Debounce the Sharp IR sensor. I suspect a capacitor would work just as well... /
int distance = analogRead(eyePin);
while (abs(distance - analogRead(eyePin)) > 20) {
distance = analogRead(eyePin); / Debounce */
}
/* Too close to something? Stop and turn around */
if (distance > DANGER_DISTANCE) {
stopMoving();
turnAround(bestDirection());
}
/* Getting close to something, slow down */
else if (distance > MAX_SAFE_DISTANCE) {
halfSpeed();
}
/* All clear ahead, full on */
else {
moveStraight();
}
}
}
void moveServo(int distance) {
servo.write(distanceSERVO_STEP_SIZE);
delay(50); / For the servo to settle */
}
/*
- Routine to figure out where to rotate to get the most unobstructed angle.
/
int bestDirection() {
/
- Rotate the servo, building a map of the obstacles we see in front of us.
- Record the position that is most unobstructed, scaled up to use as a delay factor when turning.
*/
refresh_view(0, NUM_SERVO_STEPS);
int safest = furthestView();
return ((safest - STRAIGHT_AHEAD) * 50);
}
/* Routine to return the least number in the view[] array. */
int furthestView() {
int furthest=view[0];
int servo_position=0;
for (int i=0; i<NUM_SERVO_STEPS; i++) {
if (view < furthest) {
_ furthest = view*;_
servo_position = i;
_ } _
_ }_
return(servo_position);
_}_
_/ Routine to scan around, recording what we see where /
void refresh_view(int start, int end) {
_ for (int i=start; i<end; i++) {
_ moveServo(i);_
_ / Debounce the Sharp IR sensor /
while (abs(analogRead(eyePin) - view) > 10) {
view = analogRead(eyePin);
}
}_
moveServo(STRAIGHT_AHEAD);
_}
/ Negative turnAmount means turn left, positive turnAmount means turn right /
void turnAround(int turnAmount) {
if (turnAmount < 0) {_
setSpeed(-FULL_SPEED, FULL_SPEED);
_ }
else {_
setSpeed(FULL_SPEED, -FULL_SPEED);
_ }
delay(500); / Arbitrary delay factor, it just works well /
}
void moveStraight() {_
setSpeed(FULL_SPEED, FULL_SPEED);
_}
void stopMoving() {_
setSpeed(NO_SPEED, NO_SPEED);
_ delay(500);
}
void halfSpeed() {_
setSpeed(HALF_SPEED, HALF_SPEED);
_}
void reverse() {_
setSpeed(-FULL_SPEED, -FULL_SPEED);
_ delay(500);
}
void setSpeed(int left, int right) {
if (left >= 0) { / Forward /_
digitalWrite(motor_leftdig, LOW);
analogWrite(motor_leftpwm, left);
_ }
else { / Reverse /_
digitalWrite(motor_leftdig, HIGH);
analogWrite(motor_leftpwm, FULL_SPEED+left);
_ }
if (right >= 0) { / Forward /_
digitalWrite(motor_rightdig, LOW);
analogWrite(motor_rightpwm, right);
_ }
else { / Reverse /_
digitalWrite(motor_rightdig, HIGH);
analogWrite(motor_rightpwm, FULL_SPEED+right);
_ }
}*_