Sistema de señalización de Tráfico

Autores: Diego Picazo García, Ángel Esparrells González, Lara Fernández Gutiérrez y Mireya De Diego Gordo

Información

Nuestro proyecto consiste en el diseño e implementación de una mochila con un sistema de señalización integrado, el cual sirve para dar indicaciones durante la circulación. Su uso está orientado principalmente a medios de desplazamiento como bicicletas, patines o patinetes.

Hemos elegido esta idea de proyecto ya que consideramos que al circular utilizando alguno de estos medios, es peligroso el no poder señalizar ningún tipo de indicación, salvo con la mano, lo cual puede producir gran cantidad de accidentes.

Materiales

  • Matriz LED 16×16 (31.99€)
  • Bateria de 4.5V (5€)
  • Cables (Equipo Docente)
  • Interruptores (1.30€/botón)
  • MPU-6050 (11.99€)
  • Protoboard (Equipo Docente)
  • Placa Arduino (Equipo Docente)
  • Mochila (7.99€)
  • Polímetro (Traído de casa)

Pasos Dados

  1. Lluvia de ideas
  2. Selección final del proyecto
  3. Planteamiento y esquema de organización y reparto de tareas
  4. Lista con los materiales a necesitar y posterior compra de estos
  5. Estudio del funcionamiento de los materiales adquiridos
  6. Inicio del código y conexiones mediante pruebas y correcciones
  7. Solución a problemas encontrados
  8. Diseño final del proyecto
  9. Redacción de memoria
  10. Realización de vídeo de demostración

Implementación

El software utilizado para la realización de la práctica ha sido Arduino. Hemos utilizado diferentes librerías:

  • Adafruit para la utilización de la matriz LED
  • MPU-6050 para la comunicación con el módulo de sensor de movimiento MPU-6050, permitiendo así obtener mediciones precisas y en tiempo real de la posición y la orientación de un objeto
  • Wire.h para la comunicación con el módulo del sensor de movimiento que usa el protocolo I2C, facilitando el proceso de lectura y escritura de datos desde y hacia el dispositivo

Problemas y Soluciones

  • Los cables no funcionaban bien y hubo que cambiarlos por un recubrimiento mejor y una calidad superior
  • El acelerómetro no hacía contacto correctamente y hubo que soldar las entradas
  • El acelerómetro no imprimía los valores correctos (error (127,127,127)), indagando por la red descubrimos que era un error común que estaba provocado por el propio dispositivo que estaba roto
  • Problemas de concurrencia de la placa de Arduino al leer dos entradas simultáneamente, analógicas y digitales. Resuelto quitando algunas de las comprobaciones de los intermitentes (señales digitales) y de esa forma evitando la interrupción con señales analógicas
  • Para disminuir la latencia, se ha acotado el tiempo de la función «STOP» en 6 unidades, 3 por encima y 3 por abajo del valor registrado, evitando falsas lecturas a la hora de estar parados con la mochila.
  • Tuvimos un problema con la memoria RAM de Arduino, lo que nos obligó en varias ocasiones a refactorizar el código de distintas formas

Reparto de Tareas

En relación al código hemos invertido aproximadamente unas 10 horas, ya que además de realizar la implementación, tuvimos que documentarnos a través de vídeos y apuntes de la asignatura sobre el funcionamiento de los materiales adquiridos.

Respecto al montaje del proyecto, hemos empleado 15 horas, ya que hemos ido probando el correcto funcionamiento de cada componente por separado antes de su completa elaboración.

En cuanto a la memoria, su realización nos ha supuesto el empleo de unas 5 horas.

Casos de Uso

Video de Demostración

Video explicativo del código y de la funcionalidad del sistema de alumbrado.

Código en Arduino

/*        Traffic Signaling system for scooters using Adafruit               *
 * @authors Diego Picazo, Lara Fernandez, Mireya de Diego, Angel Esparrells  *
 * @date 24/04/2004                                                          *
 * @version 1.3                                                              */
#include 
#include 
#include 
#include 
#include 
// PIN where the DIN of the matrix is located in our microcontroller
#define DATA_PIN 6
// DIGITAL PINs of the flashing lights
#define LEFT_FLASHING_PIN 4
#define RIGHT_FLASHING_PIN 8

/*                   MATRIX DECLARATION OR BUILDER                                      *
 * @param 1: Width of the matrix                                                        *
 * @param 2: Height of matrix                                                           *
 * @param 3: DATA PIN of the matrix                                                     *
 * @param 4: flags passed to the matrix, in this case:                                  *
 *    -  NEO_MATRIX_TOP, NEO_MATRIX_RIGHT -> Position of the first LED                  *
 *    -  NEO_MATRIX_ROWS, NEO_MATRIX_ZIGZAG -> Behaviour of the segments of the matrix  *
 * @param 5:                                                                            *
 *    -  NEO_GRB, NEO_KHZ800 -> Specifing that colouring is GRB with a 800KHz bitstream */
Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(16, 16, DATA_PIN,
  NEO_MATRIX_TOP     + NEO_MATRIX_LEFT +
  NEO_MATRIX_ROWS + NEO_MATRIX_ZIGZAG,
  NEO_GRB            + NEO_KHZ800);

