Arduino Nano RP2040

Generic question, that I'm unable to figure out. I've copied the code for ble device to device. (https://docs.arduino.cc/tutorials/nano-rp2040-connect/rp2040-ble-device-to-device/)
This works like a champ for what it is.
I want to do some other functions, such as reading and responding to the built in accelerometer. (I tested this code without BLE and it worked as expected)
Where do I put this code?
I tried putting it at the beginning of my main loop, and had to hard reset my arduino.
Should it go
where I put it, and I just failed,
inside the BLEDevice peripheral = BLE.available(); function
inside the controlLed(BLEDevice peripheral) function
or am I trying to do something that is not allowed.

The general idea is to read the button press data from the other arduino (the remote), and only take action if a button is pressed. Otherwise continue to determine if the accelerometer is being held within the desired band

Any guidance is greatly appreciated!!

Code Below

void setup() {
//  pinMode(LED_BUILTIN, OUTPUT);
  // Set LED Pins as Outputs
  pinMode (LED_Array[0], OUTPUT);
  pinMode (LED_Array[1], OUTPUT);
  pinMode (LED_Array[2], OUTPUT);

  Serial.begin(9600);
  while (!Serial);
  // initialize the BLE hardware
  BLE.begin();
  Serial.println("BLE Central - LED control");
  // start scanning for Button Device BLE peripherals
  BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214");
}
void loop() {
  float x, y, z;              // instantaneous axis values from acceleromter
  float sumx, sumy, sumz;     // sum of axis values
  float avg = 1000.0;         // total number of values to average
  int i;                          // counting integer
  int off_lvl = 1;                     // Value out of range
  
Serial.println("I'm in the main loop");

    sumx = 0.0;     // initialize averaging sum to 0
    sumy = 0.0;
    sumz = 0.0;
    for (i=0; i<=avg; i++){           // loop for a predetermined number of cycles
      IMU.readAcceleration(x, y, z);  // read accelerometer values
      sumx = sumx + x;                // sum values
      sumy = sumy + y;
      sumz = sumz + z;
    }
    x = sumx / avg;                   // find average of each axis
    z = sumy / avg;
    z = sumz / avg;

    if (Hi_array){
      if (z < Array_vals[Hi_index]){
        Color_Select(1, 0, 0); // Turn Red only on
        off_lvl = 99;
      }
    } else {
      if (x > Array_vals[Hi_index]){
        Color_Select(1, 0, 0); // Turn Red only on
        off_lvl =98;
      }
    }  
    if (Lo_array){
      if (z > Array_vals[Lo_index]){
        Color_Select(0, 0, 1); // Turn Yellow only on
        off_lvl = 97;
      }
    } else {
      if (x < Array_vals[Lo_index]){
        Color_Select(0, 0, 1); // Turn Green only on
       off_lvl = 96;
      }
    }  
    if (off_lvl == 1){
        Color_Select(0, 1, 0); // Turn Green only on
    }
    off_lvl = 1;                   // Reset fault each round

    switch (Butt_Pressed) {             // Switch statement to generate action for button presses
      case 0:
        Serial.println("Set");             // Set / Reset the High Low values
        set_points (x,z);
        break;
      case 1:
        Serial.println("Increase Span");        // Make life easier
        change_span (1);
        break;
      case 2:
        Serial.println("Decrease Span");        // Make life harder
        change_span (0);
        break;
      case 3:
        Serial.println("Raise ");           // Raise 
        recenter (1);
        break;
      case 4:
        Serial.println("Lower ");           // Lower 
        recenter (0);
        break;
      case 5:
        Serial.println("Save Current Setpoints"); // Save the current setting TBD
        //set_points (x,z);
        break;
      case 6:
        Serial.println("Send saved setpoints");   // Recall previous setting TBD
        //set_points (x,z);
        break;
      case 7:
        break;                            // No button pressed
      default:
        Serial.println("Mult-button Press");      // Multiple buttons pressed TBD
        //set_points (x,z);
        break;
    }

  // check if a peripheral has been discovered
  BLEDevice peripheral = BLE.available();
  if (peripheral) {
    // discovered a peripheral, print out address, local name, and advertised service
    Serial.print("Found ");
    Serial.print(peripheral.address());
    Serial.print(" '");
    Serial.print(peripheral.localName());
    Serial.print("' ");
    Serial.print(peripheral.advertisedServiceUuid());
    Serial.println();
    if (peripheral.localName().indexOf("Button Device") < 0) {
      Serial.println("No 'Button Device' in name");
      return;  // If the name doesn't have "Button Device" in it then ignore it
    }
    // stop scanning
    BLE.stopScan();
    controlLed(peripheral);
    // peripheral disconnected, start scanning again
    BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214");
  }
}
void controlLed(BLEDevice peripheral) {
  // connect to the peripheral
  Serial.println("Connecting ...");
  if (peripheral.connect()) {
    Serial.println("Connected");
  } else {
    Serial.println("Failed to connect!");
    return;
  }
  // discover peripheral attributes
  Serial.println("Discovering attributes ...");
  if (peripheral.discoverAttributes()) {
    Serial.println("Attributes discovered");
  } else {
    Serial.println("Attribute discovery failed!");
    peripheral.disconnect();
    return;
  }
  // retrieve the LED characteristic
  BLECharacteristic LEDCharacteristic = peripheral.characteristic("19b10001-e8f2-537e-4f6c-d104768a1214");
  if (!LEDCharacteristic) {
    Serial.println("Peripheral does not have LED characteristic!");
    peripheral.disconnect();
    return;
  }
  while (peripheral.connected()) {
    // while the peripheral is connected
    Butt_Pressed = Num_Buttons;
    if (LEDCharacteristic.canRead()) {
      byte value = LEDCharacteristic.read();
      LEDCharacteristic.readValue(value);
      Butt_Pressed = value;
    }
    delay(500);
  }
  Serial.println("Peripheral disconnected");
}

how did it fail? give details ? serial monitor output?

the program in post 1 appears to be missing code before setup()

which RP2040 are you using? give a link?

which accelerometer. are you using? give a link? how is it connected to the RP2040?

if tested in a separate program does the accelerometer. work OK?

1 Like

I failed to copy the accel init into my init loop.
I appreciate you pointing me back to the accel.
Got it running now. Thanks!

For your questions:
Arduino Nano RP2040 Connect https://docs.arduino.cc/hardware/nano-rp2040-connect/
On-board accelerometer LSM6DSOX

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.