Arduino Audio VU Meter Stereo I2C LCD 16X2

oke kembali lagi oprek-oprek arduino… sekarang maen vu meter stereo dengan menggunakan lcd dan i2c untuk koneksinya ke lcd … oke langsung aja ke script na ya …

#include  Wire.h
#include  LiquidCrystal_I2C.hLiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display

#define IN_LEFT    2
#define IN_RIGHT   3

#define T_REFRESH    100            // msec bar refresh rate
#define T_PEAKHOLD   3*T_REFRESH    // msec peak hold time before return

byte  fill[6]={ 0x20,0x00,0x01,0x02,0x03,0xFF };      // character used to fill (0=empty  5=full)
byte  peak[7]={ 0x20,0x00,0x04,0x05,0x06,0x07,0x20 }; // character used to peak indicator
int   lmax[2];                                        // level max memory
int   dly[2];                                         // delay & speed for peak return

void  bar  ( int row,int lev )
{
  lcd.setCursor( 0,row );
  lcd.write( row ? ‘R’ : ‘L’ );
  for( int i=1 ; i<16 i="" p="">  {
    int f=constrain( lev      -i*5,0,5 );
    int p=constrain( lmax[row]-i*5,0,6 );
    if( f )
      lcd.write( fill[ f ] );
    else
      lcd.write( peak[ p ] );
  }
  if( lev>lmax[row] )
  {
    lmax[row] = lev;
    dly[row]  = -(T_PEAKHOLD)/T_REFRESH;                // Starting delay value. Negative=peak don’t move
  }
  else
  {
    if( dly[row]>0 )
      lmax[row] -= dly[row];

    if( lmax[row]<0 p="">      lmax[row]=0;
    else
      dly[row]++;
  }
}

byte block[8][8]=
{
  { 0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10 },  // define character for fill the bar
  { 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18 },
  { 0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C },
  { 0x1E,0x1E,0x1E,0x1E,0x1E,0x1E,0x1E,0x1E },

  { 0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08 },  // define character for peak level
  { 0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04 },
  { 0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02 },
  { 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01 },
};

void  setup  ( void )
{
  lcd.init();                      // initialize the lcd
lcd.backlight();
// set up the LCD’s number of columns and rows:
lcd.begin(16, 2);
// Print setup message to the LCD.
lcd.print(”  AUDIO  LEVEL  “);
lcd.setCursor(0,1);
lcd.print(“STEREO – I2C LCD”);
delay(500);
  lcd.begin( 16,2 );
  for( int i=0 ; i<8 i="" p="">    lcd.createChar( i,block[i] );
}

long  lastT=0;

void  loop  ( void )
{
  if( millis()    return;
  lastT += T_REFRESH;  
  int anL = map( sqrt( analogRead( IN_LEFT  )*16 ),0,128,0,80 );  // sqrt to have non linear scale (better was log)
  int anR = map( sqrt( analogRead( IN_RIGHT )*16 ),0,128,0,80 );
  bar( 0,anL );
  bar( 1,anR );
}

berikut dapat dilihat di youtube :

Add a Comment

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Open chat