Code:
#include <Arduino.h>
#include <Wire.h>
#include <radio.h>
#include <si4703.h>
#include "U8glib.h"
#include <avr/wdt.h> // only needed for Watchdog
#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
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE); // define type of display (OLED 0.96" 128x64 pix)
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"};
unsigned long startMillis; // for timing display interval
unsigned long currentMillis; // -||-
const unsigned long period = 1000; // display interval = 1 second
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() {
wdt_enable(WDTO_1S); // enable Watchdog, set time to 1 second
radio.init(); // initialize radio
Wire.begin();
Serial.begin(9600);
startMillis = millis(); // start timer for display interval
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"
// begin debugging I2C failure
int addr=0x3c; // für OLED
Wire.beginTransmission(addr); // transmit to device I2C addr
int ioerr=Wire.endTransmission();
if(ioerr) { // Ausgabe eines Fehlers samt i2c-addr.
Serial.println("vor OLED print:");
Serial.print("Fehler bei addr 0x");
if (addr<16) { Serial.print("0"); }
Serial.print(addr,HEX);
Serial.print(" ioerr=");
Serial.println(ioerr);
}
// end debugging I2C failure
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()
{
// begin debugging I2C failure
Serial.println("Test läuft: ");
int addr=0x10; // für SI4703
Wire.beginTransmission(addr); // transmit to device I2C addr
int ioerr=Wire.endTransmission();
if(ioerr) { // Ausgabe eines Fehlers samt i2c-addr.
Serial.println("für SI4703 print:");
Serial.print("Fehler bei addr 0x");
if (addr<16) { Serial.print("0"); }
Serial.print(addr,HEX);
Serial.print(" ioerr=");
Serial.println(ioerr);
}
addr=0x3c; // für OLED
Wire.beginTransmission(addr); // transmit to device I2C addr
ioerr=Wire.endTransmission();
if(ioerr) { // Ausgabe eines Fehlers samt i2c-addr.
Serial.println("für OLED print:");
Serial.print("Fehler bei addr 0x");
if (addr<16) { Serial.print("0"); }
Serial.print(addr,HEX);
Serial.print(" ioerr=");
Serial.println(ioerr);
}
// end debugging I2C failure
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);
displayData();
}
else if (buttonState_2 == LOW)
{
volume --;
if (volume < 0) volume = 0;
radio.setVolume(volume);
delay(100);
displayData();
}
else if (buttonState_3 == LOW)
{
stationNr ++;
if (stationNr == 10) stationNr = 0;
radio.setFrequency(fmFreq[stationNr]);
flag = 0;
delay(500);
displayData();
}
else if (buttonState_4 == LOW)
{
radio.seekUp();
flag = 1;
delay(500);
displayData();
}
else if (buttonState_5 == LOW)
{
radio.seekDown();
flag = 1;
delay(500);
displayData();
}
else
{
currentMillis = millis(); // get the current "time" (number of milliseconds since the circle started)
if (currentMillis - startMillis >= period) // test whether the period has elapsed
{
displayData(); // call display routine
startMillis = currentMillis; // save new start time for next display circle
}
}
wdt_reset(); // reset Watchdog
}
Lesezeichen