- 3D-Druck Einstieg und Tipps         
Ergebnis 1 bis 10 von 33

Thema: FM-Radio mit Arduino Pro Mini und Si4703 - Programm hängt sich auf!

Baum-Darstellung

Vorheriger Beitrag Vorheriger Beitrag   Nächster Beitrag Nächster Beitrag
  1. #12
    HaWe
    Gast
    ich finde bare-metal-C-Tipps zu Arduino C++ Klassen wie Wire auch nicht so hilfreich, denn gerade die Wire Class ist bereits hoch optimiert.
    Eventuell reicht es, wenn du ab und zu einfach mal deine i2c-Devices "anpingst" , um zu sehen, ob ein Fehler zurückgegeben wird, und danach erst (wenn kein Fehler) deine Device-Lese-Schreib-Funktionen aufrufst:

    Code:
    Wire.beginTransmission(addr); // transmit to device I2C addr
    int ioerr=Wire.endTransmission();
    if(ioerr) {   // Ausgabe eines Fehlers samt i2c-addr. 
      Serial.print("Fehler bei addr 0x"); 
      if (addr<16) { Serial.print("0"); }
      Serial.print(addr,HEX); 
      Serial.print(" ioerr=");  
      Serial.println(ioerr); 
    }
    So kannst du checken, wie oft hier überhaupt wo was schief läuft.

    Aber mal eine ganz andere Frage :
    Wo in deinem Sketch (void setup() ) startest du denn überhaupt Wire mit Wire.begin() - irgendwie sehe ich es nicht....?
    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/ti...Voltmeter.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();
     }
    Geändert von HaWe (17.02.2018 um 12:06 Uhr) Grund: code verschönert

Ähnliche Themen

  1. Unit 1.2 hängt sich auf
    Von Billy51 im Forum Open Source Software Projekte
    Antworten: 0
    Letzter Beitrag: 06.03.2011, 12:52
  2. Timer hängt sich auf?
    Von Ineedhelp im Forum C - Programmierung (GCC u.a.)
    Antworten: 15
    Letzter Beitrag: 22.08.2008, 16:49
  3. Programm hängt sich auf
    Von martin66119 im Forum Basic-Programmierung (Bascom-Compiler)
    Antworten: 5
    Letzter Beitrag: 07.10.2007, 21:06
  4. LCD hängt sich auf
    Von hotijack im Forum Basic-Programmierung (Bascom-Compiler)
    Antworten: 4
    Letzter Beitrag: 30.05.2007, 16:47
  5. Lade Programm geht nicht (hängt sich auf)
    Von REX im Forum Robby CCRP5
    Antworten: 1
    Letzter Beitrag: 11.09.2004, 04:19

Berechtigungen

  • Neue Themen erstellen: Nein
  • Themen beantworten: Nein
  • Anhänge hochladen: Nein
  • Beiträge bearbeiten: Nein
  •  

Solar Speicher und Akkus Tests