Saturday, July 17, 2021

Basic Typewriter

 



Maybe i should call this a Knob writer since characters are entered not through keys but through a knob (potentionmeter).


Why take all this trouble to attach a LCD module or display?


Let's go back to the turing machine. What is a machine? A machine solves a problem. For example: you can create a machine that checks whether a number is even or odd. You can build a machine that checks whether a given number is prime. You can build a machine that converts given message to a cipher(secret message).


So a machine solves a problem. What that means is you give it an input and it will give the answer in form of output. Now we already know Turing machine is the ultimate machine. There are less powerful machines that have limitations on what problems they can solve. But a turing machine can solve any problem. 


So since we are concerned with the output not the input, not the machine not the process involved to build it. Then the question is how do we understand the output. There are many ways to represent the output or the answer that the machine gives. LEDs can be used as output medium. But the problem with LEDs is it takes time to interpret it. For example to read an answer from the leds you need to build a number system and then convert the leds using the number sysstem to the number or the result. 


So the only reason why we build this complex circuit with a LCD module is just to make it easier for us to see the output or the answer of the problem that the machine solves. 


So in summary a Turing machine can solve any problem. Solving a problem means getting the output of the given input. There are many ways to interpret the output. The easiest way is to just display the output directly in the screen. For this the LCD module helps us. LCD module in the arduino world is analogous to computer monitors in the desktop world. 


Here is the demonstration of a basic typewriter that can be used to enter and display characters. 


View

Wednesday, July 7, 2021

Memory Game

 



In this series of showing the power of a turing machine this time i created a musical memory game. It is also called simon game. 

Here is the Turing machine

#include "pitches.h"

//this program needs pitches.h file. you can google for pitches.h

int lednotes[] = {
  NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4
};


void setup() {

  Serial.begin(9600);

  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);

  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);


  randomSeed(analogRead(0));


}


int leds[] = {8, 9, 10, 11};



void turnOnLed(int p){

    //play a sound while turning led on
    int noteDuration = 1000 / 4;
    tone(7, lednotes[p], noteDuration);

  for(int i=0; i<4; i++){
    if(p == i){
      digitalWrite(leds[p], HIGH);
      
    }
    else{
      digitalWrite(leds[i], LOW);
      
    }
  }
 
}

void turnOffLeds(){
  for(int i=0; i<4; i++){
    
      digitalWrite(leds[i], LOW);
    
  } 
}




int inputturn = false;



//store the led sequence
int sequence[100];
//store the player inputs
int inputs[100];




//length of the sequence. starts with 1 which means it lights one led.
int count = 1;
int inputcount = 0;



void playIncorrectSound(){

    for(int i=0; i<2; i++){

    int noteDuration = 1000 / 8;
    tone(7, NOTE_G3, noteDuration);

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(7);

      
    }
 
}



void checkAnswer(){


  for(int i=0; i<count; i++){

      if(inputs[i] != sequence[i]){

        inputcount = 0;
        count = 1;
        
        inputturn = false;



        playIncorrectSound();
        
        return;
        
        
      }
      
  }


        inputturn = false;
        count += 1;
        inputcount = 0;
        
 
}



void loop() {


    //After showing the led sequence inputturn = true. This means its player turn to input the pattern using the buttons.
    if(inputturn){

      
    if(digitalRead(2) == LOW){
      Serial.println("first pressed");     

       digitalWrite(8, HIGH);

       inputs[inputcount] = 0;

       inputcount += 1;

       delay(200);
      
    }
    else{
      
      digitalWrite(8, LOW);
      
    }


    if(digitalRead(3) == LOW){
      Serial.println("second pressed");
      digitalWrite(9, HIGH);

       inputs[inputcount] = 1;

       inputcount += 1;

       delay(200);

    }
    else{
      digitalWrite(9, LOW);
       
      
    }

    if(digitalRead(4) == LOW){
      Serial.println("third pressed");
      digitalWrite(10, HIGH);


       inputs[inputcount] = 2;

       inputcount += 1;

       delay(200);

    }
    else{
      digitalWrite(10, LOW);
       
      
    }

    if(digitalRead(5) == LOW){
      Serial.println("fourth pressed");
      digitalWrite(11, HIGH);


       inputs[inputcount] = 3;

       inputcount += 1;

       delay(200);


       
    }
    else{
      digitalWrite(11, LOW);
       
      
    }


    if(inputcount == count){
        checkAnswer();
    }
   

 
    }
    else{


//time to show the led sequence

        turnOffLeds();
        delay(100);


      
      for(int i=0; i<count; i++){
 
        int randNumber = random(0, 4);

        sequence[i] = randNumber;
 
        
        turnOnLed(randNumber);
        delay(500);
        turnOffLeds();
        delay(100);


        
      }

      inputturn = true;


    }
 

}


