Arduino/Zumo locked into reverse and turn

#include <Wire.h>
#include <ZumoShield.h>
#include <NewPing.h>
#include <QTRSensors.h>

#define LED 13
#define QTR_THRESHOLD 1500

#define REVERSE_SPEED 225
#define TURN_SPEED 200
#define ATTACK_SPEED 300
#define FORWARD_SPEED 200
#define REVERSE_DURATION 250
#define TURN_DURATION 250

ZumoMotors motors;
Pushbutton button(ZUMO_BUTTON);

#define NUM_SENSORS 6
unsigned int sensor_values[NUM_SENSORS];

#define TRIGGER_PIN 4
#define ECHO_PIN 5
#define MAX_DISTANCE 200

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

ZumoReflectanceSensorArray sensors(QTR_NO_EMITTER_PIN);


void waitForButtonAndCountDown()
{
  digitalWrite(LED, HIGH);
  button.waitForButton();
  digitalWrite(LED, LOW);
  delay(3000);
  
}

void setup()
{

  pinMode(LED, HIGH);
  pinMode(TRIGGER_PIN, OUTPUT);  
	pinMode(ECHO_PIN, INPUT);


  waitForButtonAndCountDown();
}

void loop()
{
  if (button.isPressed())
  {
    motors.setSpeeds(0, 0);
    button.waitForRelease();
    waitForButtonAndCountDown();
  }
  

  //delay();
  int distance = sonar.ping_cm();

  
  sensors.read(sensor_values);

  if (sensor_values[0] < QTR_THRESHOLD)
  {
    motors.setSpeeds(-REVERSE_SPEED, -REVERSE_SPEED);
    delay(REVERSE_DURATION);
    motors.setSpeeds(TURN_SPEED, -TURN_SPEED);
    delay(TURN_DURATION);
    motors.setSpeeds(FORWARD_SPEED, FORWARD_SPEED);
  }
  else if (sensor_values[5] < QTR_THRESHOLD)
  {
    motors.setSpeeds(-REVERSE_SPEED, -REVERSE_SPEED);
    delay(REVERSE_DURATION);
    motors.setSpeeds(-TURN_SPEED, TURN_SPEED);
    delay(TURN_DURATION);
    motors.setSpeeds(FORWARD_SPEED, FORWARD_SPEED);
  }
  else if (distance >= 1 && distance <= 15)
    {
      motors.setSpeeds(ATTACK_SPEED, ATTACK_SPEED);
    }
  else
  {
    motors.setSpeeds(FORWARD_SPEED, FORWARD_SPEED);
  }
}

This is my code above, changed to D-Pin 4 and 5 for my HC-SR04 sensor but this seems to be the part still causing me issues. when i disconnect the sensor, the motors work fine but i cant seem to figure out if its the conditions ive set thats causing me issues.

this is for my mini zumo robot, im using a zumo shield and an arduino uno.

Hello.

I moved your post to the “Robots” section of the forum since it seemed more appropriate.

From your code, it looks like you are trying to use the line sensor array; however, the line sensors use pins 4 and 5 by default (as well as A2, A0, 11, and A3). So, that conflict is probably causing problems.

It looks like you might be using this new ping library, which supports a one-pin mode; if that’s the case, you might try using that with pin 2 (which should be free when using an Arduino Uno). If the one pin mode does not work for you, you could free up either pin A1 or pin 3 by removing their associated jumper, if you do not need the batter level monitoring feature or buzzer, respectively. You can find more information about this in the “Jumper settings” section of the Zumo shield user’s guide.

Brandon

1 Like