Dear coding buddies,
Since about one year, we are working on a very nice project. Summed up shortly, it is about indoor bouldering (climbing), we made a concept for DIY climbing walls and I created the Android app for it. Now, I constructed an easy Arduino-based LED System which lights up the holds which the climber should take in the respective climbing route.
So far, it works well. However, to be honest, I'm a very bad coder. I programmed the Android app using a graphical programming environment. Now, with the Arduino, there is no way around coding. So I had to code again. The point is - I know my Arduino code is crap. It works somehow, but there is so much potential for enhancements.
If you please have a look at my scratch, you will surely find a lot of things that can be coded better. Maybe you can help me with this. I'm sure I will learn a lot if you can guide me.
The whole project is a non-profit comunity work and we already spent uncountable hours for it, but it's a real cool project and we have a lot of fun. If you are interested in it, have a look at www.i-nowa.com (German). Also, I'm going to make games for kids with the LED System (eg. a snake that you have to climb away from). It's an open project, maybe someone is interested in joining? Ideas and help are always appreciated!
So, here's the scratch (some parts are even not tested yet). Please don't be too cruel to me ![]()
// ~~~ iNoWa - the indoor NorthWall. ~~~
// NANO-BLE Firmware für die
// Steuerung des LED Systems
//
// Version 1.0.0
// 03.01.2022
//##############################################
#include "FastLED.h"
#include <JC_Button.h> // https://github.com/JChristensen/JC_Button
#define NUM_LEDS 154
#define RDY_LED 2
#define TST_BUTTON 11
#define PULLUP true // To keep things simple, we use the Arduino's internal pullup resistor.
#define INVERT false // Since the pullup resistor will keep the pin high unless the
// switch is closed, this is negative logic, i.e. a high state
// means the button is NOT pressed. (Assuming a normally open switch.)
#define DEBOUNCE_MS 100
#define SPK_PIN 12
#define DATA_PIN 13
int brightness = 254;
CRGB leds[NUM_LEDS];
Button Testbutton(TST_BUTTON, PULLUP, INVERT, DEBOUNCE_MS); // Declare the button.
int fadeAmount = 5; // Set the amount to fade I usually do 5, 10, 15, 20, 25 etc even up to 255.
int count;
int nbr = 0;
char inByte; // incoming serial byte
String text = "";
String data[25][2];
void setup() {
Serial.begin(9600); // Start serial connection
delay(1000); // Soft startup to ease the flow of electrons.
FastLED.addLeds<WS2811, DATA_PIN, RGB>(leds, NUM_LEDS); // Configure LED chain
FastLED.setBrightness(brightness); // set your brightness as desired
startupLEDsTest(); // optional R,G,B test
count = 0;
// Start Melody
tone(SPK_PIN, 264);
delay(300);
tone(SPK_PIN, 330);
delay(300);
tone(SPK_PIN, 396);
delay(450);
noTone(SPK_PIN);
FastLED.clear();
}
void startupLEDsTest() {
// startup blink test to confirm LEDs are working.
FastLED.setBrightness(127);
fill_solid(leds, NUM_LEDS, CRGB(255,0,0)); // fill red
FastLED.show();
delay(1000);
fill_solid(leds, NUM_LEDS, CRGB(0,255,0)); // fill green
FastLED.show();
delay(1000);
fill_solid(leds, NUM_LEDS, CRGB(0,0,255)); // fill blue
FastLED.show();
delay(1000);
FastLED.clear();
fill_rainbow( leds, NUM_LEDS, 0, 7); // rainbow
FastLED.show();
delay(2000);
FastLED.setBrightness(brightness); // reset brightness to master level
FastLED.clear();
FastLED.show();
} //end_startupLEDsTest
void checkButton() {
Testbutton.read();
if (Testbutton.wasReleased()) { // Test button pressed
Serial.println(nbr);
nbr++; // increase button press counter
if (nbr == 5) {
nbr = 0;
FastLED.clear();
tone(SPK_PIN, 864); // signal for mode 0
delay(300);
noTone(SPK_PIN);
}
else if (nbr == 1) {
fill_solid(leds, NUM_LEDS, CRGB(255,0,0)); // fill red
tone(SPK_PIN, 864); // signal for mode 1
delay(70);
noTone(SPK_PIN);
}
else if (nbr == 2) {
fill_solid(leds, NUM_LEDS, CRGB(0,255,0)); // fill green
for (int i=0;i<2;i++){
tone(SPK_PIN, 864); // signal for mode 2
delay(70);
noTone(SPK_PIN);
delay(70);
}
}
else if (nbr == 3) {
fill_solid(leds, NUM_LEDS, CRGB(0,0,255)); // fill green
for (int i=0;i<3;i++){
tone(SPK_PIN, 864); // signal for mode 3
delay(70);
noTone(SPK_PIN);
delay(70);
}
}
else if (nbr == 4) {
fill_rainbow( leds, NUM_LEDS, 0, 7); // rainbow
for (int i=0;i<4;i++){
tone(SPK_PIN, 864); // signal for mode 4
delay(70);
noTone(SPK_PIN);
delay(70);
}
}
FastLED.show(); // light it up
}
}
//glitter effect
void addGlitter( fract8 chanceOfGlitter) {
if( random8() < chanceOfGlitter) {
leds[ random16(NUM_LEDS) ] += CRGB::White;}
}
void loop() {
digitalWrite(RDY_LED, HIGH); // System signals be to be ready
char Buf[50];
//checkButton(); // check if Testbutton is pressed
if (Serial.available()) { // check for incoming data --> if available
delay(20); // just wait a little bit for more characters to arrive
while (Serial.available())
{
inByte = Serial.read(); // store incoming data
if ((!(inByte == '/')) && (!(inByte == ':')) && (!(inByte == '#')) && (!(inByte == '!'))) {
text.concat(inByte);
}
if (inByte == ':') { // number:color seperator
data[count][0] = text; // here is a number
text = "";
}
if (inByte == '/') { // hold separator
data[count][1] = text; // here is a char
text = "";
count++;
}
if (inByte == 'P') { // pulsate signal for ambience light mode
data[0][1] = text;
text = "";
count++;
}
if (inByte == 'S') { // sparkle signal for ambience light mode
data[1][1] = text;
text = "";
count++;
}
if (inByte == '!') { // signal for ambience light mode
FastLED.clear();
fill_solid(leds, NUM_LEDS, CRGB(data[0][0].toInt(),data[1][0].toInt(),data[2][0].toInt())); // fill in color user has set
FastLED.setBrightness(data[3][0].toInt()); // set brightness user has set
if (data[0][1] == "S"){
addGlitter(30);
}
// signal that data has successfully been processed
tone(SPK_PIN, 2000);
delay(100);
tone(SPK_PIN, 1000);
delay(100);
noTone(SPK_PIN);
FastLED.show(); // light it up
}
if (inByte == '#') { // end signal (boulder mode)
for(int i=0; i<count; i++){
Serial.print(data[i][0]);
Serial.println(data[i][1]);
}
FastLED.clear();
FastLED.show();
// fill up pixel array
for (int i = 1; i < 155; i++) {
for (int j = 0; j < count; j++) {
if (data[j][0].toInt() == i) {
if (data[j][1] == "g") {
leds[i-1] = CRGB::Green;
}
else if (data[j][1] == "b") {
leds[i-1] = CRGB::Blue;
}
else if (data[j][1] == "l") {
leds[i-1] = CRGB::Purple;
}
else if (data[j][1] == "r") {
leds[i-1] = CRGB::Red;
}
}
}
}
count = 0;
// signal that data has successfully been processed
tone(SPK_PIN, 1000);
delay(100);
tone(SPK_PIN, 2000);
delay(100);
noTone(SPK_PIN);
FastLED.show(); // light it up
}
}
}
if (data[0][1] == "P"){ // pulsate signal for ambience light
for(int i = 0; i < NUM_LEDS; i++ ){
leds[i].fadeLightBy(brightness);
}
FastLED.show();
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if(brightness == 0 || brightness == 255){
fadeAmount = -fadeAmount ;
}
delay(9); // This delay sets speed of the fade. I usually do from 5-75 but you can always go higher.
}
}