View the game play



Monday, July 5, 2021

Cipher Machine

 

Enigma but an unbreakable one





Same arduino but a different machine.

In this series of showing the power of a turing machine this time i created a cipher machine.

Turing machine is like a transformer. It can transform into any other machine. This time it transformed itself into a cipher machine.


I have used leds to represent the 26 english alphabets. 

Here is the video showing the alphabets. 




and here is the demonstration of how the machine works.




Message Encrypter

void setup() {

  //buttons
  pinMode(2, INPUT);
  pinMode(3, INPUT);


  //RGB led
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);

  //normal leds
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);

  pinMode(A0, OUTPUT);
  pinMode(A1, OUTPUT);
  pinMode(A2, OUTPUT);


 
 
  Serial.begin(9600);
}



int count = -1;


int leds[] = {8, 9, 10, 11, 12, 13, A0, A1, A2};


void turnOnRGBRed(){
      digitalWrite(5, HIGH);
      digitalWrite(6, LOW);
      digitalWrite(7, LOW);  
}

void turnOnRGBGreen(){
      digitalWrite(5, LOW);
      digitalWrite(6, HIGH);
      digitalWrite(7, LOW);  
}

void turnOnRGBBlue(){
      digitalWrite(5, LOW);
      digitalWrite(6, LOW);
      digitalWrite(7, HIGH);  
}




void turnOffLeds(){

  for(int i=0; i<9; i++){
    digitalWrite(leds[i], LOW);
 
  }


      //turn off rgb led
      digitalWrite(5, LOW);
      digitalWrite(6, LOW);
      digitalWrite(7, LOW);  

 
}


void turnOnLed(int n){

  for(int i=0; i<9; i++){
    if((i) == n){
      digitalWrite(leds[i], HIGH);
     
    }
    else{
      digitalWrite(leds[i], LOW);
     
    }
  }

 
}

void convertNumberToLEDs(int n){

  turnOnLed( n % 9 );

  switch(n/9){
    case 0:
      turnOnRGBRed();
      break;
    case 1:
      turnOnRGBGreen();
      break;
    case 2:
       turnOnRGBBlue();
       break;  
  }

}



//mode = 0 Input the key, mode = 1 Input the text, mode = 2 shows the cipher
int mode = 0;


int keys[100];
int keyindex = 0;


int plain[100];
int plainindex = 0;


int cipher[100];
int cipherindex = 0;


int prevkey = 0;


void calculateCipher(){

        for(int i=0; i<keyindex; i++){
          cipher[i] = (keys[i] + plain[i]) % 26;          
        }

       
}

void showCipher(){

    delay(1000);


    for(int i=0; i<keyindex; i++){                    
      convertNumberToLEDs(cipher[i]);    
      Serial.println(cipher[i]);
      delay(1000);
    }


}


void loop() {
  // put your main code here, to run repeatedly:


  //cycles the alphabet
  if(digitalRead(2) == HIGH){

    count += 1;


    if(count > 25){
      count = -1;
    }
   

    prevkey = 2;
   
    delay(300);
       
  }


  if(digitalRead(3) == HIGH){


    if(count >= 0 ){


       //in mode 0 stores the key
      if(mode == 0){
 
      keys[keyindex] = count;
      keyindex += 1;
       
      }//in mode 1 stores the plain text
      else if(mode == 1){
       
      plain[plainindex] = count;
      plainindex += 1;
     
      }
    count = -1;
 
    }
   
   

    if(prevkey == 3 && count == -1){

      mode += 1;


//in mode 2 shows the cipher
      if(mode >= 2){

        calculateCipher();
        showCipher();
       

      }
       
     
    }

    prevkey = 3;
    delay(300);
   
  }


  if(count >= 0 ){
     convertNumberToLEDs(count);    
  }
  else{
    turnOffLeds();
  }

}