/*                              COLOR PALETTE                                           *
 * @colors[0]: Amber                                                                    *
 * @colors[1]: Red                                                                      *
 * @colors[2]: Green                                                                    *
 * @colors[3]: White                                                                    */
const uint16_t colors[] = {
  matrix.Color(226, 148, 58), matrix.Color(255, 0, 0), matrix.Color(0, 255, 0), matrix.Color(255, 255, 255)};

// Position custom object to save the data read by the MPU controller
struct MPUPosition {
  byte X;
  byte Y;
  byte Z;
};
// Global variables of MPU controller
MPU6050 mpu;
int16_t ax, ay, az;
int16_t gx, gy, gz;
MPUPosition position;
float old_magnitude = 0, actual_magnitude = 0;

/*                  INIT THE MATRIX WITH SOME PROPERTIES                                */
void setup() {
  matrix.begin();
  matrix.setTextWrap(false);
  matrix.setBrightness(20);
  Wire.begin();
  mpu.initialize();
  mpu.CalibrateAccel(6);
  mpu.CalibrateGyro(6);
  pinMode(LEFT_FLASHING_PIN, INPUT);
  pinMode(RIGHT_FLASHING_PIN, INPUT);
  Serial.begin(9600);
}

/*                          DRAW BRAKES FUNCTION                                        */
void drawBrakes(){
  matrix.setTextColor(colors[1]);
  matrix.drawPixel(0, 0, colors[1]);
  matrix.drawPixel(1, 0, colors[1]);
  matrix.drawPixel(2, 0, colors[1]);
  matrix.drawPixel(3, 0, colors[1]);
  matrix.drawPixel(4, 0, colors[1]);
  matrix.drawPixel(5, 0, colors[1]);
  matrix.drawPixel(6, 0, colors[1]);
  matrix.drawPixel(7, 0, colors[1]);
  matrix.drawPixel(8, 0, colors[1]);
  matrix.drawPixel(9, 0, colors[1]);
  matrix.drawPixel(10, 0, colors[1]);
  matrix.drawPixel(11, 0, colors[1]);
  matrix.drawPixel(12, 0, colors[1]);
  matrix.drawPixel(13, 0, colors[1]);
  matrix.drawPixel(14, 0, colors[1]);
  matrix.drawPixel(15, 0, colors[1]);
  matrix.drawPixel(0, 1, colors[1]);
  matrix.drawPixel(1, 1, colors[1]);
  matrix.drawPixel(2, 1, colors[1]);
  matrix.drawPixel(3, 1, colors[1]);
  matrix.drawPixel(4, 1, colors[1]);
  matrix.drawPixel(5, 1, colors[1]);
  matrix.drawPixel(6, 1, colors[1]);
  matrix.drawPixel(7, 1, colors[1]);
  matrix.drawPixel(8, 1, colors[1]);
  matrix.drawPixel(9, 1, colors[1]);
  matrix.drawPixel(10, 1, colors[1]);
  matrix.drawPixel(11, 1, colors[1]);
  matrix.drawPixel(12, 1, colors[1]);
  matrix.drawPixel(13, 1, colors[1]);
  matrix.drawPixel(14, 1, colors[1]);
  matrix.drawPixel(15, 1, colors[1]);
  matrix.drawPixel(0, 2, colors[1]);
  matrix.drawPixel(1, 2, colors[1]);
  matrix.drawPixel(14, 2, colors[1]);
  matrix.drawPixel(15, 2, colors[1]);
  matrix.drawPixel(0, 3, colors[1]);
  matrix.drawPixel(1, 3, colors[1]);
  matrix.drawPixel(14, 3, colors[1]);
  matrix.drawPixel(15, 3, colors[1]);
  matrix.drawPixel(0, 4, colors[1]);
  matrix.drawPixel(1, 4, colors[1]);
  matrix.drawPixel(14, 4, colors[1]);
  matrix.drawPixel(15, 4, colors[1]);
  matrix.drawPixel(0, 5, colors[1]);
  matrix.drawPixel(1, 5, colors[1]);
  matrix.drawPixel(14, 5, colors[1]);
  matrix.drawPixel(15, 5, colors[1]);
  matrix.drawPixel(0, 6, colors[1]);
  matrix.drawPixel(1, 6, colors[1]);
  matrix.drawPixel(14, 6, colors[1]);
  matrix.drawPixel(15, 6, colors[1]);
  matrix.drawPixel(0, 7, colors[1]);
  matrix.drawPixel(1, 7, colors[1]);
  matrix.drawPixel(14, 7, colors[1]);
  matrix.drawPixel(15, 7, colors[1]);
  matrix.drawPixel(0, 8, colors[1]);
  matrix.drawPixel(1, 8, colors[1]);
  matrix.drawPixel(14, 8, colors[1]);
  matrix.drawPixel(15, 8, colors[1]);
  matrix.drawPixel(0, 9, colors[1]);
  matrix.drawPixel(1, 9, colors[1]);
  matrix.drawPixel(14, 9, colors[1]);
  matrix.drawPixel(15, 9, colors[1]);
  matrix.drawPixel(0, 10, colors[1]);
  matrix.drawPixel(1, 10, colors[1]);
  matrix.drawPixel(14, 10, colors[1]);
  matrix.drawPixel(15, 10, colors[1]);
  matrix.drawPixel(0, 11, colors[1]);
  matrix.drawPixel(1, 11, colors[1]);
  matrix.drawPixel(14, 11, colors[1]);
  matrix.drawPixel(15, 11, colors[1]);
  matrix.drawPixel(0, 12, colors[1]);
  matrix.drawPixel(1, 12, colors[1]);
  matrix.drawPixel(14, 12, colors[1]);
  matrix.drawPixel(15, 12, colors[1]);
  matrix.drawPixel(0, 13, colors[1]);
  matrix.drawPixel(1, 13, colors[1]);
  matrix.drawPixel(14, 13, colors[1]);
  matrix.drawPixel(15, 13, colors[1]);
  matrix.drawPixel(0, 14, colors[1]);
  matrix.drawPixel(1, 14, colors[1]);
  matrix.drawPixel(2, 14, colors[1]);
  matrix.drawPixel(3, 14, colors[1]);
  matrix.drawPixel(4, 14, colors[1]);
  matrix.drawPixel(5, 14, colors[1]);
  matrix.drawPixel(6, 14, colors[1]);
  matrix.drawPixel(7, 14, colors[1]);
  matrix.drawPixel(8, 14, colors[1]);
  matrix.drawPixel(9, 14, colors[1]);
  matrix.drawPixel(10, 14, colors[1]);
  matrix.drawPixel(11, 14, colors[1]);
  matrix.drawPixel(12, 14, colors[1]);
  matrix.drawPixel(13, 14, colors[1]);
  matrix.drawPixel(14, 14, colors[1]);
  matrix.drawPixel(15, 14, colors[1]);
  matrix.drawPixel(0, 15, colors[1]);
  matrix.drawPixel(1, 15, colors[1]);
  matrix.drawPixel(2, 15, colors[1]);
  matrix.drawPixel(3, 15, colors[1]);
  matrix.drawPixel(4, 15, colors[1]);
  matrix.drawPixel(5, 15, colors[1]);
  matrix.drawPixel(6, 15, colors[1]);
  matrix.drawPixel(7, 15, colors[1]);
  matrix.drawPixel(8, 15, colors[1]);
  matrix.drawPixel(9, 15, colors[1]);
  matrix.drawPixel(10, 15, colors[1]);
  matrix.drawPixel(11, 15, colors[1]);
  matrix.drawPixel(12, 15, colors[1]);
  matrix.drawPixel(13, 15, colors[1]);
  matrix.drawPixel(14, 15, colors[1]);
  matrix.drawPixel(15, 15, colors[1]);
  matrix.show(); // Show the shape in the matrix*/
}

