#define readPin A1
#define numReadings 10 //Количество чтений для усреднения
#define CLK 9
#define DIO 8
#include "GyverTM1637.h" // https://github.com/GyverLibs/GyverTM1637
#include <TimerMs.h>
TimerMs tmr(10000, 0, 1);
// A4 - SDA, A5 - SCL
//#include <Wire.h>
//#include <TEA5767Radio.h> //https://github.com/simonmonk/arduino_TEA5767
//TEA5767Radio radio = TEA5767Radio();
#include <RDA5807.h> // It is a minimal receicer with two push buttons (ATmega328 - Uno, Nano etc)
RDA5807 rx;
GyverTM1637 disp(CLK, DIO);
int band, band_play;
int readings[numReadings]; // Массив для хранения последних значений
int index = 0; // Текущий индекс массива
void setup() {
disp.clear();
disp.brightness(5);
pinMode(readPin, INPUT);
tmr.setTimerMode();
//Wire.begin();
rx.setup();
rx.setVolume(15);
rx.setMono(true);
}
void loop() {
// Чтение значения с потенциометра
int reading = analogRead(readPin);
// Запись значения в массив
readings[index] = reading;
index = (index + 1) % numReadings; // Циклическое обновление индекса
// Вычисление среднего значения
int total = 0;
for (int i = 0; i < numReadings; i++) {
total += readings[i];
}
band = (total / numReadings) + 4;
band = map(band, 0, 1023, 875, 1080);
// Небольшая пауза между чтениями
delay(10);
//band = analogRead(readPin);
//band = map(band, 0, 1023, 0, 255);
//disp.displayInt(band);
//band /= 100;
if (tmr.tick()) {
disp.brightness(0);
}
if (band != band_play) {
band_play = band;
rx.setFrequency(band_play);
//float play = band_play;
//play /= 10;
//radio.setFrequency(play);
disp.displayInt(band_play);
disp.brightness(5);
tmr.start();
}
}
/*
#include <EncButton2.h>
#include <TimerMs.h>
#include <Wire.h>
//#include <TEA5767Radio.h>
//#include <GyverOLED.h>
#include <RDA5807.h>
#define BTN_UP_PIN 8
#define BTN_DOWN_PIN 7
#define chargerPin 5
#define chargerPin_done 4
#define readPin A1
#define battery_min 3000
#define battery_max 4150
#define delta 0
#define CLK 2
#define DIO 3
#include "GyverTM1637.h"
//TEA5767Radio radio = TEA5767Radio();
//GyverOLED<SSD1306_128x64, OLED_NO_BUFFER> oled;
RDA5807 rx;
GyverTM1637 disp(CLK, DIO);
EncButton2<EB_BTN> btn_up(INPUT, BTN_UP_PIN);
EncButton2<EB_BTN> btn_down(INPUT, BTN_DOWN_PIN);
TimerMs tmr(1000, 1, 1);
// иконка батареи
const uint8_t bat25_7x12[] PROGMEM = {
0xFE, 0x02, 0x03, 0x03, 0x03, 0x02, 0xFE, 0x0F, 0x08, 0x0A,
0x0A, 0x0A, 0x08, 0x0F
};
const uint8_t bat50_7x12[] PROGMEM = {
0xFE, 0x02, 0x83, 0x83, 0x83, 0x02, 0xFE, 0x0F, 0x08, 0x0A,
0x0A, 0x0A, 0x08, 0x0F
};
const uint8_t bat75_7x12[] PROGMEM = {
0xFE, 0x02, 0xA3, 0xA3, 0xA3, 0x02, 0xFE, 0x0F, 0x08, 0x0A,
0x0A, 0x0A, 0x08, 0x0F
};
const uint8_t bat100_7x12[] PROGMEM = {
0xFE, 0x02, 0xAB, 0xAB, 0xAB, 0x02, 0xFE, 0x0F, 0x08, 0x0A,
0x0A, 0x0A, 0x08, 0x0F
};
const uint8_t bat_done_7x12[] PROGMEM = {
0xFE, 0x02, 0xFB, 0xFB, 0xFB, 0x02, 0xFE, 0x0F, 0x08, 0x0B,
0x0B, 0x0B, 0x08, 0x0F
};
uint8_t charging_animation = 0, counter = 0;
int battery, voltage;
bool charging, charging_done, screen_status = true;
int band, band_play = 916;
void setup() {
//Wire.begin();
disp.clear();
disp.brightness(5);
//disp.point(true);
//Serial.begin(9600);
//oled.init();
//oled.setScale(3);
//oled.setCursor(20, 2);
//oled.print("Radio");
//oled.setCursor(40, 5);
//oled.setScale(1);
//oled.print("v0.1");
//delay(100);
//TinyWireM.begin();
band = band_play;
//float play = band_play;
int play = band_play * 10;
//play /= 10;
//radio.setFrequency(play);
disp.displayInt(band_play);
//Serial.println(play);
rx.setup();
rx.setFrequency(play);
rx.setVolume(15);
analogReference(INTERNAL);
pinMode(readPin, INPUT);
btn_up.setButtonLevel(HIGH);
btn_down.setButtonLevel(HIGH);
btn_up.setStepTimeout(1024); //2048
btn_down.setStepTimeout(1024);
tmr.setPeriodMode();
pinMode(chargerPin, INPUT);
pinMode(chargerPin_done, INPUT);
delay(900);
//oled.clear();
//oled.update();
}
void loop() {
if (band != band_play) {
band_play = band;
//float play = band_play;
//play /= 10;
//radio.setFrequency(play);
int play = band_play * 10;
disp.displayInt(band_play);
rx.setFrequency(play);
//Serial.println(play);
}
btn_up.tick();
btn_down.tick();
if (screen_status == false) {
if ((btn_up.click()) || (btn_down.click())) {
screen_status = true;
//oled.setPower(screen_status);
disp.displayInt(band_play);
}
}
if (screen_status == true) {
//battery_check();
//main_screen();
if (btn_up.hasClicks(3)) {
counter = 0;
if (band < 1080) band += 100;
if (band > 1080) band = 1080;
}
if (btn_down.hasClicks(3)) {
counter = 0;
if (band > 875) band -= 100;
if (band < 875) band = 875;
}
if (btn_up.hasClicks(2)) {
counter = 0;
if (band < 1080) band += 10;
if (band > 1080) band = 1080;
}
if (btn_down.hasClicks(2)) {
counter = 0;
if (band > 875) band -= 10;
if (band < 875) band = 875;
}
if (btn_up.hasClicks(1)) {
counter = 0;
if (band < 1080) band++;
}
if (btn_down.hasClicks(1)) {
counter = 0;
if (band > 875) band--;
}
if (btn_up.step()) {
counter = 0;
if (band < 1080) band++;
}
if (btn_down.step()) {
counter = 0;
if (band > 875) band--;
}
if (counter > 10) {
counter = 0;
//oled.clear();
//oled.update();
screen_status = false;
disp.clear();
//oled.setPower(screen_status);
}
if (tmr.tick()) counter += 1;
}
}
/*
void main_screen() {
oled.setScale(3);
oled.setCursor(20, 2);
int one, two;
one = band / 10;
two = band - (one * 10);
oled.print(one);
oled.print(".");
oled.print(two);
if (one < 100) oled.print(" ");
oled.setScale(2);
oled.setCursor(40, 5);
oled.print(" MHz ");
charging = !digitalRead(chargerPin);
charging_done = !digitalRead(chargerPin_done);
if (charging == true) {
if ((tmr.tick())) {
switch (charging_animation) {
case 0:
oled.drawBitmap(1, 1, bat25_7x12, 7, 12, BITMAP_NORMAL, BUF_ADD);
charging_animation += 1;
break;
case 1:
oled.drawBitmap(1, 1, bat50_7x12, 7, 12, BITMAP_NORMAL, BUF_ADD);
charging_animation += 1;
break;
case 2:
oled.drawBitmap(1, 1, bat75_7x12, 7, 12, BITMAP_NORMAL, BUF_ADD);
charging_animation += 1;
break;
case 3:
oled.drawBitmap(1, 1, bat100_7x12, 7, 12, BITMAP_NORMAL, BUF_ADD);
charging_animation = 0;
break;
}
}
}
if (charging_done == true) {
oled.drawBitmap(1, 1, bat_done_7x12, 7, 12, BITMAP_NORMAL, BUF_ADD);
}
if ((charging == false) && (charging_done == false)) {
if (charging_animation > 0) {
charging_animation = 0;
}
if (battery > 75) {
oled.drawBitmap(1, 1, bat100_7x12, 7, 12, BITMAP_NORMAL, BUF_ADD);
} else if (battery > 50) {
oled.drawBitmap(1, 1, bat75_7x12, 7, 12, BITMAP_NORMAL, BUF_ADD);
} else if (battery > 25) {
oled.drawBitmap(1, 1, bat50_7x12, 7, 12, BITMAP_NORMAL, BUF_ADD);
} else if (battery >= 0) {
oled.drawBitmap(1, 1, bat25_7x12, 7, 12, BITMAP_NORMAL, BUF_ADD);
}
}
oled.update();
}
*/
/*
void battery_check() {
voltage = analogRead(readPin);
voltage = map(voltage, 1023, 0, 0, 5000) - delta;
battery = map(voltage, battery_min, battery_max, 0, 100);
if (battery > 100) battery = 100;
if (battery < 0) battery = 0;
}
*/