---
# System prepended metadata

title: Arduino Sort Algorithm Visiable Project
tags: [Arduino]

---

###### tags: `Arduino`
# Arduino Sort Algorithm Visiable Project
* code
```cpp=
const int led_len = 40;   //the length of led strit
const int Speed = 10;     //millisecond
const int brightness = 10;

#include <ArxContainer.h>
using namespace arx;
vector<int,led_len> vec;    //  color & num array
vector<int,led_len> R,G,B;    // rgb array
byte r=0, g=0, b=0;

#include "Wire.h"
//#include <Streaming.h>
#include <FastLED.h>
CRGB leds[led_len];    //led array

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);   // set the LCD address to 0x27 for a 16 chars and 2 line display
#define allclear lcd.clear(),lcd.setCursor(0,0);

#include <SoftwareSerial.h>     
SoftwareSerial BT(4,5);      //HC06 TX/RX pin

char val;
int count = 0;

void setup() {
  delay(500);            // power-up safety delay
  FastLED.addLeds<WS2812, 13, RGB>(leds, led_len);   //<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS)
  FastLED.setBrightness(brightness);
  BT.begin(9600);
  lcd.init(); lcd.backlight(); allclear; lcd.print("Ready!");
  randomSeed(analogRead(A0));
  delay(2000);
  pinMode(2,INPUT_PULLUP);
  allclear;
  reset();
  delay(100);
  //Serial.begin(9600);
  //Serial << "start " << vec.size() << endl;
}

struct ct{
  void reset(){count = 0;}
  void plusplus(){count++; lcd.setCursor(13,1); lcd.print(count);}
};

ct times;

void bobbleSort(){
  count = 0;
  for(int i=0;i<led_len;i++){
    for(int j=0;j<led_len-i;j++){
      if(vec[j]>vec[j+1]){
        int tmp = vec[j]; vec[j] = vec[j+1]; vec[j+1] = tmp;
        setColor(j,vec[j]); setColor(j+1,vec[j+1]);
        FastLED.show(),delay(Speed);
        times.plusplus();
      }
    }
  }
}

void quickSort(){
  times.reset();
  quick(0,led_len-1);
}

void quick(int l,int r){
  if(l >= r) return;
  int m = l, n = r, tmp = vec[m];
  while(m < n){
    while(m < n && vec[n] >= tmp) n--;
    if(m < n){
      vec[m++] = vec[n];
      setColor(m-1,vec[m-1]);
      FastLED.show(),delay(Speed*10);
      times.plusplus();
    }
    while(m < n && vec[m] <= tmp) m++;
    if(m < n){
      vec[n--] = vec[m];
      setColor(n+1,vec[n+1]);
      FastLED.show(),delay(Speed*10);
      times.plusplus();
    }
  }
  vec[m] = tmp;
  setColor(m,vec[m]);
  quick(l,m-1);
  quick(m+1,r);
}

void selectionSort(){
  times.reset();
  for(int i=0;i<led_len;i++){
    int Min = led_len,minpos = 0;
    for(int j=i;j<led_len;j++){
      if(vec[j]<Min){ Min = vec[j]; minpos = j;}
    }
    int tmp = vec[i]; vec[i] = vec[minpos]; vec[minpos] = tmp;
    setColor(i,vec[i]);
    setColor(minpos,vec[minpos]);
    FastLED.show(),delay(Speed*10);
    times.plusplus();
  }
}

void merge(int l, int m, int r){    //resemble the vector
  int s1 = m - l + 1, s2 = r - m;
  vector<int,led_len/2> lsub; vector<int,led_len/2> rsub;
  for(int i=0;i<s1;i++)
    lsub[i] = vec[l+i];
  for(int i=0;i<s2;i++)
    rsub[i] = vec[m + 1 + i];

  int i=0, j=0, k=l;

  while(i < s1 && j < s2){
    if(lsub[i] < rsub[j]){
      vec[k] = lsub[i];
      setColor(k,vec[k]);
      FastLED.show(),delay(Speed);
      times.plusplus();
      i++;
    }else{
      vec[k] = rsub[j];
      setColor(k,vec[k]);
      FastLED.show(),delay(Speed);
      times.plusplus();
      j++;
    }
    k++;
  }
  while(i < s1){ vec[k] = lsub[i]; i++,k++;}
  while(j < s2){ vec[k] = rsub[j]; j++,k++;}
}

void mergesort(int l, int r){   //devide vector into two
  if(l < r){
    int m = (l+r)/2;
    mergesort(l,m); mergesort(m+1,r); merge(l, m, r);
  }
}

void mergeSort(){times.reset(); mergesort(0,led_len-1);
}

//----------------------------------------------------------------------

void setColor(int pos, int col){byte r=0, g=0, b=0;  //    |R---G---B|
  if(col < led_len/2){
    r = 255 - round(255*pos/led_len*2);
    b = 0;
    g = round(255*pos/led_len*2);
  }else if(col >= led_len/2 && col < led_len){
    r = 0;
    b = round(255*(pos-led_len/2)/led_len*2);
    g = 255 - round(255*(pos-led_len/2)/led_len*2);
  }else if(col >= 40){
    if(col-40 < led_len/2){
      r = 255 - round(255*pos/led_len*2);
      b = 0;
      g = round(255*pos/led_len*2);
    }else{
      r = 0;
      b = round(255*(pos-led_len/2)/led_len*2);
      g = 255 - round(255*(pos-led_len/2)/led_len*2);
    }
    R[pos] = r, G[pos] = g, B[pos] = b;
  }
  leds[pos] = CRGB(g,r,b);  //G,R,B
  delay(10);
}

        //another color setting algorithm
void setColorVer1(int clo, int pos){ //  |--R----G----B--|
  byte r=0, g=0, b=0;

  if(clo < led_len/6&& clo >= 0){
    r = (127*pos/led_len*6) + 128;
    b = 128 - (127*pos/led_len*6);
    g = 0;
  }else if(clo < (led_len/2)){
    r = 255 - (255*(pos - led_len/6)/(led_len/3));
    g = (255*(pos - led_len/6)/(led_len/3));
    b = 0;
  }else if(clo < led_len*5/6){
    g = 255 - (255*(pos - led_len/2)/(led_len/3));
    b = (255*(pos - led_len/2)/(led_len/3));
    r = 0;
  }else{
    r = (128*(pos - led_len*5/6)/(led_len/6));
    b = 255 - ((pos - led_len*5/6)/(led_len/6));
    g = 0;
  }
  leds[pos] = CRGB(g,r,b);  //G,R,B
  FastLED.show();
  delay(10);
}

void reset(){
  for(int i=0;i<led_len;i++){
    vec[i] = i;
    setColor(i,i+40);
    //Serial << vec[i] << ' ';
    delay(10);
  }
  FastLED.show(),delay(500);
  //Serial << endl;
}

void shuffle(){
  for(int i=0;i<led_len;i++){
    int j = random(40), tmp = vec[i];
    vec[i] = vec[j];
    vec[j] = tmp;
  }
  for(int i=0;i<led_len;i++){
    setColor(i,vec[i]);
    //Serial << vec[i] << ", ";
  }
  FastLED.show(),delay(500);
  //Serial << endl;
}

void rainbow(){
  int tr = R[0], tg = G[0], tb = B[0];
  for(int i=0;i<led_len;i++){
    leds[i] = CRGB(R[i],G[i],B[i]);
    if(i != led_len - 1){
      R[i] = R[i+1];
      G[i] = G[i+1];
      B[i] = B[i+1];
    }else{
      R[i] = tr, G[i] = tg, B[i] = tb;
    }
  }
  FastLED.show();delay(10);
}

bool fg = 0;

void loop() {
  if(!digitalRead(2)){
    if(fg == 0){allclear; lcd.print("Enter sort type"); fg = 1;}
    while(BT.available()){ allclear; shuffle(); val = BT.read(); BT.println(val);
      if(val == '1'){lcd.print("BobbleSort"); delay(500);lcd.setCursor(0,1); lcd.print("switch times:");}
      else if(val == '2'){lcd.print("QuickSort");delay(500); delay(500);lcd.setCursor(0,1); lcd.print("switch times:");}
      else if(val == '3'){lcd.print("SelectionSort");delay(500); delay(500);lcd.setCursor(0,1); lcd.print("switch times:");}
      else if(val == '4'){lcd.print("MergeSort");delay(500); delay(500);lcd.setCursor(0,1); lcd.print("switch times:");}
      else if(val == '0'){lcd.print("random");delay(1000); delay(500);lcd.setCursor(0,1); lcd.print("switch times:");}
      else{lcd.print("Invalid type");}
      switch(val){
        case '1': bobbleSort(); break;
        case '2': quickSort(); break;
        case '3': selectionSort(); break;
        case '4': mergeSort(); break;
        case '0':
          int n = random(4);
          allclear;
          if(n==1){lcd.print("BobbleSort"); delay(500); lcd.setCursor(0,1); lcd.print("switch times:"); bobbleSort();}
          else if(n==2){lcd.print("QuickSort"); delay(500); lcd.setCursor(0,1); lcd.print("switch times:"); quickSort();}
          else if(n==3){lcd.print("MergeSort"); delay(500); lcd.setCursor(0,1); lcd.print("switch times:"); mergeSort();}
          else if(n==4){lcd.print("SelectionSort");delay(500); lcd.setCursor(0,1); lcd.print("switch times:"); selectionSort();}
          break;
        default:
          break;
      }
      delay(2000); fg = 0; allclear; val = random(4); reset;
    }
  }else{
    allclear;
    lcd.print("Rainbow");
    rainbow();
  }
  delay(100);
}

```