It seems that the loop is doing only one cycle after pressing the button, then stop.
This is a stereo VU meter, the goal is to switch displays from bar graph to value it in numbers, switching is ok, however respond to input is working incorrectly.
If i change input signal and press the button the changes are recordet only once, no reding in real time. What is wrong ?
Code: Select all
#include <LiquidCrystal.h>
///////////////////
const int button = PB11; // GPIO 8 for the button
const int led = PC13; // GPIO 7 for the LED
int ledflag = 0; // LED status flag
byte z, z0, z1;
int ur, ul, urr, ull;
/////////////////////
/* Modify the pin number below to meet your board
*/
#define IN_LEFT PB0 // analog input for left channel
#define IN_RIGHT PA6 // analog input for right channel
LiquidCrystal lcd(PA0, PA1, PA2, PA3, PA4, PA5);
#define T_REFRESH 50 // msec bar refresh rate
//#define T_PEAKHOLD 3*T_REFRESH // msec peak hold time before return
#define T_PEAKHOLD 1*T_REFRESH
/* local variable
*/
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
//int PC13;
void bar ( int row, int lev )
{
lcd.setCursor( 0, row );
lcd.write( row ? 'R' : 'L' );
for ( int i = 1 ; i < 16 ; i++ )
{
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 )
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.begin( 16, 2 );
for ( int i = 0 ; i < 8 ; i++ )
lcd.createChar( i, block[i] );
///////////////
pinMode(button, INPUT_PULLDOWN); // define button as an input
pinMode(led, OUTPUT); // define LED as an output
digitalWrite(led, LOW);
//////////////
}
long lastT = 0;
void loop ( void )
{
if ( millis() < lastT )
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 );// calibration *******************************************
int anR = map( sqrt( analogRead( IN_RIGHT ) * 4 ), 0, 128, 0, 80 );
///////////
if (digitalRead(button) == HIGH) { // if button is pressed
if (ledflag == 0) { // and the status flag is LOW
ledflag = 1; // make status flag HIGH
digitalWrite(led, HIGH);
// lcd.clear();
bar( 0, anL );
bar( 1, anR );
////////////////////
} //
else { // otherwise...
ledflag = 0; // make status flag LOW
digitalWrite(led, LOW);
//lcd.print(" . ");
lcd.clear();
lcd.setCursor(0, 1);
lcd.print(anR);// and turn off the LED
lcd.setCursor(0, 0);
lcd.print(anL);
// lcd.clear();
}
delay(10); // wait a sec for the
}
}