Lab_interaccio/2021/DRON-screen/DRON_screen_v2/DRON_screen_v2.ino
2025-02-25 21:29:42 +01:00

528 lines
12 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"
#include "Adafruit_NeoPixel.h"
#define RADIOSEND false
#define CE_PIN 12
#define CSN_PIN 2
#define DEBUG 0
#define TEST 0
#define SYMETRIC 0
#define SMOOTH 0
#define ALFA 0.3
#define RED 11
#define GREEN 10
#define BLUE 9
#define WHITE 6
#define SDO1 4
#define SDO2 8
#define SDO3 3
#define NUMPIXELS1 240 // fuego
#define NUMPIXELS2 240
#define NUMPIXELS3 240
int NUM_LEDS = 240;
int TotalSteps = NUM_LEDS;
int Index = TotalSteps-1;
int intensity = 100;
int delaylight = 250;
int amplilight = 240;
float ampliSmooth = 0;
float ampliSmoothLast = 0;
int sinus = 0;
float angle_rad = 0;
float sin_result = 0;
float angulo = 0;
int dibujar;
float incAngle;
unsigned long currentMillis = 0; // will store last time LED was updated
unsigned long previousMillis = 0; // will store last time LED was updated
const long intervalOn = 20;
const long intervalOff = 100;
//int idpin[8] = { 5, 13, A4, A3, A2, A1, A0, A5};
int idpin[6] = { A5, A0, A1, A2, A3, A4};
int chpin[2] = { 13, 5 }; // no se va a usar en esta App
Adafruit_NeoPixel pixels1 = Adafruit_NeoPixel(NUMPIXELS1, SDO1, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel pixels2 = Adafruit_NeoPixel(NUMPIXELS2, SDO2, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel pixels3 = Adafruit_NeoPixel(NUMPIXELS3, SDO3, NEO_GRB + NEO_KHZ800);
byte neopix_gamma[] = {
0, 0, 0, 0, 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, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2,
2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5,
5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10,
10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16,
17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25,
25, 26, 27, 27, 28, 29, 29, 30, 31, 32, 32, 33, 34, 35, 35, 36,
37, 38, 39, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 50,
51, 52, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 68,
69, 70, 72, 73, 74, 75, 77, 78, 79, 81, 82, 83, 85, 86, 87, 89,
90, 92, 93, 95, 96, 98, 99,101,102,104,105,107,109,110,112,114,
115,117,119,120,122,124,126,127,129,131,133,135,137,138,140,142,
144,146,148,150,152,154,156,158,160,162,164,167,169,171,173,175,
177,180,182,184,186,189,191,193,196,198,200,203,205,208,210,213,
215,218,220,223,225,228,231,233,236,239,241,244,247,249,252,255
};
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
uint8_t CMD[ 8 ]; // CMD + Ch + 2
//int ch = 0x4C ; // ESTRELLA
int ch = 0x76 ; // JOGUINES
//int ch = 0x20 ; // GASPAR
// Globales PIXEL
unsigned long delayed = 0; // control de tiempo de funciones de pixel
int countFrame = 0;
unsigned long launchTime = 0;
short stroboPixelFlag = 0;
short pixelFlag = 0;
short pixelOn = 0;
short stroboPixelOn = 0;
float incRed, incGreen, incBlue, incWhite;
// Globales NEON
unsigned long delayedNeon = 0; // control de tiempo de funciones de neon
unsigned long launchTimeNeon = 0;
int countFrameNeon = 0;
short stroboFlagNeon = 0;
short neonFlag = 0;
short stroboNeonOn = 0;
short chflag = 0;
uint8_t param1 = 5;
uint8_t param2 = 100;
uint8_t selectedEffect = 0;
uint16_t i = 0;
int c = 0;
int numSer = 0;
char numStr[4];
byte globalNeonRed;
byte globalNeonGreen;
byte globalNeonBlue;
byte globalNeonWhite;
uint32_t p1, p2, p3, p4, p5, p6 ; // bytes para los comandos OSC (colores)
void resetBoard()
{
//Serial.println("reset");
NVIC_SystemReset(); // esta funcion en teoria si funciona en SAMD
}
byte id()
{
int id = 0;
for (byte i = 0; i < 6; i++ ) bitWrite(id, i, digitalRead(idpin[i]));
return id;
}
int channel()
{
int value = code();
switch (value)
{
case 0:
value = 0x10; //
break;
case 1:
value = 0x4C; //
break;
case 3:
value = 0x64; //
break;
case 4:
value = 0x7F; //
break;
default:
value = 0; //
break;
}
return value;
}
int code()
{
int value = 0;
for (byte i = 0; i < 2; i++) bitWrite(value, i, digitalRead(chpin[i]));
return value;
}
//////////////////////////////////////////////////////////////////
///////////////////////// SETUP ////////////////////////////////
//////////////////////////////////////////////////////////////////
void setup() {
for (int i = 0; i < 6; i++)
{
pinMode(idpin[i], INPUT);
}
for (int i = 0; i < 2; i++)
{
pinMode(chpin[i], INPUT);
}
pixels1.begin();
pixels2.begin();
pixels3.begin();
pixels1.show(); // Initialize all pixels to 'off'
pixels2.show(); // Initialize all pixels to 'off'
pixels3.show(); // Initialize all pixels to 'off'
Serial.begin(512000);
//while (!Serial) {
// ; // wait for serial port to connect. Needed for Leonardo only
//}
Serial.println("Serial READY");
delay(1000);
//printf_begin();
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.setPALevel( RF24_PA_MAX );
radio.setAutoAck(0);
radio.setPayloadSize(8);
radio.setChannel(ch);
//#if RADIOSEND
// radio.openWritingPipe(pipe);
//#else
radio.openReadingPipe(1, pipe);
radio.startListening();
//#endif
radio.printDetails();
}
//////////////////////////////////////////////////////////////////
///////////////////////// LOOP ////////////////////////////////
//////////////////////////////////////////////////////////////////
void loop()
{
//////////////////////////////////////////////////////////////////
///////////////////////// RADIO ENCUESTA ////////////////////////
//////////////////////////////////////////////////////////////////
if(DEBUG)
{
Serial.print("id: ");
Serial.println(id());
Serial.print("code: ");
Serial.println(code());
}
////////////////////////////////////////////////////////////
//////////////////////// RADIO ///////////////////////////
////////////////////////////////////////////////////////////
if ( radio.available() )
{
Serial.println("Radio packet");
while (radio.available())
{
//done = radio.read( CMD, sizeof(CMD) );
radio.read( CMD, sizeof(CMD) );
//if (DEBUG)
// {
Serial.println("Got command");
for (int i = 0 ; i < 8 ; i++)
{
Serial.print(CMD[i]);
Serial.print(",");
}
Serial.println();
//}
if ( CMD[0] == 0 )
{
intensity = CMD[1];
}
else if ( CMD[0] == 1 )
{
incAngle = CMD[1];
//incAngle = incAngle/255. ;
}
else if ( CMD[0] == 2 )
{
selectedEffect = 2; // CMD 2 - FADE IN OUT NEON
}
}
}
////////////////////////////////////////////////////////////
//////////////////////// DRAW ///////////////////////////
////////////////////////////////////////////////////////////
if(!code())
{
switch(id())
{
case 0:
delaylight = 0; // CONTINUOS
//delay(100);
break;
case 1:
delaylight = 250;
break;
case 2:
delaylight = 125;
break;
case 3:
delaylight = 75;
break;
case 4:
delaylight = 25;
break;
}
// CICLO ON PWM CUANDO DELAY ES MAYOR QUE CERO
// SIMETRICO Y ASIMETRICO
if(!SYMETRIC)
{
amplilight = NUM_LEDS/4 + random(NUM_LEDS);
//amplilight = NUM_LEDS/2 + random(NUM_LEDS/2);
//Serial.println(amplilight);
if(SMOOTH)
{
//filtered_output[i] = α*raw_input[i] + (1-α)*filtered_output[i-1]
ampliSmooth = ALFA*amplilight + (1-ALFA)*ampliSmoothLast;
ampliSmoothLast = ampliSmooth;
//Serial.println(ampliSmooth);
}
for(int i=0; i< ampliSmooth ; i++)
{
pixels2.setPixelColor( i, intensity, intensity, intensity, 0 );
pixels3.setPixelColor( i, intensity, intensity, intensity, 0 );
}
for(int i=ampliSmooth; i< NUM_LEDS ; i++)
{
pixels2.setPixelColor( i, 0, 0, 0, 0 );
pixels3.setPixelColor( i, 0, 0, 0, 0 );
}
showStrip();
}
else
{
amplilight = NUM_LEDS/2 + random(NUM_LEDS/2);
if(SMOOTH)
{
//filtered_output[i] = α*raw_input[i] + (1-α)*filtered_output[i-1]
ampliSmooth = ALFA*amplilight + (1-ALFA)*ampliSmoothLast;
ampliSmoothLast = ampliSmooth;
//Serial.println(ampliSmooth);
}
for(int i=0; i< NUM_LEDS ; i++)
{
pixels2.setPixelColor( i, 0, 0, 0, 0 );
pixels3.setPixelColor( i, 0, 0, 0, 0 );
}
for(int i = NUM_LEDS-ampliSmooth; i< ampliSmooth ; i++)
{
pixels2.setPixelColor( i, intensity, intensity, intensity, 0 );
pixels3.setPixelColor( i, intensity, intensity, intensity, 0 );
}
showStrip();
}
// CICLO OFF PWM CUANDO DELAY ES MAYOR QUE CERO
if(delaylight)
{
delay(delaylight/4);
for(int i=0; i<NUM_LEDS; i++)
{
pixels2.setPixelColor( i, 0, 0, 0, 0 );
pixels3.setPixelColor( i, 0, 0, 0, 0 );
}
showStrip();
delay(delaylight - (delaylight/4) );
}
}
else if(code() == 1) // full LED
{
for(int i=0; i<NUM_LEDS; i++)
{
pixels2.setPixelColor( i, intensity, intensity, intensity, 0 );
pixels3.setPixelColor( i, intensity, intensity, intensity, 0 );
}
showStrip();
delay(100);
}
else if(code() == 2) // sinus functions
{
angulo = angulo + 0.5;
if(angulo >= 360)
angulo = 0;
angle_rad = angulo * M_PI / 180.;
sin_result = (sin(angle_rad) + sin(2*angle_rad))/5.; // FUNCTION
amplilight = sin_result*(NUM_LEDS/2) + NUM_LEDS/2 ;
if(DEBUG)
{
Serial.print("sin(");
Serial.print(angulo);
Serial.print("°) = ");
Serial.print(sin_result);
Serial.print(" ");
Serial.println(amplilight);
}
for(int i=0; i< NUM_LEDS ; i++)
{
pixels2.setPixelColor( i, 0, 0, 0, 0 );
pixels3.setPixelColor( i, 0, 0, 0, 0 );
}
for(int i = 0; i< amplilight ; i++)
{
pixels2.setPixelColor( i, intensity, intensity, intensity, 0 );
pixels3.setPixelColor( i, intensity, intensity, intensity, 0 );
}
currentMillis = millis();
if (currentMillis - previousMillis >= intervalOff) {
previousMillis = currentMillis;
dibujar = 0;
}
else if(currentMillis - previousMillis <= intervalOn )
{
dibujar = 1;
}
else
dibujar = 0;
if(dibujar)
showStrip();
else
{
for(int i=0; i< NUM_LEDS ; i++)
{
pixels2.setPixelColor( i, 0, 0, 0, 0 );
pixels3.setPixelColor( i, 0, 0, 0, 0 );
}
showStrip();
}
//delay(20);
}
}
void splitStrip()
{
// CONVERSION DE UNA ANIMACION DE 5M A DOS TRAMOS
for ( int i = 0; i < 150 ; i++) // numPixels/2
{
pixels2.setPixelColor( i, pixels1.getPixelColor(i) );
pixels3.setPixelColor( i, pixels1.getPixelColor(i + 150) );
}
}
void showStrip()
{
//pixels1.show(); // tira entera de 5m
pixels2.show(); // parte 1 de 2.5m
pixels3.show(); // parte 2 de 2.5m
}
void setPixel(int Pixel, byte red, byte green, byte blue, byte white)
{
pixels1.setPixelColor(Pixel, pixels1.Color(red, green, blue, white));
}