Melodie abspielen nach empfangenem Infrarotpuls

vor 1 year 11 weeks von Djun

Hallo geschätzte Freeduinocommunity!

Der folgende Post bezieht sich auf ein umfangreicheres Projekt welches momentan sehr arduinolastig ist. Da ich mich aber hauptsächlich nicht mit Microcontrollern und Programmieren beschäftige, bin ich sehr auf eure Hilfe angewiesen! Ich versuche alles so weit es geht mir selbst zu erarbeiten aber leider hapert es bei mir hin und wieder bereits bei vermeintlich einfachen Dingen.

Kurzfassung meines Vorhabens (jedoch finde ich den passenden Code nirgends):
http://www.youtube.com/watch?v=zcu0UQvQPA8

Ich benutze ein Arduino Duemilanove, an dem ein Infrarotsensor angeschlossen ist. Über den entsprechenden Code (IRMP) empfängt dieser die Signale der zugehörigen Fernbedienung und gibt sie mir im Serialmonitor in Form von Zahlenwerten aus.

Über eine if-Funktion erreiche ich das Einschalten einer LED, sobald ich eine bestimmte Taste meiner FB drücke (in diesem Falle "Volume up") Mit "Volume down" schalte ich sie wieder aus. Nun möchte ich zusätzlich gerne eine kurze Melodie beim Drücken der "Volume up" Taste ausgegeben haben. Ein Lautsprecher habe ich bereits angeschlossen, welcher auch einen Ton ausgibt, mit dem nun bestehenden Problem:

der Ton hält nicht etwa nur für den Delay an, sondern wird durch void loop scheinbar ununterbrochen und endlos weitergegeben.
(Drücke ich auf meiner FB nun "Volume down" erlischt die LED und es ertönt ein anderer zugewiesener Ton, der ebenfalls nicht enden möchte)

Mit welcher Methode kann man ein einmaliges Abspielen eines bestimmten Tons / einer bestimmten Melodie erreichen sobald eine bestimmte Taste der FB gedrückt wird?

Ähnliche Posts

