patterncppMinor
Arduino code to run an LED matrix over Bluetooth
Viewed 0 times
arduinobluetoothcodeledmatrixoverrun
Problem
I've made an Arduino program to run on my Teensy 3.1 board. It accepts characters and strings sent to a Bluetooth module (BC417) from any Bluetooth-enabled device and uses these to power certain pins on my 8x8 LED matrix.
I believe the code works, but I haven't been able to test it fully yet. I'm hoping (if it's the right subject for this site) that there are ways the code can be improved. I'm still somewhat a beginner with Arduino programming, and I know there are many functions I don't know about that will probably be useful for me now and in the future
```
#define MyBtooth Serial2
#define dw digitalWrite
char incomingChar;
String inString;
bool stream;
//
// Setup
//
void setup() {
for (int x = 0; x = 0) && (x = 14) && (x 0) {
incomingChar = Serial.read();
Serial.print("USB received: ");
Serial.println(incomingChar);
MyBtooth.print("USB received:");
MyBtooth.println(incomingChar);
switches();
}
String cancel = Serial.readString();
if (cancel.toUpperCase() == "CANCEL") {
break;
}
}
}
void BTControl() {
while (true) {
if (MyBtooth.available() > 0) {
incomingChar = MyBtooth.read();
Serial.print("UART received: ");
Serial.println(incomingChar);
MyBtooth.print("UART received:");
MyBtooth.println(incomingChar);
switches();
}
String cancel = Serial2.readString();
if (cancel.toUpperCase() == "CANCEL") {
break;
}
}
}
void letterA() {
for (int x = 0; x <= 100000; x++) {
dw(0, HIGH);
dw(1, HIGH);
dw(6, HIGH);
dw(7, HIGH);
dw(21, HIGH);
dw(3, LOW);
dw(4, LOW);
delay(0.5);
dw(21, LOW);
dw(3, HIGH);
dw(4, HIGH);
dw(2, LOW);
dw(5, LOW);
dw(20, HIGH);
delay(0.5);
dw(20, LOW);
dw(2, HIGH);
dw(5, HIGH);
dw(1, LOW);
dw(6, LOW);
dw(19, HIGH);
dw(1
I believe the code works, but I haven't been able to test it fully yet. I'm hoping (if it's the right subject for this site) that there are ways the code can be improved. I'm still somewhat a beginner with Arduino programming, and I know there are many functions I don't know about that will probably be useful for me now and in the future
```
#define MyBtooth Serial2
#define dw digitalWrite
char incomingChar;
String inString;
bool stream;
//
// Setup
//
void setup() {
for (int x = 0; x = 0) && (x = 14) && (x 0) {
incomingChar = Serial.read();
Serial.print("USB received: ");
Serial.println(incomingChar);
MyBtooth.print("USB received:");
MyBtooth.println(incomingChar);
switches();
}
String cancel = Serial.readString();
if (cancel.toUpperCase() == "CANCEL") {
break;
}
}
}
void BTControl() {
while (true) {
if (MyBtooth.available() > 0) {
incomingChar = MyBtooth.read();
Serial.print("UART received: ");
Serial.println(incomingChar);
MyBtooth.print("UART received:");
MyBtooth.println(incomingChar);
switches();
}
String cancel = Serial2.readString();
if (cancel.toUpperCase() == "CANCEL") {
break;
}
}
}
void letterA() {
for (int x = 0; x <= 100000; x++) {
dw(0, HIGH);
dw(1, HIGH);
dw(6, HIGH);
dw(7, HIGH);
dw(21, HIGH);
dw(3, LOW);
dw(4, LOW);
delay(0.5);
dw(21, LOW);
dw(3, HIGH);
dw(4, HIGH);
dw(2, LOW);
dw(5, LOW);
dw(20, HIGH);
delay(0.5);
dw(20, LOW);
dw(2, HIGH);
dw(5, HIGH);
dw(1, LOW);
dw(6, LOW);
dw(19, HIGH);
dw(1
Solution
Remove duplication
There's clearly some duplication in your code, such as:
As you seem to commonly write the same string to both
I'd also consider refactoring this:
To something like this:
Where:
You also seem to write to ordered ranges of pins like this:
Again it might be worth writing a utility method something like this (I'm not sure of the type for pinState), where direction is either 1 (incrementing range) or -1 (decrementing range):
Called as:
There may also be scope for doing a similar reduction in your large case statement. Something like:
Would remove:
Serial2 Vs MyBtooth
It looks like you started off using
There's clearly some duplication in your code, such as:
Serial.println("How do you want to control the RGB matrix?");
Serial2.println("How do you want to control the RGB matrix?");As you seem to commonly write the same string to both
Serial and MyBtooth, it's probably worth defining a function that simply does that and using it instead:void println(const char *text) {
Serial.println(text);
MyBtooth.println(text);
}I'd also consider refactoring this:
String choice = Serial.readString();
String choicebt = Serial2.readString();
if ((choice.toUpperCase() == "BLUETOOTH") || (choicebt.toUpperCase() == "BLUETOOTH")) {
BTControl();
}
else if ((choice.toUpperCase() == "SERIAL") || (choicebt.toUpperCase() == "SERIAL")) {
serialControl();
}
else{}To something like this:
setupCommControl(Serial.readString());
setupCommControl(MyBtooth.readString());Where:
void setupCommControl(const char *choice) {
if (choice.toUpperCase() == "BLUETOOTH") {
BTControl();
}
else if (choice.toUpperCase() == "SERIAL") {
serialControl();
}
}You also seem to write to ordered ranges of pins like this:
dw(0, LOW);
dw(1, LOW);
dw(2, LOW);
dw(3, LOW);
dw(4, LOW);
dw(5, LOW);
dw(6, LOW);
dw(7, LOW);Again it might be worth writing a utility method something like this (I'm not sure of the type for pinState), where direction is either 1 (incrementing range) or -1 (decrementing range):
void dwRange(int startPin, int endPin, int direction, int pinState) {
for(int pin = startPin; pin <= endPin; pin += direction) {
dw(pin, pinState);
}
}Called as:
dwRange(0, 7, 1, LOW);There may also be scope for doing a similar reduction in your large case statement. Something like:
if(incomingChar >= 'a' && incomingChar <= 'h') {
dw(incomingChar - 'a', LOW);
}Would remove:
case 'a':
dw(0, LOW);
break;
case 'b':
dw(1, LOW);
break;
case 'c':
dw(2, LOW);
break;
case 'd':
dw(3, LOW);
break;
case 'e':
dw(4, LOW);
break;
case 'f':
dw(5, LOW);
break;
case 'g':
dw(6, LOW);
break;
case 'h':
dw(7, LOW);
break;Serial2 Vs MyBtooth
It looks like you started off using
Serial2, then defined MyBtooth to make it more explicit when you were talking to blue tooth vs serial. There are some lingering references to Serial2 in the code that should be updated so that it is consistent throughout.Code Snippets
Serial.println("How do you want to control the RGB matrix?");
Serial2.println("How do you want to control the RGB matrix?");void println(const char *text) {
Serial.println(text);
MyBtooth.println(text);
}String choice = Serial.readString();
String choicebt = Serial2.readString();
if ((choice.toUpperCase() == "BLUETOOTH") || (choicebt.toUpperCase() == "BLUETOOTH")) {
BTControl();
}
else if ((choice.toUpperCase() == "SERIAL") || (choicebt.toUpperCase() == "SERIAL")) {
serialControl();
}
else{}setupCommControl(Serial.readString());
setupCommControl(MyBtooth.readString());void setupCommControl(const char *choice) {
if (choice.toUpperCase() == "BLUETOOTH") {
BTControl();
}
else if (choice.toUpperCase() == "SERIAL") {
serialControl();
}
}Context
StackExchange Code Review Q#127644, answer score: 3
Revisions (0)
No revisions yet.