ebaera
Published © GPL3+

Nano 33 BLE Propeller Car

Propeller driven car with accelerometer and Bluetooth wireless connection.

IntermediateFull instructions provided4,933
Nano 33 BLE Propeller Car

Things used in this project

Hardware components

DC motor (generic)
×1
Arduino Nano 33 BLE
Arduino Nano 33 BLE
×2
LEGO Parts
×1

Hand tools and fabrication machines

3D Printer (generic)
3D Printer (generic)

Story

Read more

Custom parts and enclosures

9jira gerber data

Schematics

9jira

Code

CONTROLLER

Arduino
// ============================================
//  Arduino nano 33
//  propeller CAR Controller (central)
// ============================================
//  PORT ASSIGNMENT
// ============================================
// D7:EX POWER
// A4:EX IO
// D13:LED
// ============================================
//  FILE INCLUDE 
// ============================================  
#include <Arduino_LSM9DS1.h>
#include <ArduinoBLE.h>
// ============================================
//  CONTROL VALUABLES
// ============================================  
   long itime, itime2;         // event timer  
   int tgll;
   int oldButtonState = LOW;   
// ============================================
//  SETUP
// ============================================
void setup() {
  Serial.begin(9600);
  //while (!Serial);
  pinMode(13, OUTPUT); // on board LED

  pinMode(22, OUTPUT); // RGB LED R
  pinMode(23, OUTPUT); // RGB LED G
  pinMode(24, OUTPUT); // RGB LED B
  RGB(1,0,1);          // Green  
  
  IMU.begin();                                              // begin gyro
  Serial.print("Accelerometer sample rate = "); Serial.println(IMU.accelerationSampleRate());
  
  BLE.begin(); Serial.println("BLE Central - LED");         // begin initialization       
  BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214");  // start scanning for peripherals
  
}
// ============================================
//  LOOP
  float x, y, z;
  int ONOFF;
// ============================================
void RGB(int R, int G, int B) { digitalWrite(22, R); digitalWrite(23, G); digitalWrite(24, B); }
void loop() {  
  gyro();
  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.println(peripheral.advertisedServiceUuid());
    if (peripheral.localName() != "LED") { return; }
    BLE.stopScan();                                          // stop scanning
    controlLed(peripheral);    
    BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214"); // disconnect, start scanning again
  }   
}
void gyro() {
  if (IMU.accelerationAvailable()) { IMU.readAcceleration(x, y, z); } //Serial.println(-100*x);
  if(-100*x > 30) { ONOFF = 1; } else { ONOFF = 0; } digitalWrite(13, ONOFF);  
}

void controlLed(BLEDevice peripheral) {
  Serial.println("Connecting ...");  // connect to the peripheral
  if (peripheral.connect()) { Serial.println("Connected"); } else { Serial.println("Fail"); return; }

  Serial.println("Discovering attributes ..."); // discover peripheral attributes
  if (peripheral.discoverAttributes()) { Serial.println("Attributes discovered");
  } else { Serial.println("Fail"); peripheral.disconnect(); return; }

  Serial.println("Discovering retrieve the LED characteristic ..."); // retrieve the LED characteristic
  BLECharacteristic ledCharacteristic = peripheral.characteristic("19b10001-e8f2-537e-4f6c-d104768a1214");
  if (!ledCharacteristic) { Serial.println("no LED char"); peripheral.disconnect(); return; } 
  else if (!ledCharacteristic.canWrite()) { Serial.println("no wLED char"); peripheral.disconnect(); return; }

  RGB(0,0,1); // red selected! Red/Green
  while (peripheral.connected()) {   // while the peripheral is connected
    gyro(); 
    int buttonState = ONOFF; // read the button pin    
    if (oldButtonState != buttonState) { oldButtonState = buttonState; // status changed  
      if (buttonState) { ledCharacteristic.writeValue((byte)0x01); Serial.println("ON");  }
      else {             ledCharacteristic.writeValue((byte)0x00); Serial.println("OFF"); }
    }
  }
  RGB(1,0,1); Serial.println("Peripheral disconnected"); // blue
}

CAR

Arduino
// ============================================
//  Arduino nano 33
//  propeller CAR (peripheral)
// ============================================
//  PORT ASSIGNMENT
// ============================================
// D7:EX POWER
// A4:EX IO
// D13:LED
// ============================================
//  FILE INCLUDE 
// ============================================  
#include <ArduinoBLE.h>
BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service
BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
// ============================================
//  CONTROL VALUABLES
// ============================================  
   long itime, itime2;         // event timer  
   int tgll;
// ============================================
//  SETUP
// ============================================
void setup() {
  Serial.begin(9600);
  //while (!Serial);
  
  pinMode( 7, OUTPUT); // DC motor
  pinMode(13, OUTPUT); // on board LED
  digitalWrite(7, 0);  // motor power off  

  pinMode(22, OUTPUT); // RGB LED R
  pinMode(23, OUTPUT); // RGB LED G
  pinMode(24, OUTPUT); // RGB LED B
  RGB(1,1,0);          // Blue
  
  BLE.begin();                                        // begin initialization
  BLE.setLocalName("LED");                            // define local name
  BLE.setAdvertisedService(ledService);               // set advertised local name and service UUID:
  ledService.addCharacteristic(switchCharacteristic); // add the characteristic to the service
  BLE.addService(ledService);                         // add service
  switchCharacteristic.writeValue(0);                 // set the initial value for the characeristic:
  BLE.advertise(); Serial.println("BLE LED Peripheral");
}
// ============================================
//  LOOP
// ============================================
void RGB(int R, int G, int B) { digitalWrite(22, R); digitalWrite(23, G); digitalWrite(24, B); }
void loop() {
  BLEDevice central = BLE.central();
  if (central) {  // if a central is connected to peripheral:
 
    RGB(0,1,0); Serial.print("Connected to central: "); // Red/Blue
    Serial.println(central.address());                  // print the central's MAC address:

    while (central.connected()) {      // while the central is still connected to peripheral:
      if (switchCharacteristic.written()) {
        if (switchCharacteristic.value()) {             // any value other than 0
            digitalWrite(13, 1); digitalWrite(7, 1); Serial.println("ON");  // will turn the ON
        } else {                                        // a 0 value
            digitalWrite(13, 0); digitalWrite(7, 0); Serial.println("OFF"); // will turn the OFF
        }
      }
    }

    Serial.print(F("Disconnected from central: ")); // when the central disconnects, print it out:
    RGB(1,1,0); Serial.println(central.address());  // disconected Blue
  }
  //if(millis() - itime > 200) { itime = millis(); digitalWrite(13,  tgll); tgll = !tgll; }   
}

Credits

ebaera

ebaera

0 projects • 10 followers

Comments