Showing posts with label RP2040. Show all posts
Showing posts with label RP2040. Show all posts

Tuesday, December 20, 2022

Adafruit Accessories Quad Level-Shifter and NeoPixel Ring

Today I'll be using a 74AHCT125N  quad level-shifter to use a 5V 24 neoPixel ring with my Arduino Nano Connect. It is on pin 4.

That part was pretty easy. Wiring followed the adafruit reference. The only problem was sending the programmable input/output (PIO) state machine commands from one of the cores. This would cause animations to stall while running CPU-heavy instructions. I could run it on core0 since it will just be handling BLE communications. I would like it to be able to run the lights in some preset animations. Ideally independently while taking some FIFO interrupts from the main core. PIOs are capable of looping small snippets. I've only used PIOs once before using a Pi Pico and micropython.
Time for a dive into some assembly language with pioasm. The pico c sdk datasheet has great info on this at page 33.

Here is what I have so far:
#include <NeoPixelConnect.h>
#define MAXIMUM_NUM_NEOPIXELS 24

// Create an instance of NeoPixelConnect and initialize it
// to use GPIO pin 4 (D12) as the control pin, for a string
// of 8 neopixels. Name the instance p
NeoPixelConnect p(4, MAXIMUM_NUM_NEOPIXELS, pio0, 0);

// this array will hold a pixel number and the rgb values
uint8_t random_pixel_setting[4];
uint8_t waiting_pixel_setting[4];

int adjDelay = 0;
uint16_t runner = 0;
uint16_t leadPixel = 0;
void waiting(){
  for( int i = MAXIMUM_NUM_NEOPIXELS-1; i >= 0; i--){//leadPixel update
    //leadPixel = i;
    for(int j = 0; j < MAXIMUM_NUM_NEOPIXELS; j++){//pixel update
      //Lead pixel will be i + j
      uint8_t pixel = i + j;
      while(pixel >= MAXIMUM_NUM_NEOPIXELS){
        pixel -= MAXIMUM_NUM_NEOPIXELS;
      }
      waiting_pixel_setting[0] = pixel;
      // Serial.print("Pixel:");
      // Serial.println(pixel);
     
      waiting_pixel_setting[3] = random( 255 - ((255 / MAXIMUM_NUM_NEOPIXELS) * j) - (255 / MAXIMUM_NUM_NEOPIXELS) ,255 - ((255 / MAXIMUM_NUM_NEOPIXELS) * j));
      // Serial.print("B:");
      // Serial.println(waiting_pixel_setting[3]);
     
      waiting_pixel_setting[1] = waiting_pixel_setting[3] / random(3, 5);
      // Serial.print("R:");
      // Serial.println(waiting_pixel_setting[1]);
     
      waiting_pixel_setting[2] = waiting_pixel_setting[3] / random(5, 10);
      // Serial.print("G:");
      // Serial.println(waiting_pixel_setting[2]);
     
      p.neoPixelSetValue(pixel, waiting_pixel_setting[1], waiting_pixel_setting[2], waiting_pixel_setting[3], false);
    }
    p.neoPixelShow();
    delay(adjDelay++);
    if(adjDelay == 120){
      adjDelay = 0;
      Serial.println("Reset!");
    }
  }
  Serial.println(urgb_u32(waiting_pixel_setting[1], waiting_pixel_setting[2], waiting_pixel_setting[3]) << 8u);
}
uint32_t urgb_u32(uint8_t r, uint8_t g, uint8_t b) {
    return
            ((uint32_t)(r) << 8) |
            ((uint32_t)(g) << 16) |
            (uint32_t)(b);
}
// select a random pixel number in the string
uint8_t get_pixel_number(){
  if(runner == MAXIMUM_NUM_NEOPIXELS){
    runner = 0;
  }
  return (uint16_t)runner++;
}

// select a random intensity
uint8_t get_pixel_intensity(){
    return((uint8_t)random(25,100));
}

void get_random_pixel_and_color(){
    random_pixel_setting[0] = get_pixel_number();
    random_pixel_setting[1] = get_pixel_intensity();
    random_pixel_setting[2] = get_pixel_intensity();
    random_pixel_setting[3] = get_pixel_intensity();
}

void setup(){
    Serial.begin(115200);
    // while(!Serial){//Wait for serial to come up
    // sleep_ms(100);  
    // }
    Serial.println("In setup");
    //waiting();
}

void loop(){

  waiting();
  //delay(1000);
}



References:

https://datasheets.raspberrypi.com/pico/raspberry-pi-pico-c-sdk.pdf

https://raspberrypi.github.io/pico-sdk-doxygen/group__hardware__pio.html

https://learn.adafruit.com/neopixels-on-raspberry-pi/raspberry-pi-wiring

https://github.com/MrYsLab/NeoPixelConnect

https://forum.arduino.cc/t/programming-pio-with-pico/704290

https://stackoverflow.com/questions/795286/what-does-the-question-mark-character-mean-in-c

Thursday, August 11, 2022

