viernes, 10 de junio de 2016

Ejemplos Basicos (4) - Control de Led RGB



En este nuevo tutorial vamos a explicar como controlar un Led RGB o utilizando Transistores controlar una tira Led RGB de 12v, como veréis el ejemplo es muy similar al DimmableLight, simplemente almacenamos 3 valores de brillo, uno para cada color.

Vamos con el ejemplo:


/**************************************************************************
    Souliss - RGB LED for Expressif ESP8266
   
    Control and RGB LED or a Strip using the PWM, fade in/out and flash
    effects are available as the Android interface. 
   
    Load this code on ESP8266 board using the porting of the Arduino core
    for this platform.
       
***************************************************************************/

// Let the IDE point to the Souliss framework
#include "SoulissFramework.h"

// Configure the framework
#include "bconf/MCU_ESP8266.h"              // Load the code directly on the ESP8266
#include "conf/Gateway.h"                   // The main node is the Gateway, we have just one node
#include "conf/IPBroadcast.h"

// **** Define the WiFi name and password ****
#define WIFICONF_INSKETCH
#define WiFi_SSID               "mywifi"
#define WiFi_Password           "mypassword"   

// Include framework code and libraries
#include <ESP8266WiFi.h>
#include <EEPROM.h>

/*** All configuration includes should be above this line ***/
#include "Souliss.h"

// This identify the number of the LED logic
#define LEDCONTROL      0              
#define LEDRED                 1              
#define LEDGREEN            2        
#define LEDBLUE               3              

          

void setup()
{  
    Initialize();

    // Connect to the WiFi network and get an address from DHCP
    GetIPAddress();                          
    SetAsGateway(myvNet_dhcp);       // Set this node as gateway for SoulissApp 

    Set_LED_Strip(LEDCONTROL);
    pinMode(4, OUTPUT);                 // Power the LED
    pinMode(5, OUTPUT);                 // Power the LED
    pinMode(13, OUTPUT);               // Power the LED}


void loop()
{
    // Here we start to play
    EXECUTEFAST() {                    
        UPDATEFAST();  
       
        FAST_50ms() {   // We process the logic and relevant input and output every 50 milliseconds
             Logic_LED_Strip(LEDCONTROL);
            // Use the output values to control the PWM
            analogWrite(4, mOutput(LEDRED));
            analogWrite(5, mOutput(LEDGREEN));
            analogWrite(12, mOutput(LEDBLUE));

        }
             
        // Here we handle here the communication with Android
        FAST_GatewayComms();                                       
    }
    EXECUTESLOW()
    {  
        UPDATESLOW();

        SLOW_10s()  {
       
            // The timer handle timed-on states
            Timer_LED_Strip(LEDCONTROL);
                       
        }    
    }
}

Tal y como se explica al principio de la entrada se puede observar que no son muchos los cambios con respecto al DimmableLight, salvo que en lugar de un Pin de Salida utilizamos tres, en este caso hemos definido los Slots al principio del Sketch por comodidad, como se puede apreciar se utilizan 4 "huecos" en el memory_map para almacenar el estado y el valor de las 3 salidas por separado.

Por ultimo tambien se puede observar que utilizamos el Timer al igual que en el AutoLight.

Como siempre, para una comprension mas profunda de la estructura es recomendable leer esta Entrada: Estructura del código Souliss

Comentarios, sugerencias o preguntas aqui abajo :P Salu2

1 comentario:

  1. Buenas te hago una consulta, se realizo la conexion del led rgb se conecta todo perfecto, quiero cambiar el color con potenciometro y me desconecta la placa de red hasta que la desconecto vuelve de nuevo. Algo esta mal configurado?

    ResponderEliminar