2 Antworten auf “Melodie abspielen nach empfangenem Infrarotpuls”


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
  1. Djun sagt:

    Ok, teilweise konnte ich das Problem nun bereits mit einer boolean-Funktion lösen. Der Ton ertönt nur kurz und endet dann auch wieder.

    Das neue Problem ist nun jedoch, dass ich nicht mehrfach die gleiche Taste drücken kann um damit weitere Male den Sound abzuspielen (ich muss jedesmal erst die "Volume down"-Taste drücken um erneut den Effekt der "Volume up"-Taste zu erhalten).

    Der Code:

    /* This is a simple example for using the IRMP library
    * original home of IRMP is: http://www.mikrocontroller.net/articles/IRMP
    * arduino port of IRMP home: https://gitorious.org/arduino-addons/irmp-arduino
    *
    * (C) 2012 Stefan Seyfried
    *
    * This program is free software. It comes without any warranty, to
    * the extent permitted by applicable law. You can redistribute it
    * and/or modify it under the terms of the Do What The Fuck You Want
    * To Public License, Version 2, as published by Sam Hocevar. See
    * http://sam.zoy.org/wtfpl/COPYING for more details.
    */

    /* use TimerOne http://arduino.cc/playground/Code/Timer1 for interrupts */
    #include
    /* first include Arduino.h, the IDE includes it after irmp*.h ... */
    #include "Arduino.h"
    /* ... and then chokes on uintX_t ... */

    extern "C" {
    #include
    #include
    }

    /* undefine this if you don't want blinking LED for diagnosis */
    #define LED_PIN 13
    #define SER_BAUD 115200

    /* F_INTERRUPTS is the interrupt frequency defined in irmpconfig.h */
    #define US (1000000 / F_INTERRUPTS)
    const int speakerPin = 4;
    const int ledPin = 6;
    boolean signal1 = false;
    boolean signal2 = false;

    void setup()
    {
    pinMode(ledPin, OUTPUT);
    pinMode(speakerPin, OUTPUT);
    Serial.begin(SER_BAUD);
    /* greeting string and debugging ouput */
    Serial.println("IRMP test sketch");
    Serial.print("US: ");
    Serial.println(US);
    /* configure the input pin. IRMP_BIT is defined in irmpconfig.h,
    default is digital pin 2 */
    pinMode(IRMP_BIT, INPUT);
    #ifdef LED_PIN
    pinMode(LED_PIN, OUTPUT);
    #endif
    led(HIGH);
    delay(20); /* make sure the greeting string is out before starting */
    led(LOW);
    Timer1.initialize(US);
    Timer1.attachInterrupt(timerinterrupt);
    }

    IRMP_DATA irmp_data;
    void loop()
    {
    if (irmp_get_data (&irmp_data))
    {
    led(HIGH);
    Serial.print(irmp_data.protocol);
    Serial.print(" A:");
    Serial.print(irmp_data.address, HEX);
    Serial.print(" C:");
    Serial.print(irmp_data.command);
    Serial.print(" ");
    Serial.print(irmp_data.flags, HEX);
    Serial.println("");
    /* Serial.print is asynchronous, so the LED is only flashing very lightly */
    led(LOW);
    }
    if((irmp_data.command == 57120)&&(signal1 == false))
    {
    digitalWrite(ledPin, HIGH);
    tone(4, 400, 100);
    delay(2000);
    signal1 = !signal1;
    signal2 = false;
    }
    if((irmp_data.command == 40800)&&(signal1 == true))
    {
    digitalWrite(ledPin, LOW);
    tone(4, 300,300);
    delay(2000);
    signal1 = false;
    signal2 = true;
    }
    }

    /* helper function: attachInterrupt wants void(), but irmp_ISR is uint8_t() */
    void timerinterrupt()
    {
    irmp_ISR();
    }

    static inline void led(int state)
    {
    #ifdef LED_PIN
    digitalWrite(LED_PIN, state);
    #endif
    }

    Login or register to post comments

  1. Djun sagt:

    ok, bin soweit im monolog mit mir selbst zum gewünschten ergebnis gekommen. Die beste Methode funktioniert bei mir ohne boolean-Funktion und erfordert die bestimmte Änderung des Wertes von irmp_data.command = 00000 (fünf Nullen einfach nur deshalb, da die anderen Werte auch fünfstellig sind (mit einer Ausnahme), aber es sollte auch mit nur einer "0" funktionieren).

    Der Vollständigkeit halber der zum Ziel führende Code. Das nächste Problem kommt aber bestimmt!

    Der folgende Code decodiert verschiedenste Fernbedienungen und zeigt im Serialmonitor einen entsprechenden Tastenwert an. Hier wurde eine FB von einem Universum Musik Center VTC-CD 3055 verwendet und jede der 16 Tasten wird erkannt und es ertönt jeweils der zugehörige Ton beim Drücken einer bestimmten Taste. Die eingefügten Libraries können auf untenstehendem Link heruntergeladen werden! (https://gitorious.org/arduino-addons/irmp-arduino)

    /* This is a simple example for using the IRMP library
    * original home of IRMP is: http://www.mikrocontroller.net/articles/IRMP
    * arduino port of IRMP home: https://gitorious.org/arduino-addons/irmp-arduino
    *
    * (C) 2012 Stefan Seyfried
    *
    * This program is free software. It comes without any warranty, to
    * the extent permitted by applicable law. You can redistribute it
    * and/or modify it under the terms of the Do What The Fuck You Want
    * To Public License, Version 2, as published by Sam Hocevar. See
    * http://sam.zoy.org/wtfpl/COPYING for more details.
    */

    /* use TimerOne http://arduino.cc/playground/Code/Timer1 for interrupts */
    #include
    /* first include Arduino.h, the IDE includes it after irmp*.h ... */
    #include "Arduino.h"
    /* ... and then chokes on uintX_t ... */

    extern "C" {
    #include
    #include
    }

    /* undefine this if you don't want blinking LED for diagnosis */
    #define LED_PIN 13
    #define SER_BAUD 115200

    /* F_INTERRUPTS is the interrupt frequency defined in irmpconfig.h */
    #define US (1000000 / F_INTERRUPTS)
    const int speakerPin = 4;
    const int ledPin = 6;

    void setup()
    {
    pinMode(ledPin, OUTPUT);
    pinMode(speakerPin, OUTPUT);
    Serial.begin(SER_BAUD);
    /* greeting string and debugging ouput */
    Serial.println("IRMP test sketch");
    Serial.print("US: ");
    Serial.println(US);
    /* configure the input pin. IRMP_BIT is defined in irmpconfig.h,
    default is digital pin 2 */
    pinMode(IRMP_BIT, INPUT);
    #ifdef LED_PIN
    pinMode(LED_PIN, OUTPUT);
    #endif
    led(HIGH);
    delay(20); /* make sure the greeting string is out before starting */
    led(LOW);
    Timer1.initialize(US);
    Timer1.attachInterrupt(timerinterrupt);
    }

    IRMP_DATA irmp_data;
    void loop()
    {
    if (irmp_get_data (&irmp_data))
    {
    led(HIGH);
    Serial.print(irmp_data.protocol);
    Serial.print(" A:");
    Serial.print(irmp_data.address, HEX);
    Serial.print(" C:");
    Serial.print(irmp_data.command);
    Serial.print(" ");
    Serial.print(irmp_data.flags, HEX);
    Serial.println("");
    /* Serial.print is asynchronous, so the LED is only flashing very lightly */
    led(LOW);
    }

    if(irmp_data.command == 57120) //Volume up
    {
    digitalWrite(ledPin, HIGH);
    tone(4, 100, 100);
    irmp_data.command = 00000;
    }

    if(irmp_data.command == 40800) //Volume down
    {
    digitalWrite(ledPin, LOW);
    tone(4, 200,100);
    irmp_data.command = 00000;
    }

    if(irmp_data.command == 24480) //Tuner
    {
    digitalWrite(ledPin, LOW);
    tone(4, 300,100);
    irmp_data.command = 00000;
    }

    if(irmp_data.command == 8160) //CD
    {
    digitalWrite(ledPin, LOW);
    tone(4, 400,100);
    irmp_data.command = 00000;
    }

    if(irmp_data.command == 61710) //Stop
    {
    digitalWrite(ledPin, LOW);
    tone(4, 500,100);
    irmp_data.command = 00000;
    }

    if(irmp_data.command == 61965) //Ply/Ps.
    {
    digitalWrite(ledPin, LOW);
    tone(4, 600,100);
    irmp_data.command = 00000;
    }

    if(irmp_data.command == 60435) //Tuning up
    {
    digitalWrite(ledPin, LOW);
    tone(4, 700,100);
    irmp_data.command = 00000;
    }

    if(irmp_data.command == 60180) //Tuning Down
    {
    digitalWrite(ledPin, LOW);
    tone(4, 800,100);
    irmp_data.command = 00000;
    }

    if(irmp_data.command == 62730) //Skip>>
    {
    digitalWrite(ledPin, LOW);
    tone(4, 900,100);
    irmp_data.command = 00000;
    }

    if(irmp_data.command == 62220) //Skip<<.
    {
    digitalWrite(ledPin, LOW);
    tone(4, 1000,100);
    irmp_data.command = 00000;
    }

    if(irmp_data.command == 62475) //Repeat
    {
    digitalWrite(ledPin, LOW);
    tone(4, 1100,100);
    irmp_data.command = 00000;
    }

    if(irmp_data.command == 58140) //Mute
    {
    digitalWrite(ledPin, LOW);
    tone(4, 1200,100);
    irmp_data.command = 00000;
    }

    if(irmp_data.command == 60945) //Band
    {
    digitalWrite(ledPin, LOW);
    tone(4, 1300,100);
    irmp_data.command = 00000;
    }

    if(irmp_data.command == 60690) //M.Scan
    {
    digitalWrite(ledPin, LOW);
    tone(4, 1400,100);
    irmp_data.command = 00000;
    }

    if(irmp_data.command == 62985) //Disc
    {
    digitalWrite(ledPin, LOW);
    tone(4, 1500,100);
    irmp_data.command = 00000;
    }

    if(irmp_data.command == 58395) //Power
    {
    digitalWrite(ledPin, LOW);
    tone(4, 1600,100);
    irmp_data.command = 00000;
    }
    }

    /* helper function: attachInterrupt wants void(), but irmp_ISR is uint8_t() */
    void timerinterrupt()
    {
    irmp_ISR();
    }

    static inline void led(int state)
    {
    #ifdef LED_PIN
    digitalWrite(LED_PIN, state);
    #endif
    }

    Login or register to post comments