Fun With Arduino, Nothing Else Needed

2,906

24

2

The reason for the empty breadboard in the picture is because there are no circuits to build, all you need is an Arduino.

I was curious to see how an Arduino would work for math and simple text based graphics so I decided to do a little experimenting. These programs are better suited for a computer but it is interesting to see that an Arduino can run them.

Have fun.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Calculate Pi Using the “Leibniz Formula”

This is an interesting way of calculating Pi, however it is very slow if you want to get past a few Digits. Run this if you can spare an Arduino for a few years.

Copy/paste the code into the Arduino IDE, upload it, open the serial monitor and set the baud at 115200.

```/********************************************
*
* Filename: CalcPi.ino
*
* Calculate Pi using the “Leibniz formula”:
*   Pi=(4/1)-(4/3)+(4/5)-4/7)+(4/9)...
*
********************************************/
float Pi=4;
float M=1;

void setup()
{
Serial.begin(115200);
}

void loop()
{
M += 2;
Pi = Pi - (4/M);
M += 2;
Pi = Pi + (4/M);

Serial.print(Pi, 21);
Serial.print(" - ");
Serial.println(m);
}```

Step 2: Prime Number Generator

A prime number is a number that can only be divided by two numbers, one and itself. The "two numbers" in that definition is important, it eliminates one. The first few prime numbers are 2, 3, 5, 7, 11, and 13. The larger a number is the less chance it is prime, but the number of primes is infinite.

This program will generate a list of prime numbers using the serial monitor, you can press any key, followed by enter, to pause the program for three seconds.

Copy this code into the Arduino IDE, upload it, and start the serial monitor at 9600 baud:

```/*****************************************************************
* Filename: primes.ino
*
* Find prime numbers and output them through the serial monitor.
* Press any key, then enter, to pause the program for 3 seconds.
*
*****************************************************************

/*********************************************************
* The setup() function, runs at startup.
*********************************************************/
void setup()
{
Serial.begin(9600);  //Start the serial monitor.
}

/*********************************************************
* The loop() function, runs continuous when setup finishes.
*********************************************************/
void loop()
{
if(Serial.available()>0)
{
delay(3000);
}
Serial.println("2");
for(long i=3;i<2147483647;i+=2) // Check odd numbers up to 2^31.
{
if(Serial.available()>0) // Is there a keypress?
{
delay(3000);           // and wait three seconds.
}
long flag = 0;
int limit = sqrt(i);

for(long j=3;j<=limit;j+=2)     // Inside for statement.
{
if((i % j) < 1)        // Is i divisable by j?
{
flag = 1;            // If yes then i is not prime,
j = limit;           // set flag and force exit
}                      // of inside for statement.
}
if(flag < 1)             // If flag is still zero i is prime.
{
Serial.println(i);     // Print the results.
if(i < 1000) delay(250); // Just makes the start easier to see.
}
}
}```

Step 3: Conway's Game of Life

Conway's game of life is a zero player game, the object is to put in a starting pattern and see what happens. The rules are simple, you start with a pattern in a grid. Each spot in the grid is called a cell. Each cell can be either off or on. First the program looks at all the cells and counts the number of adjacent cells that are on around it. Then it turns all the cells on or off according to the following rules.

If less than two adjacent cells are on the cell is turned off, representing under population.

If more than three adjacent cells are on the cell is turned off, representing over population.

A cell that is off is turned on if exactly three adjacent are on.

Copy/paste the code into the Arduino IDE, upload it, open the serial monitor and set the baud at 115200. The program will show you the starting pattern and wait for input. When you press any key the program will start running and continue until you cut the power or press reset.

You can change the starting pattern by changing the zeros and ones in the array in the program. Search Wikipedia for "Conway's game of life" to find more interesting patterns, or experiment with some of your own.

```/*******************************************************
* Filename: life.ino
*
* Conway's game of life
*******************************************************/

byte start[14][42]={
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0},
{0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};

byte count[14][42];
long iterations = 0;

/**********************************************************
* The setup() function, runs at startup.
**********************************************************/
void setup()
{
Serial.begin(115200);
}

/*********************************************************
* The loop() function, runs continuos when setup finishes.
*********************************************************/
void loop()
{
for(int i=1;i<14;i++)      // Display results.
{
for(int j=1;j<41;j++)
{
if(start[i][j]==0) Serial.print(" ");
else Serial.print("@");
}
Serial.println(" ");
}
iterations++;
Serial.print(iterations);
Serial.println(" iterations");
delay(500);
if(iterations==1)
{
while(Serial.available() ==0);
}

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

for(int i=0;i<14;i++)     // Zero counts from previous iteration.
{
for(int j=0;j<41;j++)
{
count[i][j]=0;
}
}

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

for(int i=1;i<14;i++)     // Count adjacent live cells.
{
for(int j=1;j<41;j++)
{
if(start[i-1][j-1]==1) count[i][j]++;
if(start[i-1][j]==1) count[i][j]++;
if(start[i-1][j+1]==1) count[i][j]++;

if(start[i][j-1]==1) count[i][j]++;
if(start[i][j+1]==1) count[i][j]++;

if(start[i+1][j-1]==1) count[i][j]++;
if(start[i+1][j]==1) count[i][j]++;
if(start[i+1][j+1]==1) count[i][j]++;
}
}

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

for(int i=1;i<14;i++)     // Switch on/off cells.
{
for(int j=1;j<41;j++)
{
if (count[i][j]<2) start[i][j]=0;
if (count[i][j]>3) start[i][j]=0;
if (count[i][j]==3) start[i][j]=1;
}
}
}  // End of loop()```

182 9.9K
73 3.8K
72 4.4K

2 Discussions

I just added a program to calculate Pi using the "Leibniz formula" to this instructable.