Hello, and thank you in advance.
I’m a teacher in Mozambique. I brought a few Zumo kits with me for some afterschool activities and the kids and I are learning and programming them together. I also happen to have a bunch of HC-SR04s in my tool box, so I am trying to get them to play nicely together. My coding skills are pretty basic, but I’ve already combed the interweb pretty thoroughly and tried a bunch of solutions, so I thought I’d try here.
Nothing fancy to begin with - mostly modifying the sample BorderDetect code so far, with the basic idea to have “Normal Speed” and an “Attack Speed” when another bot gets close.
I can get my rangefinding code to return good data in a non-Zumo sketch, with the same pins, so I know the HC-SR04 works, and the connections are good.
This works fine:
#include <NewPing.h>
#define TRIGGER_PIN 6
#define ECHO_PIN 3
#define MAX_DISTANCE 200
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
void setup() {
Serial.begin(115200);
}
void loop() {
delay(50);
int uS = sonar.ping_cm();
Serial.print("Distance: ");
Serial.print(uS);
Serial.println("cm");
if (uS >= 1 && uS <= 15)
{
Serial.print("ATTACK");
}
else
{
Serial.print("All clear!");
}
}
But when I insert the same basic thing into the Zumo code (I’ve inserted the whole thing below), the HC-SR04 seems to return only zeros, and never shifts into Attack Mode.
I’m using a preassembled Zumo kit with an Arduino Uno R3. I have the Trigger and the Echo pins of the HC-SR04 plugged into pins 3 & 6. (I disabled the buzzer, and removed the buzzer library and code from the example to free up pin 3).
Here is my (rather crude) modification of the Zumo Border Detect code, without the buzzer stuff, and with some serial I used to try to troubleshoot.
#include <ZumoMotors.h>
#include <Pushbutton.h>
#include <QTRSensors.h>
#include <ZumoReflectanceSensorArray.h>
#include <NewPing.h>
#define LED 13
#define TRIGGER_PIN 3
#define ECHO_PIN 6
#define MAX_DISTANCE 200
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
// this might need to be tuned for different lighting conditions, surfaces, etc.
#define QTR_THRESHOLD 1500 // microseconds
// these might need to be tuned for different motor types
#define REVERSE_SPEED 200 // 0 is stopped, 400 is full speed
#define TURN_SPEED 200
#define ATTACK_SPEED 400
#define FORWARD_SPEED 50
#define REVERSE_DURATION 200 // ms
#define TURN_DURATION 300 // ms
ZumoMotors motors;
Pushbutton button(ZUMO_BUTTON); // pushbutton on pin 12
#define NUM_SENSORS 6
unsigned int sensor_values[NUM_SENSORS];
ZumoReflectanceSensorArray sensors(QTR_NO_EMITTER_PIN);
void waitForButtonAndCountDown()
{
digitalWrite(LED, HIGH);
button.waitForButton();
digitalWrite(LED, LOW);
// Three Second Delay Start
delay(3000);
}
void setup()
{
pinMode(LED, HIGH);
waitForButtonAndCountDown();
// Serial.begin(115200);
}
void loop()
{
if (button.isPressed())
{
// if button is pressed, stop and wait for another press to go again
motors.setSpeeds(0, 0);
button.waitForRelease();
waitForButtonAndCountDown();
}
// read sensors
delay(50);
int uS = sonar.ping_cm();
// Serial.print("Distance: ");
// Serial.print(uS);
// Serial.println("cm");
delay(50);
sensors.read(sensor_values);
if (sensor_values[0] < QTR_THRESHOLD)
{
// if leftmost sensor detects line, reverse and turn to the right
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)
{
// if rightmost sensor detects line, reverse and turn to the left
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
{
// otherwise, go straight
// If proximity sensor detects something ... ATTACK SPEED
if (uS >= 1 && uS <= 15)
{
// Serial.print("ATTACK");
motors.setSpeeds(ATTACK_SPEED, ATTACK_SPEED);
}
// Otherwise ... forward at normal speed
else
{
// Serial.print("All clear!");
motors.setSpeeds(FORWARD_SPEED, FORWARD_SPEED);
}
}
}
Thanks, and I apologize in advance if it is too obvious.