flux_diagram


\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usepackage{lib/freetikz}
\usetikzlibrary{shapes,arrows}


\begin{document}
\tikzstyle{decision} = [diamond, draw, fill=blue!20, 
    text width=4.5em, text badly centered, node distance=3cm, inner sep=0pt]
\tikzstyle{block} = [rectangle, draw, fill=blue!20, 
    text width=5em, text centered, rounded corners, minimum height=4em]
\tikzstyle{line} = [draw, -latex']
\tikzstyle{cloud} = [draw, ellipse,fill=red!20, node distance=3cm,
    minimum height=2em]

\begin{tikzpicture}[node distance = 2cm, auto]
    % Nodes
    \node [cloud] (init) {inicialització};
    \node [decision, below of=init] (state_change) {Canvi d'estat};
    \node [block, below of=state_change, node distance=3cm] (eval_interrupt) {Evaluació causa de la interrupció};
    \node [decision, below of=eval_interrupt] (state_3) {Joystick esquerre};
    \node [decision, below of=state_3] (state_1) {Botó S1};
    \node [decision, below of=state_1] (state_2) {Botó S2};
    \node [decision, below of=state_2] (state_4) {Joystick dreta};
    \node [decision, below of=state_4] (state_5) {Joystick amunt};
    \node [decision, below of=state_5] (state_6) {Joystick avall};
    \node [decision, below of=state_6] (state_7) {Joystick centre};
    
    \node [block, right of=state_3, node distance=3cm] (leds_left) {Set direcció esquerre};
    \node [block, right of=state_1, node distance=3cm] (leds_1) {Encén RGB};
    \node [block, right of=state_2, node distance=3cm] (leds_2)  {Apaga RGB};
    \node [block, right of=state_4, node distance=3cm] (leds_4)  {Encén RG Apaga B};
    \node [block, right of=state_5, node distance=3cm] (leds_5)  {Encén RB Apaga G};
    \node [block, right of=state_6, node distance=3cm] (leds_6)  {Encén RB Apaga R};
    \node [block, right of=state_7, node distance=3cm] (leds_7)  {Inverteix estat};
    
    \node [block, right of=leds_4, node distance=3cm] (leds_right) {Set direcció dreta};
    \node [block, right of=leds_5, node distance=3cm] (vel_up) {Augmenta velocitat transició};
    \node [block, right of=leds_6, node distance=3cm] (vel_down) {Disminueix velocitat transició};
    
    \node [decision, right of=state_1, xshift = 7.5cm] (sentit1) {Sentit esquerra?};
    \node [decision, right of=sentit1] (sentit2) {Sentit dreta?};
    \node [decision, below of=sentit1] (start1) {Cas inici o led final};
    \node [block, right of=start1, node distance=3cm] (inici1) {Encendre últim led};
    \node [block, below of=start1, node distance=3cm] (shift1) {Encendre led anterior};
    \node [decision, right of=sentit2] (start2) {Cas inici o led final};
    \node [block, right of=start2, node distance=3cm] (inici2) {Encendre primer led};
    \node [block, below of=start2, node distance=3cm] (shift2) {Encendre led següent};
    
    \node [block, right of=shift1, node distance=9cm] (delay) {Delay};
    \node [below of=init, node distance=1cm, draw=none] (ghost0) {};
    \node [right of=delay, node distance=3cm, draw=none] (ghost1) {};
    \node [right of=ghost0, node distance=22.5cm, draw=none] (ghost2) {};
    \node [right of=state_1, node distance=8cm] (ghost3) {};
    \node [right of=state_change, node distance=7.5cm] (ghost4) {};
    
 
    
    
    % Arestes
    \path [line] (init) -- (state_change);
    \path [line] (state_change) -- node {yes}(eval_interrupt);
    \path [line] (state_change.east) -- node[pos=0.05] {no}(state_change -| sentit1) -- (sentit1.north);
    \path [line] (eval_interrupt) -- (state_3);
    \path [line] (state_3) -- node {no}(state_1);
    \path [line] (state_1) -- node {no}(state_2);
    \path [line] (state_2) -- node {no}(state_4);
    \path [line] (state_4) -- node {no}(state_5);
    \path [line] (state_5) -- node {no}(state_6);
    \path [line] (state_6) -- node {no}(state_7);
    
    \path [line] (state_3) -- node {yes}(leds_left);
    \path [line] (state_1) -- node {yes}(leds_1);
    \path [line] (state_2) -- node {yes}(leds_2);
    \path [line] (state_4) -- node {yes}(leds_4);
    \path [line] (state_5) -- node {yes}(leds_5);
    \path [line] (state_6) -- node {yes}(leds_6);
    \path [line] (state_7) -- node {yes}(leds_7);
    
    \path [line] (leds_left) -- (leds_1);
    
    \path [line] (leds_4) -- (leds_right);
    \path [line] (leds_5) -- (vel_up);
    \path [line] (leds_6) -- (vel_down);
    
    \path[line] (sentit1) -- node {yes}(start1);
    \path[line] (sentit2) -- node {yes}(start2);
    \path[line] (start1) -- node {yes}(inici1);
    \path[line] (start2) -- node {yes}(inici2);

    \path [line] (sentit1) -- node {no} (sentit2);
    \path [line](sentit2) -- node[pos=.05] {no} (sentit2 |- ghost0) -- (ghost0);
    \path [line] (start1) -- node {no} (shift1);
    \path [line] (start2) -- node {no} (shift2);
    \path [line] (shift1) -- (delay);
    \path [line] (shift2) -- (delay);
    \path [line] (inici1) -- (delay);
    \path [line] (inici2) -- (delay);
    \path [line] (delay.east) -- (delay -| ghost1) -- (ghost1 |- ghost2) -- (ghost0);
    \path [line] (leds_1) -- (sentit1);
    \path [line] (leds_2) -- (leds_2 -| ghost3);
    \path [line] (leds_right) -- (leds_right -| ghost3);
    \path [line] (vel_up) -- (vel_up -| ghost3);
    \path [line] (vel_down) -- (vel_down -| ghost3);
    \path [line] (leds_7) -- (leds_7 -| ghost3) -- (ghost3);

\end{tikzpicture}
\end{document}