/*                          DRAW WARNING ARROWS FUNCTION                                */
void drawArrowWarning() {
  matrix.clear(); // Cleaning the matrix
  matrix.drawPixel(12, 2, colors[0]);
  matrix.drawPixel(13, 2, colors[0]);
  matrix.drawPixel(10, 3, colors[0]);
  matrix.drawPixel(11, 3, colors[0]);
  matrix.drawPixel(12, 3, colors[0]);
  matrix.drawPixel(13, 3, colors[0]);
  matrix.drawPixel(8, 4, colors[0]);
  matrix.drawPixel(9, 4, colors[0]);
  matrix.drawPixel(10, 4, colors[0]);
  matrix.drawPixel(11, 4, colors[0]);
  matrix.drawPixel(12, 4, colors[0]);
  matrix.drawPixel(13, 4, colors[0]);
  matrix.drawPixel(6, 5, colors[0]);
  matrix.drawPixel(7, 5, colors[0]);
  matrix.drawPixel(8, 5, colors[0]);
  matrix.drawPixel(9, 5, colors[0]);
  matrix.drawPixel(10, 5, colors[0]);
  matrix.drawPixel(11, 5, colors[0]);
  matrix.drawPixel(12, 5, colors[0]);
  matrix.drawPixel(13, 5, colors[0]);
  matrix.drawPixel(4, 6, colors[0]);
  matrix.drawPixel(5, 6, colors[0]);
  matrix.drawPixel(6, 6, colors[0]);
  matrix.drawPixel(7, 6, colors[0]);
  matrix.drawPixel(8, 6, colors[0]);
  matrix.drawPixel(9, 6, colors[0]);
  matrix.drawPixel(10, 6, colors[0]);
  matrix.drawPixel(11, 6, colors[0]);
  matrix.drawPixel(12, 6, colors[0]);
  matrix.drawPixel(13, 6, colors[0]);
  matrix.drawPixel(2, 7, colors[0]);
  matrix.drawPixel(3, 7, colors[0]);
  matrix.drawPixel(4, 7, colors[0]);
  matrix.drawPixel(5, 7, colors[0]);
  matrix.drawPixel(6, 7, colors[0]);
  matrix.drawPixel(7, 7, colors[0]);
  matrix.drawPixel(8, 7, colors[0]);
  matrix.drawPixel(9, 7, colors[0]);
  matrix.drawPixel(10, 7, colors[0]);
  matrix.drawPixel(11, 7, colors[0]);
  matrix.drawPixel(12, 7, colors[0]);
  matrix.drawPixel(13, 7, colors[0]);
  matrix.drawPixel(2, 8, colors[0]);
  matrix.drawPixel(3, 8, colors[0]);
  matrix.drawPixel(4, 8, colors[0]);
  matrix.drawPixel(5, 8, colors[0]);
  matrix.drawPixel(6, 8, colors[0]);
  matrix.drawPixel(7, 8, colors[0]);
  matrix.drawPixel(8, 8, colors[0]);
  matrix.drawPixel(9, 8, colors[0]);
  matrix.drawPixel(10, 8, colors[0]);
  matrix.drawPixel(11, 8, colors[0]);
  matrix.drawPixel(12, 8, colors[0]);
  matrix.drawPixel(13, 8, colors[0]);
  matrix.drawPixel(4, 9, colors[0]);
  matrix.drawPixel(5, 9, colors[0]);
  matrix.drawPixel(6, 9, colors[0]);
  matrix.drawPixel(7, 9, colors[0]);
  matrix.drawPixel(8, 9, colors[0]);
  matrix.drawPixel(9, 9, colors[0]);
  matrix.drawPixel(10, 9, colors[0]);
  matrix.drawPixel(11, 9, colors[0]);
  matrix.drawPixel(12, 9, colors[0]);
  matrix.drawPixel(13, 9, colors[0]);
  matrix.drawPixel(6, 10, colors[0]);
  matrix.drawPixel(7, 10, colors[0]);
  matrix.drawPixel(8, 10, colors[0]);
  matrix.drawPixel(9, 10, colors[0]);
  matrix.drawPixel(10, 10, colors[0]);
  matrix.drawPixel(11, 10, colors[0]);
  matrix.drawPixel(12, 10, colors[0]);
  matrix.drawPixel(13, 10, colors[0]);
  matrix.drawPixel(8, 11, colors[0]);
  matrix.drawPixel(9, 11, colors[0]);
  matrix.drawPixel(10, 11, colors[0]);
  matrix.drawPixel(11, 11, colors[0]);
  matrix.drawPixel(12, 11, colors[0]);
  matrix.drawPixel(13, 11, colors[0]);
  matrix.drawPixel(10, 12, colors[0]);
  matrix.drawPixel(11, 12, colors[0]);
  matrix.drawPixel(12, 12, colors[0]);
  matrix.drawPixel(13, 12, colors[0]);
  matrix.drawPixel(12, 13, colors[0]);
  matrix.drawPixel(13, 13, colors[0]);
  matrix.show(); // Show the shape in the matrix
}