Message Decrypter


void setup() {
  // put your setup code here, to run once:


  pinMode(2, INPUT);
  pinMode(3, INPUT);

  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);


  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);

  pinMode(A0, OUTPUT);
  pinMode(A1, OUTPUT);
  pinMode(A2, OUTPUT);


 
 
  Serial.begin(9600);
}


int count = -1;


int leds[] = {8, 9, 10, 11, 12, 13, A0, A1, A2};


void turnOnRGBRed(){
      digitalWrite(5, HIGH);
      digitalWrite(6, LOW);
      digitalWrite(7, LOW);  
}

void turnOnRGBGreen(){
      digitalWrite(5, LOW);
      digitalWrite(6, HIGH);
      digitalWrite(7, LOW);  
}

void turnOnRGBBlue(){
      digitalWrite(5, LOW);
      digitalWrite(6, LOW);
      digitalWrite(7, HIGH);  
}




void turnOffLeds(){

  for(int i=0; i<9; i++){
    digitalWrite(leds[i], LOW);
 
  }


      //turn off rgb led
      digitalWrite(5, LOW);
      digitalWrite(6, LOW);
      digitalWrite(7, LOW);  

 
}


void turnOnLed(int n){

  for(int i=0; i<9; i++){
    if((i) == n){
      digitalWrite(leds[i], HIGH);
     
    }
    else{
      digitalWrite(leds[i], LOW);
     
    }
  }

 
}

void convertNumberToLEDs(int n){

  turnOnLed( n % 9 );

  switch(n/9){
    case 0:
      turnOnRGBRed();
      break;
    case 1:
      turnOnRGBGreen();
      break;
    case 2:
       turnOnRGBBlue();
       break;  
  }

}



int mode = 0;


int keys[100];
int keyindex = 0;


int plain[100];
int plainindex = 0;


int cipher[100];
int cipherindex = 0;


int prevkey = 0;


void calculatePlain(){

        for(int i=0; i<keyindex; i++){
          plain[i] = (cipher[i] - keys[i]) % 26;    
          if(plain[i] < 0){
            plain[i] = 26 + plain[i];    
          }
        }

       
}

void showPlain(){

    delay(1000);


    for(int i=0; i<keyindex; i++){                    
      convertNumberToLEDs(plain[i]);    
      Serial.println(plain[i]);
      delay(1000);
    }


}


void loop() {
  // put your main code here, to run repeatedly:


  if(digitalRead(2) == HIGH){

    count += 1;


    if(count > 25){
      count = -1;
    }
   

    prevkey = 2;
   
    delay(300);
       
  }

  if(digitalRead(3) == HIGH){


    if(count >= 0 ){

      if(mode == 0){
 
      keys[keyindex] = count;
      keyindex += 1;
       
      }
      else if(mode == 1){
       
      cipher[cipherindex] = count;
      cipherindex += 1;
     
      }
     
     count = -1;
 
    }
   
   

    if(prevkey == 3 && count == -1){

      mode += 1;


      if(mode >= 2){

        calculatePlain();
        showPlain();
       

      }
       
     
    }

    prevkey = 3;
    delay(300);
   
  }

 

  if(count >= 0 ){
     convertNumberToLEDs(count);    
  }
  else{
    turnOffLeds();
  }

}



Friday, July 2, 2021

Large Prime Generating Machine

 



Warning: The colorful blinking lights are encoding sensitive information. ;)

I think in the world of Arduino a prime such as 999983 can be considered a large prime.

In this series of showing the power of a turing machine this whole week i spent building a prime generating machine. 


To review, Turing Machine is the minimalistic machine that can solve any (interesting) problem.

