Components and supplies
LEGO Parts
Arduino Nano 33 BLE
DC motor (generic)
Tools and machines
3D Printer (generic)
Project description
Code
CAR
arduino
1// ============================================ 2// Arduino nano 33 3// 4 propeller CAR (peripheral) 5// ============================================ 6// 7 PORT ASSIGNMENT 8// ============================================ 9// D7:EX 10 POWER 11// A4:EX IO 12// D13:LED 13// ============================================ 14// 15 FILE INCLUDE 16// ============================================ 17#include 18 <ArduinoBLE.h> 19BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); 20 // BLE LED Service 21BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", 22 BLERead | BLEWrite); 23// ============================================ 24// CONTROL 25 VALUABLES 26// ============================================ 27 long itime, 28 itime2; // event timer 29 int tgll; 30// ============================================ 31// 32 SETUP 33// ============================================ 34void setup() { 35 36 Serial.begin(9600); 37 //while (!Serial); 38 39 pinMode( 7, OUTPUT); // 40 DC motor 41 pinMode(13, OUTPUT); // on board LED 42 digitalWrite(7, 0); // 43 motor power off 44 45 pinMode(22, OUTPUT); // RGB LED R 46 pinMode(23, OUTPUT); 47 // RGB LED G 48 pinMode(24, OUTPUT); // RGB LED B 49 RGB(1,1,0); // 50 Blue 51 52 BLE.begin(); // begin initialization 53 54 BLE.setLocalName("LED"); // define local name 55 56 BLE.setAdvertisedService(ledService); // set advertised local name 57 and service UUID: 58 ledService.addCharacteristic(switchCharacteristic); // add 59 the characteristic to the service 60 BLE.addService(ledService); // 61 add service 62 switchCharacteristic.writeValue(0); // set the 63 initial value for the characeristic: 64 BLE.advertise(); Serial.println("BLE 65 LED Peripheral"); 66} 67// ============================================ 68// 69 LOOP 70// ============================================ 71void RGB(int R, int 72 G, int B) { digitalWrite(22, R); digitalWrite(23, G); digitalWrite(24, B); } 73void 74 loop() { 75 BLEDevice central = BLE.central(); 76 if (central) { // if a central 77 is connected to peripheral: 78 79 RGB(0,1,0); Serial.print("Connected to 80 central: "); // Red/Blue 81 Serial.println(central.address()); // 82 print the central's MAC address: 83 84 while (central.connected()) { // 85 while the central is still connected to peripheral: 86 if (switchCharacteristic.written()) 87 { 88 if (switchCharacteristic.value()) { // any value other 89 than 0 90 digitalWrite(13, 1); digitalWrite(7, 1); Serial.println("ON"); 91 // will turn the ON 92 } else { // 93 a 0 value 94 digitalWrite(13, 0); digitalWrite(7, 0); Serial.println("OFF"); 95 // will turn the OFF 96 } 97 } 98 } 99 100 Serial.print(F("Disconnected 101 from central: ")); // when the central disconnects, print it out: 102 RGB(1,1,0); 103 Serial.println(central.address()); // disconected Blue 104 } 105 //if(millis() 106 - itime > 200) { itime = millis(); digitalWrite(13, tgll); tgll = !tgll; } 107}
CONTROLLER
arduino
1// ============================================ 2// Arduino nano 33 3// propeller CAR Controller (central) 4// ============================================ 5// PORT ASSIGNMENT 6// ============================================ 7// D7:EX POWER 8// A4:EX IO 9// D13:LED 10// ============================================ 11// FILE INCLUDE 12// ============================================ 13#include <Arduino_LSM9DS1.h> 14#include <ArduinoBLE.h> 15// ============================================ 16// CONTROL VALUABLES 17// ============================================ 18 long itime, itime2; // event timer 19 int tgll; 20 int oldButtonState = LOW; 21// ============================================ 22// SETUP 23// ============================================ 24void setup() { 25 Serial.begin(9600); 26 //while (!Serial); 27 pinMode(13, OUTPUT); // on board LED 28 29 pinMode(22, OUTPUT); // RGB LED R 30 pinMode(23, OUTPUT); // RGB LED G 31 pinMode(24, OUTPUT); // RGB LED B 32 RGB(1,0,1); // Green 33 34 IMU.begin(); // begin gyro 35 Serial.print("Accelerometer sample rate = "); Serial.println(IMU.accelerationSampleRate()); 36 37 BLE.begin(); Serial.println("BLE Central - LED"); // begin initialization 38 BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214"); // start scanning for peripherals 39 40} 41// ============================================ 42// LOOP 43 float x, y, z; 44 int ONOFF; 45// ============================================ 46void RGB(int R, int G, int B) { digitalWrite(22, R); digitalWrite(23, G); digitalWrite(24, B); } 47void loop() { 48 gyro(); 49 BLEDevice peripheral = BLE.available(); 50 if (peripheral) { // discovered a peripheral, print out address, local name, and advertised service 51 Serial.print("Found "); Serial.print(peripheral.address()); Serial.print(" '"); 52 Serial.print(peripheral.localName()); Serial.print("' "); 53 Serial.println(peripheral.advertisedServiceUuid()); 54 if (peripheral.localName() != "LED") { return; } 55 BLE.stopScan(); // stop scanning 56 controlLed(peripheral); 57 BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214"); // disconnect, start scanning again 58 } 59} 60void gyro() { 61 if (IMU.accelerationAvailable()) { IMU.readAcceleration(x, y, z); } //Serial.println(-100*x); 62 if(-100*x > 30) { ONOFF = 1; } else { ONOFF = 0; } digitalWrite(13, ONOFF); 63} 64 65void controlLed(BLEDevice peripheral) { 66 Serial.println("Connecting ..."); // connect to the peripheral 67 if (peripheral.connect()) { Serial.println("Connected"); } else { Serial.println("Fail"); return; } 68 69 Serial.println("Discovering attributes ..."); // discover peripheral attributes 70 if (peripheral.discoverAttributes()) { Serial.println("Attributes discovered"); 71 } else { Serial.println("Fail"); peripheral.disconnect(); return; } 72 73 Serial.println("Discovering retrieve the LED characteristic ..."); // retrieve the LED characteristic 74 BLECharacteristic ledCharacteristic = peripheral.characteristic("19b10001-e8f2-537e-4f6c-d104768a1214"); 75 if (!ledCharacteristic) { Serial.println("no LED char"); peripheral.disconnect(); return; } 76 else if (!ledCharacteristic.canWrite()) { Serial.println("no wLED char"); peripheral.disconnect(); return; } 77 78 RGB(0,0,1); // red selected! Red/Green 79 while (peripheral.connected()) { // while the peripheral is connected 80 gyro(); 81 int buttonState = ONOFF; // read the button pin 82 if (oldButtonState != buttonState) { oldButtonState = buttonState; // status changed 83 if (buttonState) { ledCharacteristic.writeValue((byte)0x01); Serial.println("ON"); } 84 else { ledCharacteristic.writeValue((byte)0x00); Serial.println("OFF"); } 85 } 86 } 87 RGB(1,0,1); Serial.println("Peripheral disconnected"); // blue 88}
CAR
arduino
1// ============================================ 2// Arduino nano 33 3// propeller CAR (peripheral) 4// ============================================ 5// PORT ASSIGNMENT 6// ============================================ 7// D7:EX POWER 8// A4:EX IO 9// D13:LED 10// ============================================ 11// FILE INCLUDE 12// ============================================ 13#include <ArduinoBLE.h> 14BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service 15BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite); 16// ============================================ 17// CONTROL VALUABLES 18// ============================================ 19 long itime, itime2; // event timer 20 int tgll; 21// ============================================ 22// SETUP 23// ============================================ 24void setup() { 25 Serial.begin(9600); 26 //while (!Serial); 27 28 pinMode( 7, OUTPUT); // DC motor 29 pinMode(13, OUTPUT); // on board LED 30 digitalWrite(7, 0); // motor power off 31 32 pinMode(22, OUTPUT); // RGB LED R 33 pinMode(23, OUTPUT); // RGB LED G 34 pinMode(24, OUTPUT); // RGB LED B 35 RGB(1,1,0); // Blue 36 37 BLE.begin(); // begin initialization 38 BLE.setLocalName("LED"); // define local name 39 BLE.setAdvertisedService(ledService); // set advertised local name and service UUID: 40 ledService.addCharacteristic(switchCharacteristic); // add the characteristic to the service 41 BLE.addService(ledService); // add service 42 switchCharacteristic.writeValue(0); // set the initial value for the characeristic: 43 BLE.advertise(); Serial.println("BLE LED Peripheral"); 44} 45// ============================================ 46// LOOP 47// ============================================ 48void RGB(int R, int G, int B) { digitalWrite(22, R); digitalWrite(23, G); digitalWrite(24, B); } 49void loop() { 50 BLEDevice central = BLE.central(); 51 if (central) { // if a central is connected to peripheral: 52 53 RGB(0,1,0); Serial.print("Connected to central: "); // Red/Blue 54 Serial.println(central.address()); // print the central's MAC address: 55 56 while (central.connected()) { // while the central is still connected to peripheral: 57 if (switchCharacteristic.written()) { 58 if (switchCharacteristic.value()) { // any value other than 0 59 digitalWrite(13, 1); digitalWrite(7, 1); Serial.println("ON"); // will turn the ON 60 } else { // a 0 value 61 digitalWrite(13, 0); digitalWrite(7, 0); Serial.println("OFF"); // will turn the OFF 62 } 63 } 64 } 65 66 Serial.print(F("Disconnected from central: ")); // when the central disconnects, print it out: 67 RGB(1,1,0); Serial.println(central.address()); // disconected Blue 68 } 69 //if(millis() - itime > 200) { itime = millis(); digitalWrite(13, tgll); tgll = !tgll; } 70}
Downloadable files
9jira
9jira
9jira
9jira
Documentation
9jira gerber data
9jira gerber data
9jira gerber data
9jira gerber data
Comments
Only logged in users can leave comments
Nano 33 BLE Propeller Car | Arduino Project Hub