/*                          DRAW STOP FUNCTION                                          */
void drawStop() {
  matrix.clear(); // Cleaning the matrix
  matrix.setTextColor(colors[1]);
  // Fill pixels to make the stop's shape
  matrix.drawPixel(0, 0, colors[1]);
  matrix.drawPixel(1, 0, colors[1]);
  matrix.drawPixel(2, 0, colors[1]);
  matrix.drawPixel(3, 0, colors[1]);
  matrix.drawPixel(4, 0, colors[1]);
  matrix.drawPixel(5, 0, colors[1]);
  matrix.drawPixel(6, 0, colors[1]);
  matrix.drawPixel(7, 0, colors[1]);
  matrix.drawPixel(8, 0, colors[1]);
  matrix.drawPixel(9, 0, colors[1]);
  matrix.drawPixel(10, 0, colors[1]);
  matrix.drawPixel(11, 0, colors[1]);
  matrix.drawPixel(12, 0, colors[1]);
  matrix.drawPixel(13, 0, colors[1]);
  matrix.drawPixel(14, 0, colors[1]);
  matrix.drawPixel(15, 0, colors[1]);
  matrix.drawPixel(0, 1, colors[1]);
  matrix.drawPixel(1, 1, colors[1]);
  matrix.drawPixel(2, 1, colors[1]);
  matrix.drawPixel(3, 1, colors[1]);
  matrix.drawPixel(4, 1, colors[1]);
  matrix.drawPixel(5, 1, colors[3]);
  matrix.drawPixel(6, 1, colors[3]);
  matrix.drawPixel(7, 1, colors[3]);
  matrix.drawPixel(8, 1, colors[1]);
  matrix.drawPixel(9, 1, colors[1]);
  matrix.drawPixel(10, 1, colors[3]);
  matrix.drawPixel(11, 1, colors[1]);
  matrix.drawPixel(12, 1, colors[1]);
  matrix.drawPixel(13, 1, colors[1]);
  matrix.drawPixel(14, 1, colors[1]);
  matrix.drawPixel(15, 1, colors[1]);
  matrix.drawPixel(0, 2, colors[1]);
  matrix.drawPixel(1, 2, colors[1]);
  matrix.drawPixel(2, 2, colors[1]);
  matrix.drawPixel(3, 2, colors[1]);
  matrix.drawPixel(4, 2, colors[1]);
  matrix.drawPixel(5, 2, colors[3]);
  matrix.drawPixel(6, 2, colors[1]);
  matrix.drawPixel(7, 2, colors[3]);
  matrix.drawPixel(8, 2, colors[3]);
  matrix.drawPixel(9, 2, colors[3]);
  matrix.drawPixel(10, 2, colors[3]);
  matrix.drawPixel(11, 2, colors[1]);
  matrix.drawPixel(12, 2, colors[1]);
  matrix.drawPixel(13, 2, colors[1]);
  matrix.drawPixel(14, 2, colors[1]);
  matrix.drawPixel(15, 2, colors[1]);
  matrix.drawPixel(0, 3, colors[1]);
  matrix.drawPixel(1, 3, colors[1]);
  matrix.drawPixel(2, 3, colors[1]);
  matrix.drawPixel(3, 3, colors[1]);
  matrix.drawPixel(4, 3, colors[1]);
  matrix.drawPixel(5, 3, colors[1]);
  matrix.drawPixel(6, 3, colors[1]);
  matrix.drawPixel(7, 3, colors[1]);
  matrix.drawPixel(8, 3, colors[1]);
  matrix.drawPixel(9, 3, colors[1]);
  matrix.drawPixel(10, 3, colors[1]);
  matrix.drawPixel(11, 3, colors[1]);
  matrix.drawPixel(12, 3, colors[1]);
  matrix.drawPixel(13, 3, colors[1]);
  matrix.drawPixel(14, 3, colors[1]);
  matrix.drawPixel(15, 3, colors[1]);
  matrix.drawPixel(0, 4, colors[1]);
  matrix.drawPixel(1, 4, colors[1]);
  matrix.drawPixel(2, 4, colors[1]);
  matrix.drawPixel(3, 4, colors[1]);
  matrix.drawPixel(4, 4, colors[1]);
  matrix.drawPixel(5, 4, colors[3]);
  matrix.drawPixel(6, 4, colors[1]);
  matrix.drawPixel(7, 4, colors[1]);
  matrix.drawPixel(8, 4, colors[1]);
  matrix.drawPixel(9, 4, colors[1]);
  matrix.drawPixel(10, 4, colors[1]);
  matrix.drawPixel(11, 4, colors[1]);
  matrix.drawPixel(12, 4, colors[1]);
  matrix.drawPixel(13, 4, colors[1]);
  matrix.drawPixel(14, 4, colors[1]);
  matrix.drawPixel(15, 4, colors[1]);
  matrix.drawPixel(0, 5, colors[1]);
  matrix.drawPixel(1, 5, colors[1]);
  matrix.drawPixel(2, 5, colors[1]);
  matrix.drawPixel(3, 5, colors[1]);
  matrix.drawPixel(4, 5, colors[1]);
  matrix.drawPixel(5, 5, colors[3]);
  matrix.drawPixel(6, 5, colors[3]);
  matrix.drawPixel(7, 5, colors[3]);
  matrix.drawPixel(8, 5, colors[3]);
  matrix.drawPixel(9, 5, colors[3]);
  matrix.drawPixel(10, 5, colors[3]);
  matrix.drawPixel(11, 5, colors[1]);
  matrix.drawPixel(12, 5, colors[1]);
  matrix.drawPixel(13, 5, colors[1]);
  matrix.drawPixel(14, 5, colors[1]);
  matrix.drawPixel(15, 5, colors[1]);
  matrix.drawPixel(0, 6, colors[1]);
  matrix.drawPixel(1, 6, colors[1]);
  matrix.drawPixel(2, 6, colors[1]);
  matrix.drawPixel(3, 6, colors[1]);
  matrix.drawPixel(4, 6, colors[1]);
  matrix.drawPixel(5, 6, colors[3]);
  matrix.drawPixel(6, 6, colors[1]);
  matrix.drawPixel(7, 6, colors[1]);
  matrix.drawPixel(8, 6, colors[1]);
  matrix.drawPixel(9, 6, colors[1]);
  matrix.drawPixel(10, 6, colors[1]);
  matrix.drawPixel(11, 6, colors[1]);
  matrix.drawPixel(12, 6, colors[1]);
  matrix.drawPixel(13, 6, colors[1]);
  matrix.drawPixel(14, 6, colors[1]);
  matrix.drawPixel(15, 6, colors[1]);
  matrix.drawPixel(0, 7, colors[1]);
  matrix.drawPixel(1, 7, colors[1]);
  matrix.drawPixel(2, 7, colors[1]);
  matrix.drawPixel(3, 7, colors[1]);
  matrix.drawPixel(4, 7, colors[1]);
  matrix.drawPixel(5, 7, colors[1]);
  matrix.drawPixel(6, 7, colors[1]);
  matrix.drawPixel(7, 7, colors[1]);
  matrix.drawPixel(8, 7, colors[1]);
  matrix.drawPixel(9, 7, colors[1]);
  matrix.drawPixel(10, 7, colors[1]);
  matrix.drawPixel(11, 7, colors[1]);
  matrix.drawPixel(12, 7, colors[1]);
  matrix.drawPixel(13, 7, colors[1]);
  matrix.drawPixel(14, 7, colors[1]);
  matrix.drawPixel(15, 7, colors[1]);
  matrix.drawPixel(0, 8, colors[1]);
  matrix.drawPixel(1, 8, colors[1]);
  matrix.drawPixel(2, 8, colors[1]);
  matrix.drawPixel(3, 8, colors[1]);
  matrix.drawPixel(4, 8, colors[1]);
  matrix.drawPixel(5, 8, colors[3]);
  matrix.drawPixel(6, 8, colors[3]);
  matrix.drawPixel(7, 8, colors[3]);
  matrix.drawPixel(8, 8, colors[3]);
  matrix.drawPixel(9, 8, colors[3]);
  matrix.drawPixel(10, 8, colors[3]);
  matrix.drawPixel(11, 8, colors[1]);
  matrix.drawPixel(12, 8, colors[1]);
  matrix.drawPixel(13, 8, colors[1]);
  matrix.drawPixel(14, 8, colors[1]);
  matrix.drawPixel(15, 8, colors[1]);
  matrix.drawPixel(0, 9, colors[1]);
  matrix.drawPixel(1, 9, colors[1]);
  matrix.drawPixel(2, 9, colors[1]);
  matrix.drawPixel(3, 9, colors[1]);
  matrix.drawPixel(4, 9, colors[1]);
  matrix.drawPixel(5, 9, colors[3]);
  matrix.drawPixel(6, 9, colors[1]);
  matrix.drawPixel(7, 9, colors[1]);
  matrix.drawPixel(8, 9, colors[1]);
  matrix.drawPixel(9, 9, colors[1]);
  matrix.drawPixel(10, 9, colors[3]);
  matrix.drawPixel(11, 9, colors[1]);
  matrix.drawPixel(12, 9, colors[1]);
  matrix.drawPixel(13, 9, colors[1]);
  matrix.drawPixel(14, 9, colors[1]);
  matrix.drawPixel(15, 9, colors[1]);
  matrix.drawPixel(0, 10, colors[1]);
  matrix.drawPixel(1, 10, colors[1]);
  matrix.drawPixel(2, 10, colors[1]);
  matrix.drawPixel(3, 10, colors[1]);
  matrix.drawPixel(4, 10, colors[1]);
  matrix.drawPixel(5, 10, colors[3]);
  matrix.drawPixel(6, 10, colors[3]);
  matrix.drawPixel(7, 10, colors[3]);
  matrix.drawPixel(8, 10, colors[3]);
  matrix.drawPixel(9, 10, colors[3]);
  matrix.drawPixel(10, 10, colors[3]);
  matrix.drawPixel(11, 10, colors[1]);
  matrix.drawPixel(12, 10, colors[1]);
  matrix.drawPixel(13, 10, colors[1]);
  matrix.drawPixel(14, 10, colors[1]);
  matrix.drawPixel(15, 10, colors[1]);
  matrix.drawPixel(0, 11, colors[1]);
  matrix.drawPixel(1, 11, colors[1]);
  matrix.drawPixel(2, 11, colors[1]);
  matrix.drawPixel(3, 11, colors[1]);
  matrix.drawPixel(4, 11, colors[1]);
  matrix.drawPixel(5, 11, colors[1]);
  matrix.drawPixel(6, 11, colors[1]);
  matrix.drawPixel(7, 11, colors[1]);
  matrix.drawPixel(8, 11, colors[1]);
  matrix.drawPixel(9, 11, colors[1]);
  matrix.drawPixel(10, 11, colors[1]);
  matrix.drawPixel(11, 11, colors[1]);
  matrix.drawPixel(12, 11, colors[1]);
  matrix.drawPixel(13, 11, colors[1]);
  matrix.drawPixel(14, 11, colors[1]);
  matrix.drawPixel(15, 11, colors[1]);
  matrix.drawPixel(0, 12, colors[1]);
  matrix.drawPixel(1, 12, colors[1]);
  matrix.drawPixel(2, 12, colors[1]);
  matrix.drawPixel(3, 12, colors[1]);
  matrix.drawPixel(4, 12, colors[1]);
  matrix.drawPixel(5, 12, colors[3]);
  matrix.drawPixel(6, 12, colors[3]);
  matrix.drawPixel(7, 12, colors[3]);
  matrix.drawPixel(8, 12, colors[3]);
  matrix.drawPixel(9, 12, colors[3]);
  matrix.drawPixel(10, 12, colors[3]);
  matrix.drawPixel(11, 12, colors[1]);
  matrix.drawPixel(12, 12, colors[1]);
  matrix.drawPixel(13, 12, colors[1]);
  matrix.drawPixel(14, 12, colors[1]);
  matrix.drawPixel(15, 12, colors[1]);
  matrix.drawPixel(0, 13, colors[1]);
  matrix.drawPixel(1, 13, colors[1]);
  matrix.drawPixel(2, 13, colors[1]);
  matrix.drawPixel(3, 13, colors[1]);
  matrix.drawPixel(4, 13, colors[1]);
  matrix.drawPixel(5, 13, colors[3]);
  matrix.drawPixel(6, 13, colors[1]);
  matrix.drawPixel(7, 13, colors[3]);
  matrix.drawPixel(8, 13, colors[1]);
  matrix.drawPixel(9, 13, colors[1]);
  matrix.drawPixel(10, 13, colors[1]);
  matrix.drawPixel(11, 13, colors[1]);
  matrix.drawPixel(12, 13, colors[1]);
  matrix.drawPixel(13, 13, colors[1]);
  matrix.drawPixel(14, 13, colors[1]);
  matrix.drawPixel(15, 13, colors[1]);
  matrix.drawPixel(0, 14, colors[1]);
  matrix.drawPixel(1, 14, colors[1]);
  matrix.drawPixel(2, 14, colors[1]);
  matrix.drawPixel(3, 14, colors[1]);
  matrix.drawPixel(4, 14, colors[1]);
  matrix.drawPixel(5, 14, colors[3]);
  matrix.drawPixel(6, 14, colors[3]);
  matrix.drawPixel(7, 14, colors[3]);
  matrix.drawPixel(8, 14, colors[1]);
  matrix.drawPixel(9, 14, colors[1]);
  matrix.drawPixel(10, 14, colors[1]);
  matrix.drawPixel(11, 14, colors[1]);
  matrix.drawPixel(12, 14, colors[1]);
  matrix.drawPixel(13, 14, colors[1]);
  matrix.drawPixel(14, 14, colors[1]);
  matrix.drawPixel(15, 14, colors[1]);
  matrix.drawPixel(0, 15, colors[1]);
  matrix.drawPixel(1, 15, colors[1]);
  matrix.drawPixel(2, 15, colors[1]);
  matrix.drawPixel(3, 15, colors[1]);
  matrix.drawPixel(4, 15, colors[1]);
  matrix.drawPixel(5, 15, colors[1]);
  matrix.drawPixel(6, 15, colors[1]);
  matrix.drawPixel(7, 15, colors[1]);
  matrix.drawPixel(8, 15, colors[1]);
  matrix.drawPixel(9, 15, colors[1]);
  matrix.drawPixel(10, 15, colors[1]);
  matrix.drawPixel(11, 15, colors[1]);
  matrix.drawPixel(12, 15, colors[1]);
  matrix.drawPixel(13, 15, colors[1]);
  matrix.drawPixel(14, 15, colors[1]);
  matrix.drawPixel(15, 15, colors[1]);
  matrix.show(); // Show the shape in the matrix
}