If there is a way to do it, a Turing Machine will do it. 

In other words most interesting problems have step by step procedure to solve them. The step by step procedure is called an algorithm. So if there is an algorithm then a Turing machine will solve it. 

Our Arduino is a Turing Machine (Universal Turing Machine).

These are some ways i view Turing machines as. Turing machine is the ultimate machine. Turing machine is the father of all machines. Turing machine is the fullstop of machines. A turing machine can generate any machine. A turing machine can solve any problems. If you have a turing machine you dont need any other machine. 

In the previous project we built a calculator using a Turing machine. This time a Turing machine transforms itself into a large prime generating machine.


I had 10 leds and 6 RGB leds in stock. I started by building this beast. It took 2 hours to complete it. 




After that soon the constraints and limitations of arduino uno started hitting. First of all i realized the limited number of pins available. 

There are only 12 pins for output. Pin 2-13. 

Because of the pins limitation and red led can only have 2 states on and off i dismissed the above design. The above design turned out to be inefficient. 

Instead of one red led using a RGB led turns out to be much efficient. It can generate many colors. If i use 16 different colors as 16 different states or digits then while 10 leds can represent 2^10 = 1024 numbers. Only three RGB led can represent 16 * 16 * 16 = 4096 numbers. 

So i soon dismissed the idea of using normal leds. I checked my drawer and there lay exactly 6 RGB pins. 

Upon further study of the UNO architecture. 

I realized only 6 pins can be used for RGB leds to generate 16 different colors. You can see closely in the photo below that the pins that have a little ~ sign are pins that can do PWM meaning i can use analogwrite to write various different values. Other pins can only write 1 or 0. 

Also i realized Anlong In pins can also be configured for output. Including the analog in pins we now have 12 + 6 = 18 pins that can be used for leds. 




Since i had exactly 6 rgb leds and 18 pins available among which 6 pins can be used for PWM which helps generate 16 colors, it started feeling like the 6 RGB leds were waiting for this PRIME GENERATRING MACHINE project. 


Each RGB led has 3 pins. We have total 6 leds which totals 6*3 = eacactly 18 pins i.e the number of pins available.

So the final design which i thought was more efficient is as follows. Ill use the top most two leds with PWM i.e the top most two leds will generate 16 colors each and the bottom 4 will generate 8 colors.


For the low leds since each led has 3 pins one for R, G and B and for each pin we can only write either 0 or 1 so we can generate 8 different colors.  

If we write these values

R = 0 G = 0 B = 0 ->Off
R = 1 G = 0 B = 0 ->Red
R = 0 G = 1 B = 0 ->Green
R = 0 G = 0 B = 1 ->Blue
R = 1 G = 1 B = 0 ->Yellow
R = 1 G = 0 B = 1 ->Magenta
R = 0 G = 1 B = 1 ->Cyan
R = 1 G = 1 B = 1 ->White





For the top most two leds since we can use analogWrite we can write any value from 0 to 255. So we can generate many colors but we need distinguishing colors. 

So i went with these colors

No night
low red
high red
low green
high green
low blue
high blue
low yellow
high yellow
low magenta
high magenta
low cyan
high cyan
low white 
high white
Brown

The following video shows these 16 states of a RGB led



After deciding the design and the total number of states each led have i now have to come up with a new number system that this circuit or leds will use. 

A number system is a way to represent numbers, understand them and do mathematics on them.

The number system we use is hindu arabic number system. It is just one way to represent numbers. 

In binary system we use 0 and 1 to represent any number.

So we can create our own number system.

This is the number system i came up with. I named it the RGB Number System.

The place values are at the top. Each column lists all the symbols or digits available for that placevalue. 

N, r, R are abbreviations for No light, low red, High red etc.



To the right are some examples of converting the hindu arabic number system numbers to the RGB number system.


Next i need to verify the correctness of this system. I wrote this javascript code to verify the correctness. I checked to make sure 1 million unique numbers can be represented with this system. 

<script>


let placevalues = [65536, 4096, 512, 64, 8, 1];

