## Wednesday, 20 July 2011

### Calculate Prime Numbers using an ARDUINO Board

Following example uses an Arduino board to calculate a prime number list, starting from one.
It shows the result using serial port communication.

#### BUILDING AND RUNNING THE CODE

You should copy this code into your Arduino IDE window, compile it (C-R), upload the code into the board (C-U) and to see the result open the serial monitor (C-Shift-M).

If everything goes fine your serial monitor will show a prime on each line every time it is calculated. Maximum prime able to be calculated depends on long type maximum value.

```long number = 3;

void setup()
{
Serial.begin(9600);
Serial.println("#");
Serial.println("1");
Serial.println("2");
}

void loop()
{
int is_prime = 1;
long i = 3;
long top = sqrt(number);
while (i<top)
{
if (number%i == 0)
{
is_prime = 0;
break;
}
i++;
}
if (is_prime)
{
Serial.println(number,DEC);
}
number += 2;
}
```

Within setup function, it shows 1 and 2 primes. It then starts calculating in the main loop using number 3.

Primality test here consists on performing divisions and checking the remainder. It is not needed to test with every divisor, when square root is reached we know it is a prime.

When a prime number is found it is sent over serial line.

As we start calculations with number three, we can skip all even numbers.

#### YOU MAY ALSO BE INTERESTED IN:

Install and run a program in Arduino using 64 bits Ubuntu (lucid) distro

## Wednesday, 6 July 2011

### Install and run a program in Arduino using 64 bits Ubuntu (lucid) distro

Arduino is an open-source electronics prototyping platform based on flexible, easy-to-use hardware and software.

Arduino provides a Java based IDE and examples are written in C language: http://arduino.cc/en/Reference/HomePage

MY SYSTEM consists on:

Arduino Duemilanove: An ATmega328 running at 16 MHz with auto-reset. It connects to the computer using a USB cable.

Ubuntu distro:
Release: 10.04
Codename: lucid

Linux kernel
2.6.32-32-generic
x86_64 AMD 64 bits architecture.

#### INSTALLING ARDUINO IDE SOFTWARE

First we install needed dependencies:
• openjdk-6-jre
• avr-gcc (aka "gcc-avr")
• version 4.3.2 or later 4.3.x version (4.4.x and 4.5.x seem to have various problems)
• avr-libc

NOTE: On the contrary to what Arduino page said, I did not need avr-gcc-c++.

`\$ sudo aptitude install openjdk-6-jre gcc-avr avr-libc`
NOTE: gcc-avr version is 4.3.4 in my system