/*                          DRAW FORWARD FUNCTION                                       */
void drawArrowForward() {
  matrix.clear(); // Cleaning the matrix
  matrix.setTextColor(colors[2]);
  byte pixels[36][2] = {
    {7, 4}, {7, 5}, {6, 5}, {5, 6}, {6, 6}, {7, 6}, {8, 6}, {9, 6},
    {10, 6}, {11, 6}, {4, 7}, {5, 7}, {6, 7}, {7, 7}, {8, 7}, {9, 7},
    {10, 7}, {11, 7}, {4, 8}, {5, 8}, {6, 8}, {7, 8}, {8, 8}, {9, 8},
    {10, 8}, {11, 8}, {5, 9}, {6, 9}, {7, 9}, {8, 9}, {9, 9}, {10, 9},
    {11, 9}, {7, 11}, {7, 10}, {6, 10}
  };
  // Draw the arrow's pixels
  for (int i = 0; i < 36; i++) {
    matrix.drawPixel(pixels[i][0], pixels[i][1], colors[2]);
  }
  matrix.show(); // Show the shape in the matrix
}

/*                          DRAW RIGHT ARROW FUNCTION                                   */
void drawArrowRight() {
  matrix.clear(); // Cleaning the matrix
  matrix.setTextColor(colors[0]);
  byte arrowPixels[36][2] = {
    {6,4}, {7,4}, {8,4}, {9,4}, {6,5}, {7,5}, {8,5}, {9,5},
    {6,6}, {7,6}, {8,6}, {9,6}, {6,7}, {7,7}, {8,7}, {9,7},
    {6,8}, {7,8}, {8,8}, {9,8}, {4,8}, {5,8}, {10,8}, {11,8},
    {5,9}, {10,9}, {6,9}, {7,9}, {8,9}, {9,9}, {6,10}, {7,10},
    {8,10}, {9,10}, {7,11}, {8,11}
  };
  for (byte i = 0; i < 36; i++) {
    matrix.drawPixel(arrowPixels[i][0], arrowPixels[i][1], colors[0]);
  }
  matrix.show();
}

