mikey
YaBB Newbies
Offline

Arduino rocks
Posts: 10
|
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(distance*SERVO_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[i] < furthest) { furthest = view[i]; 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[i]) > 10) { view[i] = 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); } }
|