let digitsHigh = ["N", "r", "R", "g", "G", "b", "B", "y", "Y", "c", "C", "m", "M", "w", "W", "O"];
let digitsLow = ["N", "R", "G", "B", "Y", "C", "M", "W"];


function getMultipliers(num){

 
  let result = [];
 
  let nonzeroadded = false;

  for(let i=0; i<placevalues.length; i++){

      if(placevalues[i] <= num){

        let div = Math.floor(num / placevalues[i]);

        result.push(div);
       

        num = num - placevalues[i]*div;

        nonzeroadded = true;
      }
      else{
       
        if(nonzeroadded){
          result.push(0);          
        }
       
      }

  }

  return result;
 
 
}


function convertMultipliersToRGB(lis){
 
  let numdigits = lis.length;  
  let result = "";
 
  for(let i=0; i<numdigits; i++){
   
    if(numdigits - i > 4){
      result += digitsHigh[lis[i]];      
    }
    else{
      result += digitsLow[lis[i]];      
    }
       
  }
 
  return result;
}


let allNumbers = [];


for(let i=1; i<=1000000; i++){
  allNumbers.push(convertMultipliersToRGB(getMultipliers(i)));
}

//If these two are same then there are no duplicates, we have 1 million unique representations
console.log(new Set(allNumbers).size);
console.log(allNumbers.length);

</script>




After verifying that there is no mistake in the number system creation. I went on to actually implment it using code. 

Here is the machine counting from 1 to ..... in RGB number system





After that creating prime generator is straight forward. 

Here is the prime generating machine




I quickly noticed the inefficiency. It was too slow to be able to find all primes till million in reasonable time. 

Now came the optimization part. 

I had learnt about an efficent way to find primes called the Sieve of Eratosthenes

My first attempt was to use this technique

So i wrote code to implment this method

Here is the code in C++

*************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>

using namespace std;


bool flags[1000000];


int main()
{

    for(int i=0; i<1000000; i++){
        flags[i] = false;
    }
   
   
    int nextPrime = 2;
    cout << nextPrime << "\n";
       
   
    while(true){
       
        int count = 1;
       
        while(nextPrime * count < 1000000){
           
            flags[nextPrime * count] = true;
           
            count += 1;
        }
       
        int temp = nextPrime;
       
        while(flags[temp] == true){
           
            temp += 1;
           
        }
     
     
     
        nextPrime = temp;
       
       
        cout << nextPrime << "\n";
       
       
        if(nextPrime > 1000000){
            break;
        }
       
    }
   
   



    return 0;
}



When it was time to implement it in arduino. Soon software limitation hit. Hardware limitation had hit earlier (low pins).


The software limitation was i couldnt create an array of  bool flags[1000000];


Arduino only has 2k of RAM available which means 2000 bytes which meant at most i could create 

bool flags[2000]

So i cannot use the sieve of Eratosthenes directly.

I came up with a modified version of  sieve of Eratosthenes that only used bool flags[1000] 1000 bytes.

Here i test the correctness of this new algorithm

#include <iostream>

using namespace std;






/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////HELPER FUNCTIONS//////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////




//slow prime tester
bool isPrime(long n){

  for(long i=2; i<n; i++){

    if(n % i == 0){
      return false;
    }
    
  }

  return true;

  
}





bool arr[1000];



//fast prime tester
bool isPrimeFast(long n){


    if(n < 1000){
      return arr[n];
    }
  
    bool isPrm = true;

    for(int j=2; j<1000; j++){

      if(arr[j]){

        if(n % j == 0){
          isPrm = false;
          break;
        }
        
      }

    }

    return isPrm;
}




void fillPrimesTill1000(){

  for(int i=0; i<1000; i++){
    arr[i] = false;
  }



for(int i=2; i<1000; i++){
    if(isPrime(i)){
      arr[i] = true;
    }
  }

  
}



int main()
{


fillPrimesTill1000();


  //Find the primes from 3. No need to check even numbers.
  for(long i=3; i<1000000; i +=2){
      
   if(isPrimeFast(i) != isPrime(i)){    //uncomment this for fast version
   //if(isPrime(i)){      //uncomment this for slow version
      cout << i << " " << isPrimeFast(i) << " " << isPrime(i) << "\n";
      break;
    }
    else{
        cout << i << "PASS\n";
    }


  }


    return 0;
}




