6
« : 23 Октября 2016, 19:38:02 »
#include "Wire.h"
#include "LiquidCrystal_I2C.h"
#include "FM1216.h"
LiquidCrystal_I2C lcd(0x27,16,2);
FM1216 tuner;
unsigned long currentTime;
unsigned long loopTime;
const int pin_A = 3; // pin 3
const int pin_B = 2; // pin 2
unsigned char encoder_A;
unsigned char encoder_B;
unsigned char encoder_A_prev = 0;
unsigned long frq = 129900;
unsigned long frq_old;
float frq_disp;
//-------------------------------------------
void setup()
{
pinMode(pin_A, INPUT_PULLUP);
pinMode(pin_B, INPUT_PULLUP);
currentTime = millis();
loopTime = currentTime;
lcd.init();
lcd.backlight();// Включаем подсветку дисплея
lcd.setCursor(0, 0);
tuner.init();
tuner.setFreq(frq);
Serial.begin(9600);
Serial.println("Start");
lcd.setCursor(0, 0);
lcd.print(" FM1256/I H-3 ");
Serial.println(" FM1256/I H-3 ");
lcd.setCursor(0, 1);
lcd.print(" TUNER ");
Serial.print(" TUNER ");
delay(3000);
tuner.setStereo(true); // FM Стерео xDD
}
//-------------------------------------------
void loop()
{
rotary_enc();
if (frq != frq_old)
{
Serial.println(frq);
tuner.setFreq(frq);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" FM RADIO");
Serial.println(" FM RADIO");
lcd.setCursor(0, 1);
lcd.print(" FRQ: ");
Serial.print(" FRQ: ");
frq_disp = frq;
lcd.print((frq_disp / 1000) - 22.6, 1); ////коррекция 22.6 MHz
lcd.print(" MHz");
Serial.print((frq_disp / 1000) - 22.6, 1);
Serial.print("Mhz");
}
frq_old = frq;
}
//-------------------------------------------
void rotary_enc()
{
currentTime = millis();
if (currentTime >= (loopTime + 5)) {
encoder_A = digitalRead(pin_A);
encoder_B = digitalRead(pin_B);
if ((!encoder_A) && (encoder_A_prev)) {
if (encoder_B) {
frq = frq + 100;
if (frq > 130600) {
frq = 87600;
}
}
else {
frq = frq - 100;
if (frq < 87600) {
frq = 130600;
}
}
}
encoder_A_prev = encoder_A;
loopTime = currentTime;
}
}
Моя переделка