# Comparison Arduino, VHDP and VHDL

## Sequential Example#

``Main (  LED : OUT STD_LOGIC;        --Declare output){    Process() {                 --For parallel and procedural programming        Thread {                  --Procedural programming            LED <= '1';               --LED on            Wait(1000ms);             --Wait            LED <= '0';             --LED off            Wait(1000ms);             --Wait        }    }}``

## Parallel Calculation Example (+ VARIABLE vs SIGNAL)#

``Main (    Xi : IN  INTEGER range 0 to 255;    --Declare inputs    Yi : IN  INTEGER range 0 to 255;          Xo : OUT INTEGER range 0 to 255;    --Declare output    Yo : OUT INTEGER range 0 to 255;    ){    Process() {                         --For parallel and procedural programming        Xo <= (Xi / 10) + 5;            --Everything calculated in one clock cycle        Yo <= (Yi / 5) + 10;            --Second calculation is finished together with first                SIGNAL   i1 : INTEGER := 0;     --Create integer signal (takes value one cycle after assignment)        VARIABLE i2 : INTEGER := 0;     --Create integer variable (takes value immediately, but can only be used in this process)        i1 <= i1 + 1;                   --Add both + 1        i2 := i2 + 1;                If(i1 > 0){                     --The signal is still 0, so this is false            i1 <= i1 - 1;        }        If(i2 > 0){                     --The variable is 1 already            i2 := i2 - 1;        }    }}``

## Parallel Process Example#

This is a simple code for an ultrasonic-sensor

``Main (    US_Trigger : OUT STD_LOGIC := '0';    US_Echo    : IN  STD_LOGIC := '0'; ){    Process Trigger_Process () {        --Creates an impuls every ~100ms to trigger the distance measurement        Thread {            US_Trigger <= '1';            Wait(10us);            US_Trigger <= '0';            Wait(100ms);        }    }        Process Echo_Process () {        Thread {            --Waits for a new echo impuls to calculate the distance            While(US_Echo = '1'){}            While(US_Echo = '0'){}                --Counts the microseconds while the sound travels to the object and back            --58 microseconds = 1cm            For(VARIABLE d : INTEGER := 0; US_Echo = '1'; d := d + 1) {                Wait(58us);            }            --d is now the distance to the object in cm        }    }}``

## Parallel Component Example#

In the libraries you can find `Components` that form an interface for different hardware. You can find controller for ultrasonic-sensors or for UART data (like Serial in Arduino). Every `NewComponent` runs in parallel, so you can add as many UART ports as you need.

``Main (    US_Trigger : OUT STD_LOGIC_VECTOR(7 downto 0) := '0';   --8 ultrasonic sensors    US_Echo    : IN  STD_LOGIC_VECTOR(7 downto 0) := '0';        LED        : OUT STD_LOGIC;                             --1 LED to show distance with brightness){    TYPE Distance_type IS ARRAY (0 to 7) OF NATURAL range 0 to 255;    SIGNAL Distance : Distance_type;                        --Distances of the 8 sensors        Generate (for i in 0 to 7) {                            --Generate an controller for every ultrasonic-sensor        NewComponent Ultrasonic_Controller (            Update_Frequency => 15,                         --Checks 15 times in a second            Trigger          => US_Trigger(i),            Echo             => US_Echo(i),            Dist             => Distance(i),        );    }        --Convert number from 0 to 255 to a 8-bit bit vector    PWM_Generator_Duty <= STD_LOGIC_VECTOR(TO_UNSIGNED(Distance(0), PWM_Generator_Duty'LENGTH));        SIGNAL PWM_Generator_Duty : STD_LOGIC_VECTOR (7 DOWNTO 0);    NewComponent PWM_Generator (                            --Outputs distance of first sensor        Duty            => PWM_Generator_Duty,        PWM_Out(0)      => LED,    );}``

## String Example#

``Main (    RX : IN  STD_LOGIC;             --I/Os for USB to UART converter    TX : OUT STD_LOGIC;){    Process () {        Thread {                    --Procedural programming            --Create constant string "Hello World!" (Adds RAM and interface for the stored string)            NewFunction assignString (s"Hello World!", hwStr);             --Output string with UART component below            NewFunction printString (hwStr, UART_Interface_TX_Data, UART_Interface_TX_Busy, UART_Interface_TX_Enable);            Wait(2000ms);           --Wait        }    }        SIGNAL UART_Interface_TX_Enable     : STD_LOGIC;    SIGNAL UART_Interface_TX_Busy       : STD_LOGIC;    SIGNAL UART_Interface_TX_Data       : STD_LOGIC_VECTOR (7 DOWNTO 0);        NewComponent UART_Interface     --Add a UART interface (You can add as many as needed)    (        Baud_Rate     => 9600,      --Set Baudrate = 9600                RX            => RX,        TX            => TX,        TX_Enable     => UART_Interface_TX_Enable,        TX_Busy       => UART_Interface_TX_Busy,        TX_Data       => UART_Interface_TX_Data,    );}``

## Loop Example#

``Main (    LEDs_par : OUT STD_LOGIC_VECTOR(0 to 9);    LEDs_seq : OUT STD_LOGIC_VECTOR(0 to 9);){    Process() {                         --For parallel and procedural programming        For(i IN 0 to 9) {              --Turn every led on in the first clock cycle            LEDs_par(i) <= '1';         --LEDs_par <= (others => '1'); does the same        }                Thread {                        --Same program as Arduino program            For(VARIABLE i : INTEGER := 0; i < 10; i := i + 1) {                LEDs_seq(i) <= '1';     --Turn LEDs 0-9 on            }            Wait(1000ms);               --Wait            i := 0;            While(i < 10) {             --Turn LEDs 0-9 off                LEDs_seq(i) <= '0';                i := i + 1;            }            Wait(1000ms);               --Wait        }    }}``

## Loop Comparison#

The parallel `For` and `While` can be also be used in a `Thread`.

``Main (    LEDs : OUT STD_LOGIC_VECTOR(0 to 9);){    Process() {        Thread {            --Turn on LEDs 0-9 sequentially in 12 clock cycles (10 + 1 for start and end)            For(VARIABLE i : INTEGER := 0; i < 10; i := i + 1) {                LEDs(i) <= '1';             }            Wait(1000ms);       --Wait            --Turn on LEDs 0-9 parallely in 1 clock cycle            ParFor(i IN 0 to 9) {                       LEDs(i) <= '1';                 }            Wait(1000ms);       --Wait        }    }}``
Last updated on by Hendrik Mennen