You can notice with this optimization the speed gain is significant. 

In less than 30 minutes the machine can find all primes till 1 million.

Here is the demonstration



Here is the complete arduino code. The circuit design is straightforward as shown in the video. 6 rgb leds to 18 pins in arduino.



//Place values for the RGB Number system
long placevalues[6] = {65536, 4096, 512, 64, 8, 1};


//Store the multipliers of the placevalues in this array
long result[6];



//pins for each RGB led. Each array inside the array is RGB pins.
int pins[6][3] = {{7, 4, 2}, {13, 12, 8}, {A0, A1, A2}, {A3, A4, A5}, {6, 5, 3}, {11, 10, 9}};



void setup() {
  
      Serial.begin(9600);

      //For fast prime function
      fillPrimesTill1000();

}





void loop() {


  //Find the primes from 3. No need to check even numbers.
  for(long i=3; i<1000000; i +=2){
      
   if(isPrimeFast(i)){    //uncomment this for fast version
   //if(isPrime(i)){      //uncomment this for slow version

      getMultipliers(i);
      convertResultToRGB();

      
      Serial.println(i);
    }


  }




}




/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////HELPER FUNCTIONS//////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////




//slow prime tester
bool isPrime(long n){

  for(long i=2; i<n; i++){

    if(n % i == 0){
      return false;
    }
    
  }

  return true;

  
}





bool arr[1000];



//fast prime tester
bool isPrimeFast(long n){


    if(n < 1000){
      return arr[n];
    }
  
    bool isPrm = true;

    for(int j=2; j<1000; j++){

      if(arr[j]){

        if(n % j == 0){
          isPrm = false;
          break;
        }
        
      }

    }

    return isPrm;
}




void fillPrimesTill1000(){

  for(int i=0; i<1000; i++){
    arr[i] = false;
  }



for(int i=2; i<1000; i++){
    if(isPrime(i)){
      arr[i] = true;
    }
  }

  
}












//Get multipliers of the placevalues in RGB number system and stores the multipliers in the result array
void getMultipliers(long num){

 
  bool nonzeroadded = false;

  long index = 0;

  for(long i=0; i<6; i++){

      if(placevalues[i] <= num){

        long div = num / placevalues[i];
        
        result[index++] = div;
        
        
        num = num - placevalues[i]*div;

        nonzeroadded = true;
      }
      else{

        result[index++] = 0;
       
      }

  }

 
 
}




void convertResultToRGB(){

  
  writeLed(0, result[5]);
  writeLed(1, result[4]);
  writeLed(2, result[3]);
  writeLed(3, result[2]);
  writeLed(4, result[1]);
  writeLed(5, result[0]);

  
}


