Below is my original nano code. It worked 100%. As you would see is that two sonars were used. This was to determine movement without a IMU. the control statements controlling the buzzer worked 100%.
When I found the nano33BLE I thought this would enable me to only need one sensor to read distance and the onboard IMU for movement. Which I have achieved, but can't get the outputs of controlling the motor and buzzer to function.
In the nano sketch you will see only a buzzer was used. That was the case, but introduction of the motor in the nano33BLE sketch doesn't account for the failure of the code that I can see.
NANO CODE:
#include <Arduino_LSM9DS1.h>
const unsigned long StandingStillInterval = 2000;
const float MagnitudeThreshhold = 0.5; // Determine by experiment
float x, y, z;
float oldX, oldY, oldZ;
long IsStill = false; // board movement setting
static unsigned long timeLastMoved = 0;
float degreesX = 0;
float degreesY = 0;
float degreesZ = 0;
//sr-04-1 is sonar1 detecting obstacles in front of dog
//sr-04-2 is sonar2 detecting if the dog is lying down
#define pingPin1 2 //trig pin of sr04-1
#define echoPin1 3 //echo pin of sr04-1
#define pingPin2 4 //trig pin of sr04-2
#define echoPin2 5 //echo pin of sr04-2
#define buzzerneg 12 //pin 12 buzzer negative GND
#define buzzerpos A3 //A3 buzzer output
long duration1, duration2;
int distance1, distance2;
void setup()
{
Serial.begin(9600);
while (!Serial);
Serial.println("Started");
if (!IMU.begin())
{
Serial.println("Failed to initialize IMU!");
while (1);
}
Serial.print("Accelerometer sample rate = ");
Serial.print(IMU.accelerationSampleRate());
Serial.println("Hz");
{
Serial.begin(9600); // Starting Serial Terminal
pinMode(pingPin1,OUTPUT); //sonar 1
pinMode(echoPin1,INPUT); //sonar 1
pinMode(12,OUTPUT); //pin12 is used as GND pin for buzzer since arduino nano has only two GND pins
pinMode(A3,OUTPUT); //pin A3 provides the output on buzzer
pinMode(pingPin2,OUTPUT); //sonar 2
pinMode(echoPin2,INPUT); //sonar 2
}
Serial.println("Adafruit VL53L0X test");
if (!lox.begin()) {
Serial.println(F("Failed to boot VL53L0X"));
while(1);
}
// power
Serial.println(F("VL53L0X API Simple Ranging example\n\n"));
}
void loop()
{
//static unsigned long timeLastMoved = 0;
if (HasMoved())
{
timeLastMoved = millis();
if (millis() - timeLastMoved < StandingStillInterval)
{
IsStill = false;
Serial.print("Started moving again at ");
Serial.println(timeLastMoved);
}
}
if (millis() - timeLastMoved > StandingStillInterval)
{
if (!IsStill)
{
IsStill = true;
Serial.print("Stopped moving at ");
Serial.println(timeLastMoved);
Serial.print("Has been still for 2 seconds at ");
Serial.println(millis());
}
}
// Move eyes whenever not still
if (!IsStill)
{ Eyes();
}
}
float square(const float f)
{
return f * f;
}
boolean HasMoved()
{
if (!IMU.accelerationAvailable())
return false; // Can't tell if it has moved because data isn't available
IMU.readAcceleration(x, y, z);
float magnitudeOfChange = square(x - oldX) + square(y - oldY) + square(z - oldZ);
//{ Serial.print("magnitude of change: ");
//Serial.println(magnitudeOfChange);
//}
oldX = x;
oldY = y;
oldZ = z;
return magnitudeOfChange > MagnitudeThreshhold; //measurement of board movement and comparing if greater than Magnitude Threshhold
}
//}
void Eyes() //Electronic eyes
{
//if (HasMoved())
( IsStill == false);
{ Serial.println(" Started eyes scanning ");
//Serial.println(timeLastMoved);
}
if (IMU.accelerationAvailable())
IMU.readAcceleration( x, y, z );
float fx, fy, fz;
//Angle Measurment code
{
if (x > 0.1) //activate angle measurement if within set boundaries
{
fx = x * 100;
degreesX = map(fx, 0, 97, 0, 90);
Serial.print(" Tilting up ");
Serial.print(degreesX);
Serial.println(" degrees");
delay(500);
}
if (x < -0.1)
{
fx = x * 100;
degreesX = map(fx, 0, -100, 0, 90);
Serial.print("Tilting down ");
Serial.print(degreesX);
Serial.println(" degrees");
delay(500);
}
if (y > 0.1)
{
fy = y * 100;
degreesY = map(fy, 0, 97, 0, 90);
Serial.print("Tilting left ");
Serial.print(degreesY);
Serial.println(" degrees");
delay(500);
}
if (y < -0.1)
{
fy = y * 100;
degreesY = map(fy, 0, -100, 0, 90);
Serial.print("Tilting right ");
Serial.print(degreesY);
Serial.println(" degrees");
delay(500);
}
if (y > 0.1)
{
fz = z * 100;
degreesZ = map(fz, 0, 97, 0, 90);
Serial.print(" Tilting up ");
Serial.print(degreesZ);
Serial.println(" degrees");
delay(500);
}
if (z < -0.1)
{
fz = z * 100;
degreesZ = map(fz, 0, -100, 0, 90);
Serial.print("Tilting down ");
Serial.print(degreesZ);
Serial.println(" degrees");
delay(500);
}
}
digitalWrite(12, LOW); //Buzzer GND is always low
//send a signal at ping pin at an interval of 0.002 seconds to check for an object
//sonar 1
digitalWrite(pingPin1, LOW);
delayMicroseconds(2);
digitalWrite(pingPin1, HIGH);
delayMicroseconds(10);
digitalWrite(pingPin1, LOW);
//read the duration of the sound wave with echoPin1
duration1 = pulseIn(echoPin1, HIGH);
//calculate distance1 based on duration of ultrasound from triggerPin to echoPin1
distance1 = duration1*0.034/2;
//add thresholds to correct for sensor value errors
// if (distance1 < 0 ) distance1 = 0;
// if (distance1 > 200) distance1 = 200;
//sonar 2
digitalWrite(pingPin2, LOW);
delayMicroseconds(100);
digitalWrite(pingPin2, HIGH);
delayMicroseconds(5000);
digitalWrite(pingPin2, LOW);
//read the duration of the sound wave with echoPin2
duration2 = pulseIn(echoPin2, HIGH);
//calculate distance2 based on duration of ultrasound from triggerPin to echoPin
distance2 = duration2*0.034/2;
//add thresholds to correct for sensor value errors
//if (distance2 < 0 ) distance2 = 0;
// if (distance2 > 200) distance2 = 200;
/*==================================
* Print to Serial Monitor
==================================*/
Serial.print("distance1: ");
Serial.print(distance1);
Serial.print("cm, distance 2:");
Serial.print(distance2);
Serial.println("cm ");
/*==================================
* Control Buzzer
==================================*/
if (distance2 > 5 && distance1 <30 && distance1 >20)
{analogWrite(A3,255);
tone(A3, 25000, 1000); // tone on pin 8 at 25000 Hz for 1 second
delay(1000);
analogWrite(A3,0);
delay(1000); } //sound buzzer every second if obstacle distance is between 20-30cm.
else if (distance2 > 5 && distance1 <20 && distance1 >15)
{analogWrite(A3,255);
tone(A3, 30000, 500); // tone on pin 8 at 30000 Hz for 1/2 second
delay(500);
analogWrite(A3,0);
delay(500); } //sound buzzer every 0.5 seconds if obstacle distance is between 10-20cm.
else if (distance2 > 5 && distance1 <15 && distance1 >5)
{analogWrite(A3,255);
tone(A3,40000, 100); // tone on pin 8 at 40000 Hz for 0.1 second
delay(100);
analogWrite(A3,0);
delay(100); } //sound buzzer every 0.1 seconds if obstacle distance is between 0-10cm.
else
analogWrite(A3,0); //do not sound the buzzer
}