/*                          DRAW LEFT ARROW FUNCTION                                    */
void drawArrowLeft() {
  matrix.clear(); // Cleaning the matrix
  matrix.setTextColor(colors[0]);
  byte arrowPixels[36][2] = {
    {7,4}, {8,4}, {6,5}, {9,5}, {7,5}, {8,5}, {5,6}, {6,6},
    {7,6}, {8,6}, {9,6}, {10,6}, {4,7}, {5,7}, {6,7}, {7,7},
    {8,7}, {9,7}, {10,7}, {11,7}, {6,8}, {7,8}, {8,8}, {9,8},
    {6,9}, {7,9}, {8,9}, {9,9}, {6,10}, {7,10}, {8,10}, {9,10},
    {6,11}, {7,11}, {8,11}, {9,11}
  };
  for (byte i = 0; i < 36; i++) {
    matrix.drawPixel(arrowPixels[i][0], arrowPixels[i][1], colors[0]);
  }
  matrix.show(); // Show the shape in the matrix
}
/*               FUNCTION THAT CHECKS IF ANY FLASHING LIGHT IS ON                       */
void checkFlashingLights(){
  int buttonLeft = digitalRead(LEFT_FLASHING_PIN);
  int buttonRight = digitalRead(RIGHT_FLASHING_PIN);
  if (buttonLeft == 0 && buttonRight == 0){
    //Serial.println("0 0");
    return;
  }
  if (buttonLeft == 1 && buttonRight == 1){
    delay(215);
    drawArrowWarning();
    //Serial.println("1 1");
  }
  if (buttonLeft == 1 && buttonRight == 0){
    delay(215);
    drawArrowLeft();
    //Serial.println("1 0");
  }
  if (buttonLeft == 0 && buttonRight == 1){
    delay(215);
    drawArrowRight();
    //Serial.println("0 1");
  }
}