void writeLed(int led, int d){


if(led <= 3){



  if(d == 0){

  //N
  analogWrite(pins[led][0], 0);
  analogWrite(pins[led][1], 0);
  analogWrite(pins[led][2], 0);
    
  }
  else if(d == 1){
    //R
  analogWrite(pins[led][0], 255);
  analogWrite(pins[led][1], 0);
  analogWrite(pins[led][2], 0);

  }
  else if(d == 2){
    //G
  analogWrite(pins[led][0], 0);
  analogWrite(pins[led][1], 255);
  analogWrite(pins[led][2], 0);

  }
  else if(d == 3){
    //B
  analogWrite(pins[led][0], 0);
  analogWrite(pins[led][1], 0);
  analogWrite(pins[led][2], 255);

  }
  else if(d == 4){
    //Y
  analogWrite(pins[led][0], 255);
  analogWrite(pins[led][1], 255);
  analogWrite(pins[led][2], 0);

  }
  else if(d == 5)
  {
    //M
  analogWrite(pins[led][0], 255);
  analogWrite(pins[led][1], 0);
  analogWrite(pins[led][2], 255);

  }
  else if(d == 6){
    //C
  analogWrite(pins[led][0], 0);
  analogWrite(pins[led][1], 255);
  analogWrite(pins[led][2], 255);

  }
  else if(d == 7){
    //W
  analogWrite(pins[led][0], 255);
  analogWrite(pins[led][1], 255);
  analogWrite(pins[led][2], 255);

  }
  

  
}
else{


  

switch (d) {
  case 0:
    //do something when var equals 1
    
  //N
  analogWrite(pins[led][0], 0);
  analogWrite(pins[led][1], 0);
  analogWrite(pins[led][2], 0);

    break;
    
  case 1:

  
//r
  analogWrite(pins[led][0], 50);
  analogWrite(pins[led][1], 0);
  analogWrite(pins[led][2], 0);

    //do something when var equals 2
    break;
  case 2:
  //R
  analogWrite(pins[led][0], 255);
  analogWrite(pins[led][1], 0);
  analogWrite(pins[led][2], 0);

    //do something when var equals 2
    break;
  case 3:

  //g
  analogWrite(pins[led][0], 0);
  analogWrite(pins[led][1], 50);
  analogWrite(pins[led][2], 0);

    //do something when var equals 2
    break;
  case 4:

  
//G
  analogWrite(pins[led][0], 0);
  analogWrite(pins[led][1], 255);
  analogWrite(pins[led][2], 0);

    //do something when var equals 2
    break;
  case 5:

  //b
  analogWrite(pins[led][0], 0);
  analogWrite(pins[led][1], 0);
  analogWrite(pins[led][2], 50);

    //do something when var equals 2
    break;
  case 6:

  //B
  analogWrite(pins[led][0], 0);
  analogWrite(pins[led][1], 0);
  analogWrite(pins[led][2], 255);

    //do something when var equals 2
    break;
  case 7:

  //y
  analogWrite(pins[led][0], 50);
  analogWrite(pins[led][1], 50);
  analogWrite(pins[led][2], 0);

    //do something when var equals 2
    break;
  case 8:

  
//Y
  analogWrite(pins[led][0], 255);
  analogWrite(pins[led][1], 255);
  analogWrite(pins[led][2], 0);

    //do something when var equals 2
    break;
  case 9:

  //m
  analogWrite(pins[led][0], 50);
  analogWrite(pins[led][1], 0);
  analogWrite(pins[led][2], 50);

    //do something when var equals 2
    break;
  case 10:

  
//M
  analogWrite(pins[led][0], 255);
  analogWrite(pins[led][1], 0);
  analogWrite(pins[led][2], 255);

    //do something when var equals 2
    break;
  case 11:

  //c
  analogWrite(pins[led][0], 0);
  analogWrite(pins[led][1], 50);
  analogWrite(pins[led][2], 50);

    //do something when var equals 2
    break;
  case 12:

  //C
  analogWrite(pins[led][0], 0);
  analogWrite(pins[led][1], 255);
  analogWrite(pins[led][2], 255);

    //do something when var equals 2
    break;
  case 13:
  //w
  analogWrite(pins[led][0], 50);
  analogWrite(pins[led][1], 50);
  analogWrite(pins[led][2], 50);

    //do something when var equals 2
    break;
  case 14:

  //W
  analogWrite(pins[led][0], 255);
  analogWrite(pins[led][1], 255);
  analogWrite(pins[led][2], 255);


    //do something when var equals 2
    break;
  case 15:

  //O
  analogWrite(pins[led][0], 200);
  analogWrite(pins[led][1], 20);
  analogWrite(pins[led][2], 0);

    //do something when var equals 2
    break;
    
  default:
    // if nothing else matches, do the default
    // default is optional
    break;
}

  
}

  
}

What is engineering but solving problems under constraints.


SOME PRIMES


G B N C

512*2 + 64*1 + 8*0 + 1*3 = 1091


G R R R

512*2 + 64*1 + 8*1 + 1*1 = 1097


G G G B
512*2 + 64*2 + 8*2 + 1*3 = 1171




G B N R

512*2 + 64*2 + 8*0 + 1*1 = 1153




G C N R

512*2 + 64*6 + 8*0 + 1*1 = 1409



Other primes can be generated by taking screenshots from the videos.