Code:
/**************************************************************************
* This code is a modification/extract of several example-codes that come *
* with different si4703-libraries. Libraries used are fetched from: *
* http://www.mathertel.de/Arduino/RadioLibrary.aspx *
* Not sure if other libraries work with same code. *
* 5 push-buttons control the radio (see comments in code) *
* OLED-display shows 3 lines: *
* 1- "frequency" *
* 2- "station name" or "scan mode" *
* 3- "volume level" + "battery voltage" or "CHARGE BATTERY" *
* Code for voltage monitoring is fetched from: *
* https://code.google.com/archive/p/tinkerit/wikis/SecretVoltmeter.wiki *
* *
* U.Galepp Jan./2018 *
**************************************************************************/
#include <Arduino.h>
#include <Wire.h>
#include <radio.h>
#include <si4703.h>
#include "U8glib.h"
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE); // define type of display (OLED 0.96" 128x64 pix)
#define FIX_BAND RADIO_BAND_FM // the band that will be tuned by this sketch is FM
#define FIX_STATION 8760 // station at start-up is 87.60 MHz
#define FIX_VOLUME 1 // volume level at start-up
SI4703 radio; // Create an instance of Class for Si4703 Chip
int volume = FIX_VOLUME;
int button_1 = 3; // button volume up at Pin 3
int button_2 = 4; // button volume down at Pin 4
int button_3 = 5; // button station swap at Pin 5
int button_4 = 6; // button scan up at Pin 6
int button_5 = 7; // button scan down at Pin 7
int buttonState_1 = 0;
int buttonState_2 = 0;
int buttonState_3 = 0;
int buttonState_4 = 0;
int buttonState_5 = 0;
int flag = 0; // used for decission to show "Station Name" or "scan mode"
int stationNr = 0;
int fmFreq[10] = {8760, 8820, 9100, 9350, 9500, 10030, 10080, 10280, 10330, 10480};
char* fmName[10] = {" NDR 1 MV", " NDR Kultur", " NDR 1 MV", " NDR 2 MV",
" N-JOY", "Deutschlandfunk", " Antenne MV", " NDR Info",
"Radio Paradiso", " Ostseewelle"};
char vol[2];
float volts;
char akku[4];
long readVcc() {
long result;
// Read 1.1V reference against AVcc
ADMUX = _BV(REFS0) | _BV(MUX3)
| _BV(MUX2) | _BV(MUX1);
delay(2); // Wait for Vref to settle
ADCSRA |= _BV(ADSC); // Convert
while (bit_is_set(ADCSRA,ADSC));
result = ADCL;
result |= ADCH<<8;
result = 1126400L / result; // Back-calculate AVcc in mV
return result;
}
/******************************
* Setup *
*****************************/
void setup() {
radio.init(); // initialize radio
radio.setBandFrequency(FIX_BAND, FIX_STATION);
radio.setVolume(volume);
radio.setMono(false);
radio.setMute(false);
pinMode(button_1, INPUT); // define button Pins
pinMode(button_2, INPUT); // -||-
pinMode(button_3, INPUT); // -||-
pinMode(button_4, INPUT); // -||-
pinMode(button_5, INPUT); // -||-
digitalWrite(button_1, HIGH); // button-Pin Pull-up
digitalWrite(button_2, HIGH); // -||-
digitalWrite(button_3, HIGH); // -||-
digitalWrite(button_4, HIGH); // -||-
digitalWrite(button_5, HIGH); // -||-
}
/******************************
* Subroutine Display on OLED *
*****************************/
void displayData()
{
volts = (float)readVcc()/(float)1000; // read Vcc and divide by 1000
char s[12];
radio.formatFrequency(s, sizeof(s)); // format frequency value to output like: "87,60 MHz"
u8g.firstPage();
do {
u8g.drawFrame(0,0,128,64); // display frame around screen
u8g.setFont(u8g_font_timB14);
u8g.drawStr(15, 22, s);
u8g.setFont(u8g_font_unifont);
if(flag==0)
{
u8g.drawStr(4, 44, fmName[stationNr]); // if fequency from array, take name from array too
}
if(flag==1)
{
u8g.drawStr(25, 44, "scan mode"); // if frequency from scan, show "scan mode"
}
if(volts > 3.3) // if Vcc > 3.3V display "Volume" & "Volt"
{
u8g.setFont(u8g_font_6x13);
u8g.drawStr(6, 59,"Volume: ");
sprintf (vol, "%d", volume); // change int "volume" to char "vol"
u8g.drawStr(50, 59, vol);
u8g.drawStr(68, 59,"Bat:");
dtostrf(volts, 2, 1, akku); // change float "volts" to char "akku"
u8g.drawStr(94, 59,akku);
u8g.drawStr(115, 59,"V");
}
else // if VCC >= 3.3V display "CHARGE BATTERY"
{
u8g.drawStr(6, 59,"CHARGE BATTERY");
}
}
while( u8g.nextPage() );
}
/******************************
* Main Loop *
*****************************/
void loop()
{
buttonState_1 = digitalRead(button_1);
buttonState_2 = digitalRead(button_2);
buttonState_3 = digitalRead(button_3);
buttonState_4 = digitalRead(button_4);
buttonState_5 = digitalRead(button_5);
if (buttonState_1 == LOW)
{
volume ++;
if (volume == 16) volume = 15;
radio.setVolume(volume);
delay(100);
}
else if (buttonState_2 == LOW)
{
volume --;
if (volume < 0) volume = 0;
radio.setVolume(volume);
delay(100);
}
else if (buttonState_3 == LOW)
{
stationNr ++;
if (stationNr == 10) stationNr = 0;
radio.setFrequency(fmFreq[stationNr]);
flag = 0;
delay(500);
}
else if (buttonState_4 == LOW)
{
radio.seekUp();
flag = 1;
delay(500);
}
else if (buttonState_5 == LOW)
{
radio.seekDown();
flag = 1;
delay(500);
}
else displayData();
}
Prinzipiell funktioniert alles prima, nur leider friert das Ding immer mal wieder ein!
Lesezeichen