/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////HELPER FUNCTIONS//////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Find the primes from 3. No need to check even numbers.
In less than 30 minutes the machine can find all primes till 1 million.
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.