analog in with sensor to leds

33 views
Skip to first unread message

ixbalanque

unread,
Nov 25, 2012, 1:07:41 AM11/25/12
to toasted-circu...@googlegroups.com
I have a problem, I am attaching a sensor to the analog input of the AVR, the output voltage varies linearly from 0 to 1.56V, I want to do is: each change in sensor voltage, lighting the LED's will is ie, the LEDs turn on as the voltage changes have the sensor. I have not idea how to encode. 

This is my code:

#include <lightuino3.h>

int reloj = 5; // Reloj sincronización para los MM5451
int DatSerizq = 4; //Datos del MM5451 izq
int DatSerder = 6; //Datos del MM5451 der
int ctrlBrillo = 7; // Control de brillo CPU a MM5451's

/*****************************************************************
global var
*****************************************************************/
  int PinAnalogico = 0; //pin de entrada analógica
  int val = 0; //var para guardar el valor leído por el conversor
  
/* Preparación del programa general */
void setup()
{

  Serial.begin(9600);
}

void loop()  
{
  /* Condiciones iniciales */
  Lightuino board(reloj, DatSerizq, DatSerder, ctrlBrillo);
  board.flags |= CCShield_FASTSET; //Respuesta rápida basada en registros
  FlickerBrightness leds(board);
 
  board.set(0x0UL, 0x0UL,0x0);//Apaga driver de LED's
  
int res = 70;



for (int i=0;i<res;i++)//UP
        {
          val = analogRead(PinAnalogico)
          leds.brightness[i]= CCShield_MAX_BRIGHTNESS-1;
            Serial.println(i);
            delay(t);
          
        }// FIN UP
        for (int i=res;i>=0;i--)//DOWN
        {
          val = analogRead(PinAnalogico)
          leds.brightness[i]= CCShield_MAX_BRIGHTNESS-1;
             Serial.println(i);
            delay(t);
          leds.brightness[i]= 0;
        }// FIN DOWN

}// Termina ejecución

Andrew Stone

unread,
Nov 25, 2012, 4:45:53 PM11/25/12
to toasted-circu...@googlegroups.com
You basically have to read the analog pin and transform its value into a brightness value.  By default, the Lightuino's analog detector will detect a range from 0-5v and return a number from 0-1024.  But you have a range from 0 - 1.56v.  So the maximum value you will get is 1024*1.56/5 (maxVal).  To linearly translate into intensity, you'd want a result of maxVal to become CCShield_MAX_BRIGHTNESS and a result of 0 to become 0.  The equation in the modified code below does that.  But remember that the eye does not see twice as much energy as twice as bright -- it seems to see the square of the energy as twice as bright.  So you may want to make a look up table to translate the analog reading into a brightness.  On the other hand, your sensor may not be linear either :-).


for (int i=0;i<res;i++)//UP
        {
          val = analogRead(PinAnalogico);
          val = val * CCShield_MAX_BRIGHTNESS/(1024*1.56/5);
          leds.brightness[i]= val;
            Serial.println(i);
            delay(t);
          
        }// FIN UP


ixbalanque

unread,
Nov 25, 2012, 11:46:52 PM11/25/12
to toasted-circu...@googlegroups.com
hmm I think I did not explain well ... no need to change the brightness, I need something like this:

for example:

if sensor is equal to 0V -> turns LED0
if sensor is equal to 0.1V -> turns LED1
if sensor is equal to 0.2V -> turns LED2
if sensor is equal to 0.3V -> turns LED3
if sensor is equal to 0.4V -> turns LED4
                               ...                     ...

if sensor is equal to 0.3V -> turns LED3
if sensor is equal to 0.2V -> turns LED2
if sensor is equal to 0.1V -> turns LED1
if sensor is equal to 0V -> turns LED0

watch this video please:

My code is this now:

#include <lightuino3.h>

/* Asignación de pines del AVR a los MM5451 pines 4 a 7 
(4 últimos del bloque)*/
int reloj = 5; // Reloj sincronización para los MM5451
int DatSerizq = 4; //Datos del MM5451 izq
int DatSerder = 6; //Datos del MM5451 der
int ctrlBrillo = 7; // Control de brillo CPU a MM5451's

/*****************************************************************
  DECLARACIÓN DE VARIABLES GLOBALES
*****************************************************************/
  int PinAnalogico = 0; //pin de entrada analógica
  int val = 0; //var para guardar el valor leído por el conversor
  float ledsr;
  float ledcr;
  int led;
  
/* Preparación del programa general */
void setup()
{
  //Inicia el monitor serial arduino
  Serial.begin(9600);
}//finaliza setup

int lectura()
{
  val = analogRead(PinAnalogico);
/* analog in is of 0 to 1024 but my resolution es 0 to 70 LED
    then 1024/69LED = 14.84 because my LED0 = 0V */
    ledsr = val/14.84;
    ledcr = ledsr +0.5;
    led = (int) ledcr;
}

void loop()  //*************************** FUNCION DE EJECUCION **************************************
{
  /* Condiciones iniciales */
  Lightuino board(reloj, DatSerizq, DatSerder, ctrlBrillo);
  board.flags |= CCShield_FASTSET; //Respuesta rápida basada en registros
  FlickerBrightness leds(board); 
  board.set(0x0UL, 0x0UL,0x0);//Apaga driver de LED's
  leds.StartAutoLoop();//inicializa función autoloop para LED's
while (1)
{
  lectura();    
    leds.brightness[led]= CCShield_MAX_BRIGHTNESS-1;
      Serial.println(led);
      delay(led);
  lectura();
    leds.brightness[led]= 0;
    //leds.brightness[led]= CCShield_MAX_BRIGHTNESS-1;
}
}// Termina ejecución

thnks

ixbalanque

unread,
Nov 26, 2012, 12:00:18 PM11/26/12
to toasted-circu...@googlegroups.com
Help me Andrew!! please!

first achievement ... but I still fails :-(


my code is:

void loop()  //*************************** FUNCION DE EJECUCION **************************************
{
  /* Condiciones iniciales */
  Lightuino board(reloj, DatSerizq, DatSerder, ctrlBrillo);
  board.flags |= CCShield_FASTSET; //Respuesta rápida basada en registros
  FlickerBrightness leds(board); 
  board.set(0x0UL, 0x0UL,0x0);//Apaga driver de LED's
  leds.StartAutoLoop();//inicializa función autoloop para LED's
while (1)
{
  lectura1();    
    leds.brightness[led1]= CCShield_MAX_BRIGHTNESS-1;
      Serial.println(led1);
      delay(led1);
  lectura2();
      while (led2 < led1)
      {
        leds.brightness[led2]= 0;
        Serial.println(led2);
        delay(led2);
        lectura2();
        if (led2 == 0)
          return;
      }
    //leds.brightness[led]= CCShield_MAX_BRIGHTNESS-1;
}
}// Termina ejecución
Reply all
Reply to author
Forward
0 new messages