Pinky: Android RC control over Bluetooth to Arduino Nano PR2040 Connect.

 For remote control of Pinky the octobot I had a choice of wifi, Bluetooth, or an old drone TX and RX set up. I'm choosing to go with Bluetooth because everybody with a phone has the capability in their pockets. The old drone RX/TX setup would require additional components and take up internal cavity space in the robot. After two nights of testing I finally got BLE working last night by following these resources:


Tonight, 8/11/22, I'll try and build a joystick and control scheme into the app inventor to push command controls to the Arduino. After that, I'll try to port Pinky's old micropython codebase to the Arduino programming language and convert all of its classes to headers....
Update:8/13/2022
Finally got an app I am okay with. The code is vast, so if you want to see it, just download it to view it outside your browser.
Time to start working on the arduino code.
#include <ArduinoBLE.h>

#include <Arduino_LSM6DSOX.h>

BLEService RCService("27810000-4786-42e7-bd27-45257b2e988f"); // BLE LED Service
// BLE AppInventorRC Characteristic - custom 128-bit UUID, read and writable by central
BLEIntCharacteristic RC_AngleCharacteristic("27810001-4786-42e7-bd27-45257b2e988f", BLERead | BLEWrite | BLEWriteWithoutResponse);
BLEIntCharacteristic RC_SpeedCharacteristic("27810002-4786-42e7-bd27-45257b2e988f", BLERead | BLEWrite | BLEWriteWithoutResponse);
BLEIntCharacteristic RC_HeightCharacteristic("27810003-4786-42e7-bd27-45257b2e988f", BLERead | BLEWrite | BLEWriteWithoutResponse);
BLEIntCharacteristic RC_RotationCharacteristic("27810004-4786-42e7-bd27-45257b2e988f", BLERead | BLEWrite | BLEWriteWithoutResponse);

int LastSpeed = 0;
int LastAngle = 0;
int LastHeight = 0;
int LastRot = 0;

int tick = 0;
bool CentralConnected = 0;
//IMU
float x, y, z;

void setup() {
  Serial.begin(9600);
  // begin initialization
  if (!BLE.begin()) {
    Serial.println("starting Bluetooth® Low Energy failed!");
  }

  // set advertised local name and service UUID:
  BLE.setLocalName("Pinky_RX");
  BLE.setAdvertisedService(RCService);
  // add the characteristics to the service
  RCService.addCharacteristic(RC_AngleCharacteristic);
  RCService.addCharacteristic(RC_SpeedCharacteristic);
  RCService.addCharacteristic(RC_HeightCharacteristic);
  RCService.addCharacteristic(RC_RotationCharacteristic);
  // add service
  BLE.addService(RCService);
  // start advertising
  BLE.advertise();
  Serial.println("Pinky Peripheral, waiting for connections....");
  //IMU stuff
  if (!IMU.begin()) {
    Serial.println("Failed to initialize IMU!");
    while (1);
  }
    Serial.print("Gyroscope sample rate = ");
  Serial.print(IMU.gyroscopeSampleRate());
  Serial.println(" Hz");
  Serial.println();
  Serial.println("Gyroscope in degrees/second");
  Serial.println("X\tY\tZ");
}
void loop() {
  // listen for BLE peripherals to connect:
  BLEDevice central = BLE.central();
  // if a central is connected to peripheral:
  if (central) {
    if (!CentralConnected){
      Serial.print("Connected to central: ");
      // print the central's MAC address:
      Serial.println(central.address());
      // while the central is still connected to peripheral:
      CentralConnected = 1;
    }
   
    while (central.connected()) {
      //Serial.print("Waiting for the write thing: ");
      //delay(100);//This causes hangs / crashes
      if (RC_AngleCharacteristic.written() || RC_SpeedCharacteristic.written() || RC_HeightCharacteristic.written() || RC_RotationCharacteristic.written()) {
        //Debounce the BLE
        if (LastAngle != RC_AngleCharacteristic.value() || LastSpeed != RC_SpeedCharacteristic.value() || LastHeight != RC_HeightCharacteristic.value() || LastRot != RC_RotationCharacteristic.value()){
        tick++;
        Serial.println(tick);
        Serial.print("Angle: ");
        Serial.print(RC_AngleCharacteristic.value());
        Serial.print(" Speed: ");
        Serial.print(RC_SpeedCharacteristic.value());
        Serial.print(" Height: ");
        Serial.print(RC_HeightCharacteristic.value());
        Serial.print(" Rotation: ");
        Serial.println(RC_RotationCharacteristic.value());
        LastAngle = RC_AngleCharacteristic.value();
        LastSpeed = RC_SpeedCharacteristic.value();
        LastHeight = RC_HeightCharacteristic.value();
        LastRot = RC_RotationCharacteristic.value();
        }
       

      }
      // if (IMU.gyroscopeAvailable()) {
      // IMU.readGyroscope(x, y, z);

      // Serial.print(x);
      // Serial.print('\t');
      // Serial.print(y);
      // Serial.print('\t');
      // Serial.println(z);
      //}
    }
    if(CentralConnected){
      // when the central disconnects, print it out:
      Serial.print(F("Disconnected from central: "));
      Serial.println(central.address());
      CentralConnected = 0;
    }
  }
 
}