Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
206 changes: 206 additions & 0 deletions ArduinoR4Template/BotathonArduinoBLE.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@

#include "bluetooth.h"

ArduinoLEDMatrix matrix;

BLEService controlService(SERVICE_ID);
BLEUnsignedIntCharacteristic controlCharacteristic(CHARACTERISTIC_ID, BLERead | BLEWrite);
// Motor A connections
int enA = 13; // 9
int in1 = 12; // 8
int in2 = 11; // 7
// Motor B connections
int enB = 8; // 3
int in3 = 9; // 5
int in4 = 10; // 4

// Motor C connections
int enC = 7;
int in5 = 5;
int in6 = 6;

// Motor D connections
int enD = 2;
int in7 = 3;
int in8 = 4;

void setup() {
delay(2000);
// start serial
Serial.begin(BAUD_RATE);
while (!Serial);
//start LED matrix
matrix.begin();
matrix.stroke(0xFFFFFFFF);
matrix.textScrollSpeed(40);
matrix.textFont(Font_5x7);

motorSetUp();

#ifdef DEBUG_MATRIX
matrix.beginText(0, 1, 0xFFFFFF);
matrix.println(" Starting BLE module ");
matrix.endText(SCROLL_LEFT);
#endif

bluetoothInit(matrix, controlService, controlCharacteristic);
startPairing();
matrix.println(TEAM_NO);
matrix.endText();
controlCharacteristic.writeValue(0);

}



unsigned int previousValue = 0;
void loop() {
// Creates an object that contains information about the computer that's connected to the Arduino via Bluetooth
BLEDevice central = BLE.central();

// if the Arduino is currently pairing, stop pairing if it connected to the computer (via Bluetooth)
if(isPairing())
{
// checks if the computer is connected to the Arduino
if(central.connected()){
// stops pairing process
stopPairing();
}
else
{
// restart the loop since no computer connection = no controller input
return;
}
}
// if the Arduino isn't pairing, and there isn't a computer connected to the Arduino, start pairing
else if(!central.connected())
{
startPairing();
return;
}

// Checks if the variable has a new value i.e., it's been updated and changed
if(controlCharacteristic.written())
{
// refer to currentValue for the current controller value
unsigned int currentValue = controlCharacteristic.value();

/***INSERT YOUR CONTROLLER CODE BELOW HERE***/

/** // Below is a usage example
// checks if button A is pressed
if(buttonPressed(currentValue,Inputs::AButton))
{
// sets the left servos to move forward
left_wheels_forwards();
}
else
{
// stops the left wheels
stop_left_wheels();
}
**/

/***INSERT YOUR CONTROLLER CODE ABOVE HERE***/

// update previous value to current value
previousValue = currentValue;
}
}

// sets up pins to output on the Arduino. Also sets the motor speed to max
void motorSetUp() {
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(enC, OUTPUT);
pinMode(enD, OUTPUT);

pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);

pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(in5, OUTPUT);
pinMode(in6, OUTPUT);

pinMode(in7, OUTPUT);
pinMode(in8, OUTPUT);

// Turn off motors - Initial state
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
digitalWrite(in5, LOW);
digitalWrite(in6, LOW);
digitalWrite(in7, LOW);
digitalWrite(in8, LOW);

// set all motors to run at maximum speed, when spinning
// if your L298N has a jumper on the enables, these lines aren't needed
analogWrite(enA, 255);
analogWrite(enB, 255);
analogWrite(enC, 255);
analogWrite(enD, 255);

Serial.println("Motors have been initialized");
}

// turns on the left wheels to move forwards
void left_wheels_forwards() {
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);

digitalWrite(in5, LOW);
digitalWrite(in6, HIGH);
}

// turns on the right wheels to move forwards
void right_wheels_forwards() {
digitalWrite(in7, LOW);
digitalWrite(in8, HIGH);

digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
}
// turns on the left wheels to move backwards
void left_wheels_backwards() {
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in5, HIGH);
digitalWrite(in6, LOW);
}
// turns on the right wheels to move backwards
void right_wheels_backwards() {
digitalWrite(in7, HIGH);
digitalWrite(in8, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
}
// turns off the left wheels
void stop_left_wheels() {
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in5, LOW);
digitalWrite(in6, LOW);
}
// turns off the right wheels
void stop_right_wheels() {
digitalWrite(in7, LOW);
digitalWrite(in8, LOW);

digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}

// turns off all of the wheels
void stop_all_wheels() {
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
digitalWrite(in5, LOW);
digitalWrite(in6, LOW);
digitalWrite(in8, LOW);
digitalWrite(in7, LOW);
}
45 changes: 45 additions & 0 deletions ArduinoR4Template/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# WIP

# Installation

Ensure all of the files in this folder are in the same Sketchbook directory as `BotathonArduinoBLE.ino`.

Ensure the following libraries are installed in Arduino IDE:
* ArduinoBLE (by Arduino)
* ArduinoGraphics (by Arduino)

# Usage

Add your code between lines 89 and 100 in `BotathonArduinoBLE.ino`.

If you don't want the LED matrix on the Arduino to show debug messages, comment out line 9 in `botathonValues.h`, where it says ``#define DEBUG_MATRIX``.

## Getting controller button states

