Управление эквалайзером
- Войдите на сайт для отправки комментариев
Сб, 05/09/2015 - 20:27
Очень плохо многое понимаю, но не могу сделать элементарную работу кнопки, программа постоянно ожидает команды от энкодера. Как тут сделать кнопку, что-бы при нажатии она записывала в BAND от 0 до 5?
#include <MP1246.h>
#include <MP1090S.h>
#include <Wire.h>
#include <PinChangeInt.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
//const int Radio_PWR_Pin = 12;
//LED
int led = 9; // the pin that the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
// Init LCD
#define LCD_I2C_ADDR 0x27
#define BACKLIGHT 3
#define LCD_EN 2
#define LCD_RW 1
#define LCD_RS 0
#define LCD_D4 4
#define LCD_D5 5
#define LCD_D6 6
#define LCD_D7 7
// Объект для работы с дисплеем
LiquidCrystal_I2C lcd(LCD_I2C_ADDR,
LCD_EN,
LCD_RW,
LCD_RS,
LCD_D4,
LCD_D5,
LCD_D6,
LCD_D7);
// Массив символов для шкалы
const uint8_t charBitmap[][8] = {
{ 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10 },
{ 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18 },
{ 0x1c, 0x1c, 0x1c, 0x1c, 0x1c, 0x1c, 0x1c, 0x1c },
{ 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e },
{ 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f }
};
//Кнопка выбора --------------------------------//
int buttonPin = 2;
//const int buttonPin = 2;
const int MP1093_VOL_A = 8;
const int MP1093_VOL_B = 9;
const int MP1093_BAND_1_A = 16;
const int MP1093_BAND_1_B = 17;
const int MP1093_BAND_2_A = 2;
const int MP1093_BAND_2_B = 3;
const int MP1093_BAND_3_A = 4;
const int MP1093_BAND_3_B = 5;
const int MP1093_BAND_4_A = 6;
const int MP1093_BAND_4_B = 7;
const int MP1093_BAND_5_A = 14;
const int MP1093_BAND_5_B = 15;
const int MP1093_CounterClockWiseRot = 0;
const int MP1093_ClockWiseRot = 1;
const int MP1093_Unchanged = 2;
volatile byte VOLUME_FLAG = MP1093_Unchanged;
volatile byte BAND_1_FLAG = MP1093_Unchanged;
volatile byte BAND_2_FLAG = MP1093_Unchanged;
volatile byte BAND_3_FLAG = MP1093_Unchanged;
volatile byte BAND_4_FLAG = MP1093_Unchanged;
volatile byte BAND_5_FLAG = MP1093_Unchanged;
volatile byte BAND = 4;
void setup()
{
Serial.begin (9600);
Wire.begin();
// Кнопка -------------------------//
pinMode(buttonPin, INPUT);
int buttonState = digitalRead(buttonPin);
//attachInterrupt(0,BUTTON,LOW);
//PCintPort::attachInterrupt(MP1093_VOL_A, &MP1093_VOL_A_ISR, RISING);
//PCintPort::attachInterrupt(MP1093_VOL_B, &MP1093_VOL_B_ISR, RISING);
//PCintPort::attachInterrupt(MP1093_BAND_1_A, &MP1093_BAND_1_A_ISR, RISING);
//PCintPort::attachInterrupt(MP1093_BAND_1_B, &MP1093_BAND_1_B_ISR, RISING);
//PCintPort::attachInterrupt(MP1093_BAND_2_A, &MP1093_BAND_2_A_ISR, RISING);
//PCintPort::attachInterrupt(MP1093_BAND_2_B, &MP1093_BAND_2_B_ISR, RISING);
//PCintPort::attachInterrupt(MP1093_BAND_3_A, &MP1093_BAND_3_A_ISR, RISING);
//PCintPort::attachInterrupt(MP1093_BAND_3_B, &MP1093_BAND_3_B_ISR, RISING);
PCintPort::attachInterrupt(MP1093_BAND_4_A, &MP1093_BAND_4_A_ISR, RISING);
PCintPort::attachInterrupt(MP1093_BAND_4_B, &MP1093_BAND_4_B_ISR, RISING);
//PCintPort::attachInterrupt(MP1093_BAND_5_A, &MP1093_BAND_5_A_ISR, RISING);
//PCintPort::attachInterrupt(MP1093_BAND_5_B, &MP1093_BAND_5_B_ISR, RISING);
// MP1090::Init(Radio_PWR_Pin);
// MP1090::SetStation(95200);
// MP1090::SetVolume(3);
MP1246::Init ();
MP1246::SetVolume (0x00);
// Настройка
// LED pin 13 to be an output:
pinMode(led, OUTPUT);
// Создание символов из массива
int charBitmapSize = (sizeof(charBitmap ) / sizeof (charBitmap[0]));
// Последовательный порт
Serial.begin(9600);
// Аналоговый вход
//pinMode(A0,INPUT);
// Запись созданных символов в дисплей
lcd.begin(20,4);
for ( int i = 0; i < charBitmapSize; i++ )
{
lcd.createChar ( i, (uint8_t *)charBitmap[i] );
}
// Настройка подсветки
lcd.setBacklightPin(BACKLIGHT,POSITIVE);
lcd.setBacklight(HIGH);
//led print
{
lcd.setCursor ( 1, 1 );
lcd.setBacklight(LOW);
lcd.print ("LM 1875 Amplifier");
delay(100);
lcd.setBacklight(HIGH);
delay(1000);
}
// Подготовка надписей
lcd.clear();
lcd.setCursor(0,0);
// lcd.print("0 1 2 3 4 5");
}
//--------------------------------------------------------
void loop ()
{
int buttonState = digitalRead(buttonPin);
if ((buttonState == LOW))
{
BUTTON;
// BAND++ ;
delay(200);
}
if ((BAND_4_FLAG == MP1093_ClockWiseRot) || (BAND_4_FLAG == MP1093_CounterClockWiseRot))
{
ChangeBand (4, BAND_4_FLAG);
lcd.setCursor(10,0);
lcd.print ("B_4 ");
lcd.print (MP1246::GetBandValue (4), DEC);
lcd.print ("\n");
lcd.setCursor(0,1);
lcd.print (BAND_4_FLAG);
lcd.setCursor(0,2);
lcd.print (BAND);
lcd.setCursor(0,3);
lcd.print (buttonState);
BAND_4_FLAG = MP1093_Unchanged;
}
}
//--------------------------------------------------------
//--------------------------------------------------------
void BUTTON ()
{
int buttonState = digitalRead(buttonPin);
if (buttonState == LOW)
{ BAND++;
delay(100);}
//lcd.setCursor(0,0);
//lcd.print ("111");
}
void MP1093_VOL_A_ISR ()
{
if (PCintPort::arduinoPin == MP1093_VOL_A)
{
delay (100);
if ((digitalRead (MP1093_VOL_A)) && (!digitalRead (MP1093_VOL_B)))
{
VOLUME_FLAG = MP1093_ClockWiseRot;
}
}
}
//-----------------------------------------------------------------
void MP1093_VOL_B_ISR ()
{
if (PCintPort::arduinoPin == MP1093_VOL_B)
{
delay (100);
if ((digitalRead (MP1093_VOL_B)) && (!digitalRead (MP1093_VOL_A)))
{
VOLUME_FLAG = MP1093_CounterClockWiseRot;
}
}
}
//-----------------------------------------------------------------
void MP1093_BAND_1_A_ISR ()
{
if (PCintPort::arduinoPin == MP1093_BAND_1_A)
{
delay (100);
if ((digitalRead (MP1093_BAND_1_A)) && (!digitalRead (MP1093_BAND_1_B)))
{
BAND_1_FLAG = MP1093_ClockWiseRot;
}
}
}
//-----------------------------------------------------------------
void MP1093_BAND_1_B_ISR ()
{
if (PCintPort::arduinoPin == MP1093_BAND_1_B)
{
delay (100);
if ((digitalRead (MP1093_BAND_1_B)) && (!digitalRead (MP1093_BAND_1_A)))
{
BAND_1_FLAG = MP1093_CounterClockWiseRot;
}
}
}
//-----------------------------------------------------------------
void MP1093_BAND_2_A_ISR ()
{
if (PCintPort::arduinoPin == MP1093_BAND_2_A)
{
delay (100);
if ((digitalRead (MP1093_BAND_2_A)) && (!digitalRead (MP1093_BAND_2_B)))
{
BAND_2_FLAG = MP1093_ClockWiseRot;
}
}
}
//-----------------------------------------------------------------
void MP1093_BAND_2_B_ISR ()
{
if (PCintPort::arduinoPin == MP1093_BAND_2_B)
{
delay (100);
if ((digitalRead (MP1093_BAND_2_B)) && (!digitalRead (MP1093_BAND_2_A)))
{
BAND_2_FLAG = MP1093_CounterClockWiseRot;
}
}
}
//-----------------------------------------------------------------
void MP1093_BAND_3_A_ISR ()
{
if (PCintPort::arduinoPin == MP1093_BAND_3_A)
{
delay (100);
if ((digitalRead (MP1093_BAND_3_A)) && (!digitalRead (MP1093_BAND_3_B)))
{
BAND_3_FLAG = MP1093_ClockWiseRot;
}
}
}
//-----------------------------------------------------------------
void MP1093_BAND_3_B_ISR ()
{
if (PCintPort::arduinoPin == MP1093_BAND_3_B)
{
delay (100);
if ((digitalRead (MP1093_BAND_3_B)) && (!digitalRead (MP1093_BAND_3_A)))
{
BAND_3_FLAG = MP1093_CounterClockWiseRot;
}
}
}
//-----------------------------------------------------------------
void MP1093_BAND_4_A_ISR ()
{
if (PCintPort::arduinoPin == MP1093_BAND_4_A)
{
delay (10);
if ((digitalRead (MP1093_BAND_4_A)) && (!digitalRead (MP1093_BAND_4_B)))
{
BAND_4_FLAG = MP1093_ClockWiseRot;
}
}
}
//-----------------------------------------------------------------
void MP1093_BAND_4_B_ISR ()
{
if (PCintPort::arduinoPin == MP1093_BAND_4_B)
{
delay (10);
if ((digitalRead (MP1093_BAND_4_B)) && (!digitalRead (MP1093_BAND_4_A)))
{
BAND_4_FLAG = MP1093_CounterClockWiseRot;
}
}
}
//-----------------------------------------------------------------
void MP1093_BAND_5_A_ISR ()
{
if (PCintPort::arduinoPin == MP1093_BAND_5_A)
{
delay (100);
if ((digitalRead (MP1093_BAND_5_A)) && (!digitalRead (MP1093_BAND_5_B)))
{
BAND_5_FLAG = MP1093_ClockWiseRot;
}
}
}
//-----------------------------------------------------------------
void MP1093_BAND_5_B_ISR ()
{
if (PCintPort::arduinoPin == MP1093_BAND_5_B)
{
delay (100);
if ((digitalRead (MP1093_BAND_5_B)) && (!digitalRead (MP1093_BAND_5_A)))
{
BAND_5_FLAG = MP1093_CounterClockWiseRot;
}
}
}
//-----------------------------------------------------------------
void ChangeVolume ()
{
byte CurValue = MP1246::GetVolume ();
byte NewValue = CurValue;
NewValue++;
if (VOLUME_FLAG == MP1093_ClockWiseRot)
{
if (NewValue > 0)
NewValue--;
}
else if (VOLUME_FLAG == MP1093_CounterClockWiseRot)
{
if (NewValue < 0x2F)
NewValue++;
}
if (NewValue != CurValue)
MP1246::SetVolume (NewValue);
}
//-----------------------------------------------------------------
void ChangeBand (byte Band, byte BAND_FLAG)
{
byte CurValue = MP1246::GetBandValue (Band);
byte NewValue = CurValue;
if (BAND_FLAG == MP1093_ClockWiseRot)
{
if (NewValue > 0)
NewValue--;
}
else if (BAND_FLAG == MP1093_CounterClockWiseRot)
{
if (NewValue < 15)
NewValue++;
}
if (NewValue != CurValue)
MP1246::SetBand (Band, NewValue);
}
//-----------------------------------------------------------------
Кнопку сделал, но опять тупик. Я хочу кнопкой переключать полосы эквалайзера 5 шт., код расчитан на 5 отдельных энкодеров. Как мне сделать переключение?
#include <MP1246.h> #include <Wire.h> #include <PinChangeInt.h> #include <LCD.h> #include <LiquidCrystal_I2C.h> //LED int led = 9; // the pin that the LED is attached to int brightness = 0; // how bright the LED is int fadeAmount = 5; // how many points to fade the LED by // Init LCD #define LCD_I2C_ADDR 0x27 #define BACKLIGHT 3 #define LCD_EN 2 #define LCD_RW 1 #define LCD_RS 0 #define LCD_D4 4 #define LCD_D5 5 #define LCD_D6 6 #define LCD_D7 7 // Объект для работы с дисплеем LiquidCrystal_I2C lcd(LCD_I2C_ADDR, LCD_EN, LCD_RW, LCD_RS, LCD_D4, LCD_D5, LCD_D6, LCD_D7); // Массив символов для шкалы const uint8_t charBitmap[][8] = { { 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10 }, { 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18 }, { 0x1c, 0x1c, 0x1c, 0x1c, 0x1c, 0x1c, 0x1c, 0x1c }, { 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e }, { 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f } }; //Кнопка выбора --------------------------------// const int buttonPin = 2; const int MP1093_VOL_A = 8; const int MP1093_VOL_B = 9; const int MP1093_BAND_1_A = 6; const int MP1093_BAND_1_B = 7; const int MP1093_BAND_2_A = 2; const int MP1093_BAND_2_B = 3; const int MP1093_BAND_3_A = 4; const int MP1093_BAND_3_B = 5; const int MP1093_BAND_4_A = 6; const int MP1093_BAND_4_B = 7; const int MP1093_BAND_5_A = 14; const int MP1093_BAND_5_B = 15; const int MP1093_CounterClockWiseRot = 0; const int MP1093_ClockWiseRot = 1; const int MP1093_Unchanged = 2; volatile byte VOLUME_FLAG = MP1093_Unchanged; volatile byte BAND_1_FLAG = MP1093_Unchanged; volatile byte BAND_2_FLAG = MP1093_Unchanged; volatile byte BAND_3_FLAG = MP1093_Unchanged; volatile byte BAND_4_FLAG = MP1093_Unchanged; volatile byte BAND_5_FLAG = MP1093_Unchanged; volatile byte Band = 1; void setup() { Serial.begin (9600); Wire.begin(); // Кнопка -------------------------// pinMode(buttonPin, INPUT); // int buttonState = digitalRead(buttonPin); //attachInterrupt(0,BUTTON,LOW); //PCintPort::attachInterrupt(MP1093_VOL_A, &MP1093_VOL_A_ISR, RISING); //PCintPort::attachInterrupt(MP1093_VOL_B, &MP1093_VOL_B_ISR, RISING); PCintPort::attachInterrupt(MP1093_BAND_1_A, &MP1093_BAND_1_A_ISR, RISING); PCintPort::attachInterrupt(MP1093_BAND_1_B, &MP1093_BAND_1_B_ISR, RISING); //PCintPort::attachInterrupt(MP1093_BAND_2_A, &MP1093_BAND_2_A_ISR, RISING); //PCintPort::attachInterrupt(MP1093_BAND_2_B, &MP1093_BAND_2_B_ISR, RISING); //PCintPort::attachInterrupt(MP1093_BAND_3_A, &MP1093_BAND_3_A_ISR, RISING); //PCintPort::attachInterrupt(MP1093_BAND_3_B, &MP1093_BAND_3_B_ISR, RISING); PCintPort::attachInterrupt(MP1093_BAND_4_A, &MP1093_BAND_4_A_ISR, RISING); PCintPort::attachInterrupt(MP1093_BAND_4_B, &MP1093_BAND_4_B_ISR, RISING); //PCintPort::attachInterrupt(MP1093_BAND_5_A, &MP1093_BAND_5_A_ISR, RISING); //PCintPort::attachInterrupt(MP1093_BAND_5_B, &MP1093_BAND_5_B_ISR, RISING); MP1246::Init (); MP1246::SetVolume (0x00); // Настройка // LED pin 13 to be an output: pinMode(led, OUTPUT); // Создание символов из массива int charBitmapSize = (sizeof(charBitmap ) / sizeof (charBitmap[0])); // Последовательный порт Serial.begin(9600); // Аналоговый вход //pinMode(A0,INPUT); // Запись созданных символов в дисплей lcd.begin(20, 4); for ( int i = 0; i < charBitmapSize; i++ ) { lcd.createChar ( i, (uint8_t *)charBitmap[i] ); } // Настройка подсветки lcd.setBacklightPin(BACKLIGHT, POSITIVE); lcd.setBacklight(HIGH); //led print { lcd.setCursor ( 1, 1 ); lcd.setBacklight(LOW); lcd.print ("LM 1875 Amplifier"); delay(100); lcd.setBacklight(HIGH); delay(1000); } lcd.clear(); lcd.setCursor(0, 0); } //-------------------------------------------------------- void loop () { int buttonState = digitalRead(buttonPin); if ((buttonState == HIGH)) { lcd.setCursor(0, 2); lcd.setCursor(0, 3); delay(200); Band++ ; lcd.print (Band); } if ((Band > 4)) { Band = 0; delay(200); } if ((BAND_1_FLAG == MP1093_ClockWiseRot) || (BAND_1_FLAG == MP1093_CounterClockWiseRot)) { ChangeBand (Band, BAND_1_FLAG); lcd.setCursor(0, 1); lcd.print ("B_1 "); lcd.print (MP1246::GetBandValue (1), DEC); lcd.print ("\n"); BAND_1_FLAG = MP1093_Unchanged; } if ((BAND_4_FLAG == MP1093_ClockWiseRot) || (BAND_4_FLAG == MP1093_CounterClockWiseRot)) { ChangeBand (Band, BAND_4_FLAG); lcd.setCursor(10, 0); lcd.print ("B_4 "); lcd.print (MP1246::GetBandValue (4), DEC); lcd.print ("\n"); BAND_4_FLAG = MP1093_Unchanged; } } //-------------------------------------------------------- //-------------------------------------------------------- void MP1093_VOL_A_ISR () { if (PCintPort::arduinoPin == MP1093_VOL_A) { delay (100); if ((digitalRead (MP1093_VOL_A)) && (!digitalRead (MP1093_VOL_B))) { VOLUME_FLAG = MP1093_ClockWiseRot; } } } //----------------------------------------------------------------- void MP1093_VOL_B_ISR () { if (PCintPort::arduinoPin == MP1093_VOL_B) { delay (100); if ((digitalRead (MP1093_VOL_B)) && (!digitalRead (MP1093_VOL_A))) { VOLUME_FLAG = MP1093_CounterClockWiseRot; } } } //----------------------------------------------------------------- void MP1093_BAND_1_A_ISR () { if (PCintPort::arduinoPin == MP1093_BAND_1_A) { delay (100); if ((digitalRead (MP1093_BAND_1_A)) && (!digitalRead (MP1093_BAND_1_B))) { BAND_1_FLAG = MP1093_ClockWiseRot; } } } //----------------------------------------------------------------- void MP1093_BAND_1_B_ISR () { if (PCintPort::arduinoPin == MP1093_BAND_1_B) { delay (100); if ((digitalRead (MP1093_BAND_1_B)) && (!digitalRead (MP1093_BAND_1_A))) { BAND_1_FLAG = MP1093_CounterClockWiseRot; } } } //----------------------------------------------------------------- void MP1093_BAND_2_A_ISR () { if (PCintPort::arduinoPin == MP1093_BAND_2_A) { delay (100); if ((digitalRead (MP1093_BAND_2_A)) && (!digitalRead (MP1093_BAND_2_B))) { BAND_2_FLAG = MP1093_ClockWiseRot; } } } //----------------------------------------------------------------- void MP1093_BAND_2_B_ISR () { if (PCintPort::arduinoPin == MP1093_BAND_2_B) { delay (100); if ((digitalRead (MP1093_BAND_2_B)) && (!digitalRead (MP1093_BAND_2_A))) { BAND_2_FLAG = MP1093_CounterClockWiseRot; } } } //----------------------------------------------------------------- void MP1093_BAND_3_A_ISR () { if (PCintPort::arduinoPin == MP1093_BAND_3_A) { delay (100); if ((digitalRead (MP1093_BAND_3_A)) && (!digitalRead (MP1093_BAND_3_B))) { BAND_3_FLAG = MP1093_ClockWiseRot; } } } //----------------------------------------------------------------- void MP1093_BAND_3_B_ISR () { if (PCintPort::arduinoPin == MP1093_BAND_3_B) { delay (100); if ((digitalRead (MP1093_BAND_3_B)) && (!digitalRead (MP1093_BAND_3_A))) { BAND_3_FLAG = MP1093_CounterClockWiseRot; } } } //----------------------------------------------------------------- void MP1093_BAND_4_A_ISR () { if (PCintPort::arduinoPin == MP1093_BAND_4_A) { delay (100); if ((digitalRead (MP1093_BAND_4_A)) && (!digitalRead (MP1093_BAND_4_B))) { BAND_4_FLAG = MP1093_ClockWiseRot; } } } //----------------------------------------------------------------- void MP1093_BAND_4_B_ISR () { if (PCintPort::arduinoPin == MP1093_BAND_4_B) { delay (100); if ((digitalRead (MP1093_BAND_4_B)) && (!digitalRead (MP1093_BAND_4_A))) { BAND_4_FLAG = MP1093_CounterClockWiseRot; } } } //----------------------------------------------------------------- void MP1093_BAND_5_A_ISR () { if (PCintPort::arduinoPin == MP1093_BAND_5_A) { delay (100); if ((digitalRead (MP1093_BAND_5_A)) && (!digitalRead (MP1093_BAND_5_B))) { BAND_5_FLAG = MP1093_ClockWiseRot; } } } //----------------------------------------------------------------- void MP1093_BAND_5_B_ISR () { if (PCintPort::arduinoPin == MP1093_BAND_5_B) { delay (100); if ((digitalRead (MP1093_BAND_5_B)) && (!digitalRead (MP1093_BAND_5_A))) { BAND_5_FLAG = MP1093_CounterClockWiseRot; } } } //----------------------------------------------------------------- void ChangeVolume () { byte CurValue = MP1246::GetVolume (); byte NewValue = CurValue; NewValue++; if (VOLUME_FLAG == MP1093_ClockWiseRot) { if (NewValue > 0) NewValue--; } else if (VOLUME_FLAG == MP1093_CounterClockWiseRot) { if (NewValue < 0x2F) NewValue++; } if (NewValue != CurValue) MP1246::SetVolume (NewValue); } //----------------------------------------------------------------- void ChangeBand (byte Band, byte BAND_FLAG) { byte CurValue = MP1246::GetBandValue (Band); byte NewValue = CurValue; if (BAND_FLAG == MP1093_ClockWiseRot) { if (NewValue > 0) NewValue--; } else if (BAND_FLAG == MP1093_CounterClockWiseRot) { if (NewValue < 15) NewValue++; } if (NewValue != CurValue) MP1246::SetBand (Band, NewValue); } //-----------------------------------------------------------------