Trying to use 'switch cases' to operate 8 different outcomes but I can't get it to work

Everything above Control Buzzer and Vibrator Motor works. It is just determining whether the board is moving using the built in IMU and the gyro, acc and mag, it also measures angles in degrees of the acc which I haven't written into the code yet but will add further control over the Control Buzzer and Vibrator Motor which is in the void Eyes() loop.
Below Control Buzzer and Vibrator Motor is to activate the buzzer and motor when each case statement is true whilst the board is determined as moving which is done by
// Move eyes whenever not still
if (!IsStill)
{ Eyes();.

which activates Void EyesThat, is all it does. It was designed this way so anything in the Void Eyes is controlled by the code above determining board movement.

analogWrite only works on PWM pins, so you cannot use A3, only the digital pins are PWM capable.

Also, you never set motorPin to output mode.

Which motor, link please. There is a possibility that if you activate the motor, too much current is drawn. Hence the request for a schematic so we can see how everything is wired exactly (including power).

I originally wrote this on a nano board and it worked using the exact code and pins. So is nano33ble pins different?

the output is operating a transistor switch which is operating the motor which is a 3V vibrator such as in mobile devices. 3V as nano33BLE operates on 3V. Buzzer is operated on the A3 output, the transistor to control the motor via D6 and negative to GND

It should never have worked on a normal Nano. A0 to A7 are analog inputs, not PWM capable pins. On the normal Nano, analogWrite() will translate to digitalWrite() for those pins.

Not sure about the tone function.

And I have no idea about the Nano BLE.

#define buzzerneg 12     //pin 12 buzzer negative GND 
#define buzzerpos A3     //A3 buzzer output


unsigned int LaserMeasurement = 0;

#define motorPin D6    //vibrator motor output
int SW1 = 0;

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

}



  
      

I knew that there was a motor and where it was connected :wink: Is it connected directly or via a driver?

How much current does the motor draw? For which voltage is it rated? Hence the request for a link to the product.

The Nano33BLE is a 3.3V device and its pins can only deliver 15 mA according to the technical specs on https://store.arduino.cc/arduino-nano-33-ble. The original Nano could deliver a maximum of 40 mA and was a 5V device.

transistor switch, motor powered from a battery

1 Like

Thanks, sorry that I missed that.

No idea at this moment.

in would you said, as the buzzer was direct to A3 from the Nano board, I may have to put that off another transistor for enough current a 3.3V off the battery. The whole system including the laser Adafruit_VL53L0X run on 3V, which is why I went from using the sonars which really need 5v

I do not see pinMode anywhere in the code, so you never set D6 or A3 as outputs, although its possible the tone library sets A3 as output.
Do you hear the buzzer, and does the motor turn on?

I have tested the buzzer and the motor with simple codes to get them to operate and they do work on current pins and setup. Just that last part of the code, switch code sketch or the if, else if, else, sketch which still baffles me. I'm expecting it to be something simple that I am overlooking it. Frustrating but usually what happens. I am on a huge learning curve with programming arduino, only learnt and use logic programming in Siemens and Mitsubishi PLC for industrial control systems.

This is the Nano code and it works 100%

For the normal Nano, setting the pinMide is done in analogWrite() if I recall correctly. It might be different for the Nano33BLE.

yes that is how it is on the Nano, I thought from comparing the Nano and the Nano33BLE and NanoBLESense, other than having extra sensors and run on 3.3V, all else was transferable in hardware wiring and programming. I couldn't find anywhere that said otherwise.

Thanks guys, appreciate your help so far, excuse for a little while, I have a spinal injury and can only do so much at a time before my pain levels become unbearable. Need a little break. :beers:

For some of us it's bed time. For me the day starts now :wink: But I'm actually tired because I got up at 3 am :frowning:

I'm in Australia. Thank you for your time. Stay safe and healthy.