The function `buttonPressed` will tell you if a button is pressed down (or if it's not). Parameters are
1. `unsigned int c` - Pass `controlCharacteristic.value` here.
2. `Inputs input` - The button(s) you want to see is/are pressed down.

The function will return `true` if all `Inputs` passed in parameter 2 are pressed down. Otherwise, it will return false.

### Example usage

Assuming the A button is pressed down,

```cpp
buttonPressed(controlCharacteristic.value, Inputs::AButton);
```

will return `true`.

An equivalent function call is

```cpp
buttonPressed(controlCharacteristic.value, (Inputs) 1);
```

If you want to see if multiple buttons are pressed down,
```cpp
buttonPressed(controlCharacteristic.value,Inputs::AButton | Inputs::BButton);
```
this code will only return `true` if both the A and B buttons are pressed down. This is not equivalent to separately checking if the A
67 changes: 67 additions & 0 deletions ArduinoR4Template/bluetooth.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// This file doesn't need to be changed to get your bot working!
#include "bluetooth.h"

// this animation shows an X on the Arduino matrix. It's shown if an error occurs and the bot couldn't be initialized
const uint32_t anim_x[3] = {
0x60670e39,
0xc1f81f83,
0x9c70e606
};

// initializes Bluetooth settings on the Arduino
// matrix is only passed to display errors/messages
void bluetoothInit(ArduinoLEDMatrix &matrix, BLEService &controlService, BLEUnsignedIntCharacteristic &controlCharacteristic){
Serial.println("Starting BLE module");

// begin bluetooth module initialization. BLE.begin() returns false if initialization fails
if (!BLE.begin()) {
Serial.println("starting BLE module failed!");

// display error message
#ifdef DEBUG_MATRIX
matrix.println(" Failed to start BLE ");
matrix.endText(SCROLL_LEFT);
#endif
matrix.loadFrame(anim_x);
// halt program execution indefinitely
while (1);
}

// sets the name advertised on Bluetooth... this is how the computer will know which Arduino to connect yo
BLE.setLocalName(TEAM_NAME);

// set the UUID (ID) for the service this peripheral advertises:
BLE.setAdvertisedService(controlService);

// add the characteristic to the service
controlService.addCharacteristic(controlCharacteristic);

// add the service
BLE.addService(controlService);

// start advertising... advertising broadcasts the device across Bluetooth so that other devices can connect to it
BLE.advertise();

Serial.println("Bluetooth® device active, waiting for connections...");

#ifdef DEBUG_MATRIX
matrix.println(" Bluetooth active. ");
matrix.endText(SCROLL_LEFT);
#endif
}

// switches Arduino to pairing mode
void startPairing(){
// startTime = millis();
BLE.setPairable(Pairable::ONCE);
}

// disables pairing mode
void stopPairing(){
BLE.setPairable(false);
}

// tells you if the Arduino is in pairing mode
bool isPairing(){
return BLE.pairable();
}
10 changes: 10 additions & 0 deletions ArduinoR4Template/bluetooth.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// This file doesn't need to be changed to get your bot working!
#include <ArduinoBLE.h>
#include "ArduinoGraphics.h"
#include "Arduino_LED_Matrix.h"
#include "botathonValues.h"

void bluetoothInit(ArduinoLEDMatrix &matrix, BLEService &controlService, BLEUnsignedIntCharacteristic &controlCharacteristic);
void startPairing();
void stopPairing();
bool isPairing();
3 changes: 3 additions & 0 deletions ArduinoR4Template/botathonValues.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// You don't need to change anything here to get your bot working!
#include "botathonValues.h"
bool buttonPressed(unsigned int c,Inputs input){return ((unsigned int) input & c) == (unsigned int) input;}
45 changes: 45 additions & 0 deletions ArduinoR4Template/botathonValues.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Some of these values need to be changed to get your bot working!

// Change TEAM_NAME to use your team number
// You do not need to change TEAM_NO, but it may be helpful to show your team number there
#define TEAM_NAME "BotathonTeam23"
#define TEAM_NO 23 //used for the LED matrix

// uncomment this to have the LED matrix show debugging messages
// #define DEBUG_MATRIX

// Enum for button values
// See: https://www.learn-c.org/en/Bitmasks or README on bitmasks
enum class Inputs{ // doesn't include triggers because triggers are similar to joysticks
AButton = 1, //Bit 1
BButton = 2, //Bit 2
XButton = 4, //Bit 3
YButton = 8, //Bit 4
BumperLeft = 16, //Bit 5
BumperRight = 32, //Bit 6
LeftCenterButton = 64, //Bit 7
RightCenterButton = 128, //Bit 8

LeftJoystickButton = 256, //Bit 9
RightJoystickButton = 512, //Bit 10
XBoxButton = 1024, //Bit 11
CenterCenterButton = 2048, //Bit 12
DPadUp = 4096, //Bit 13
DPadRight = 8192, //Bit 14
DPadDown = 16384, //Bit 15
DPadLeft = 32768, //Bit 16
};

// This function tells you if a button is pressed. Pass the current value to c and the button you want to check to input
//Example usages:
// buttonPressed(currentValue, Input::AButton);
bool buttonPressed(unsigned int c,Inputs input);


// These don't need to be changed
#define CHARACTERISTIC_ID "19B10011-E8F2-537E-4F6C-D104768A1214"
#define BAUD_RATE 115200

// This also doesn't technically need to be changed
#define SERVICE_ID "1ae49b08-b750-4ef7-afd8-5395763c0da6"

Loading