/*                          LOOP FUNCTION                                               */
void loop() {
  checkFlashingLights();
  mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
  position.X = map(ax, -17000, 17000, 0, 255 ); // X axis data
  checkFlashingLights();
  position.Y = map(ay, -17000, 17000, 0, 255); // Y axis data
  checkFlashingLights();
  position.Z = map(az, -17000, 17000, 0, 255); // Z axis data
  checkFlashingLights();
  actual_magnitude = sqrt(pow(float(position.X), 2) + pow(float(position.Y), 2) + pow(float(position.Z),2));
  checkFlashingLights();
  // Print in Serial Monitor actual position
  Serial.print("Axis X = ");
  Serial.print(position.X);
  Serial.print("  ");
  Serial.print("Axis Y = ");
  Serial.print(position.Y);
  Serial.print("  ");
  Serial.print("Axis Z  = ");
  Serial.print(position.Z);
  Serial.print("  ");
  Serial.print("Actual magnitude = ");
  Serial.println(actual_magnitude);
  if ((int(actual_magnitude) = int(old_magnitude) - 3)){
    checkFlashingLights();
    delay(215);
    drawStop();
  }
  else if (int(actual_magnitude) < int(old_magnitude)){
    checkFlashingLights();
    delay(215);
    drawBrakes();
  }
  else{
    checkFlashingLights();
    delay(215);
    drawArrowForward();
  }
  old_magnitude = actual_magnitude;
  delay(200);
}

También te podría gustar...

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *