Pages: [1]   Go Down
Author Topic: using newtone and rcswitch together  (Read 408 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 1
Posts: 46
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
i've some problems using the NewTone Library together with RCSwitch-Library (using interrupt on pin 2)

I can play sound several times correctly (always using the same function), and than it suddenly stopps playing any sound, while everything else is working fine.
Shure both libs use timers and this will harm the sound quality, but why isn't it working any more?
PLZ gibe advice what i'm doing wrong or if there's an workaround.

i'm still on arduino 1.0.1 and i'm using an arduino nano V3. The code isn't pretty..sorry for that.

MANY MANY thanks for your replys
Code:
#include <RemoteReceiver.h>
#include <RemoteTransmitter.h>
#include <NewRemoteReceiver.h>
#include <InterruptChain.h>
#include <SerialCommand.h>

#include <NewTone.h>

int *currentMelody;
int *currentMelodyDurations;
int currentMelodyPosition = 0;
int currentMelodyLength = 0;
int pace = 1150; // music speed

int melodyBeep[] = {NOTE_C5, NOTE_C6};
int noteDurationsBeep[] = {8, 16};
int melodyBeepLength = 2;

int melodyUp[] = {NOTE_C4, NOTE_D4, NOTE_G4};
int noteDurationsUp[] = {4, 8, 16};  
int melodyUpLength = 3;

int melodyDown[] = {NOTE_G4, NOTE_D4, NOTE_C4};
int noteDurationsDown[] = {4, 8, 16};  
int melodyDownLength = 3;

int melodyAlarm[] = {NOTE_C6, NOTE_G6, NOTE_C6, NOTE_G5, NOTE_C7, NOTE_C6};
int noteDurationsAlarm[] = {16, 16, 8, 8, 4, 8};  
int melodyAlarmLength = 6;

// pin for the piezzo buzzer
const int BuzzerPin = 3;
  
// set the pin where the RF transmitter is connected to
const int transmitterPin = 4;

//duration of a RF command
const unsigned int period = 326;

boolean playsound = false;
boolean playloop = false;

// amount of identical codes to receiver before trigger showCode function
int CodeRepetitions = 5;

SerialCommand SCmd;   // initialise the SerialCommand object

void setup() {
  // Initialise the serial port
  Serial.begin(9600);
  Serial.println("HomeSecurityRFbridge running on Arduino Nano V3.0");
  // Initialize receiver on interrupt 0 (= digital pin 2), calls the callback "showCode"
  // after CodeRepetitions (3) identical codes have been received in a row. (thus, keep the button pressed
  // for a moment)


  // And once more, interrupt -1 to indicate you will call the interrupt handler with InterruptChain

  // Interrupt -1 to indicate you will call the interrupt handler with InterruptChain
  RemoteReceiver::init(-1, 2, showOldCode);

  // Again, interrupt -1 to indicate you will call the interrupt handler with InterruptChain
  NewRemoteReceiver::init(-1, 2, showNewCode);


  // On interrupt, call the interrupt handlers of remote and sensor receivers
  InterruptChain::addInterruptCallback(0, RemoteReceiver::interruptHandler);
  InterruptChain::addInterruptCallback(0, NewRemoteReceiver::interruptHandler);


 // Serial.print("Setting up callbacks for SerialCommands...");
  // Setup callbacks for SerialCommand commands
  //Serial.print("hello [optional arg] (replys welcome string or RE:arg) ");
  SCmd.addCommand("hello",SayHello);     // Echos the string argument back
  //Serial.print("|P [arg1] [arg2] (processes two arg commands) ");
  SCmd.addCommand("P",process_command);  // Converts two arguments to integers and echos them back
  //Serial.print("|S [arg] (sends arg) ");
  SCmd.addCommand("S",process_switch_command);  // Converts two arguments to integers and echos them back
  SCmd.addCommand("BU",BeepUp);  // Converts two arguments to integers and echos them back
  SCmd.addCommand("BD",BeepDown);  // Converts two arguments to integers and echos them back
  SCmd.addCommand("STOP",StopAudio);  // Converts two arguments to integers and echos them back
  SCmd.addCommand("BEEP",Beep);  // Converts two arguments to integers and echos them back
  SCmd.addCommand("ALARM",Alarm);  // Converts two arguments to integers and echos them back
  SCmd.setDefaultHandler(unrecognized);  // Handler for command that isn't matched  (says "What?")
  //Serial.println("...ok");

}

void loop() {
  while (playsound && currentMelodyPosition<currentMelodyLength) {
      int duration = pace/currentMelodyDurations[currentMelodyPosition];//Adjust duration with the pace of music
      NewTone(BuzzerPin,currentMelody[currentMelodyPosition],duration);
      int time = millis() + duration*1.2;
      while (millis()<time) {
        SCmd.readSerial();     // We don't do much, just process serial commands
      }
      currentMelodyPosition++;
      if (playloop && currentMelodyPosition==currentMelodyLength) {
          currentMelodyPosition=0;
      }
  }
  SCmd.readSerial();     // We don't do much, just process serial commands
}


void BeepUp() {
  currentMelody = &melodyUp[0];
  currentMelodyDurations = &noteDurationsUp[0];
  currentMelodyLength = melodyUpLength;
  currentMelodyPosition = 0;
  playsound = true;
  playloop = false;
  //tone(BuzzerPin,NOTE_C5,1000/2);
}

void BeepDown() {
  currentMelody = &melodyDown[0];
  currentMelodyDurations = &noteDurationsDown[0];
  currentMelodyLength = melodyDownLength;
  currentMelodyPosition = 0;
  playsound = true;
  playloop = false;
//  tone(BuzzerPin,NOTE_C1,1000/2);
}

void Beep() {
  currentMelody = &melodyBeep[0];
  currentMelodyDurations = &noteDurationsBeep[0];
  currentMelodyLength = melodyBeepLength;
  currentMelodyPosition = 0;
  playsound = true;
  playloop = false;
//  tone(BuzzerPin,NOTE_C3,1000/2);
}

void Alarm() {
  currentMelody = &melodyAlarm[0];
  currentMelodyDurations = noteDurationsAlarm;
  currentMelodyLength = melodyAlarmLength;
  currentMelodyPosition = 0;
  playsound = true;
  playloop = true;
}

void StopAudio() {
  playsound = false;
  playloop = false;
  currentMelodyLength = 0;
  currentMelodyPosition = 0;
  noNewTone(BuzzerPin);
}


// shows the received code sent from an old-style remote switch
void showOldCode(unsigned long receivedCode, unsigned int period) {
  // Print the received code.
  Serial.print("@OC,C:");
  Serial.print(receivedCode);
  Serial.print(",P:");
  Serial.print(period);
  Serial.println("#");
}

// Shows the received code sent from an new-style remote switch
void showNewCode(NewRemoteCode receivedCode) {
  // Print the received code.
  Serial.print("@NC,A:");
  Serial.print(receivedCode.address);

  if (receivedCode.groupBit) {
    Serial.print(",G:");
  }
  else {
    Serial.print(",U");
    Serial.print(receivedCode.unit);
  }

  switch (receivedCode.switchType) {
  case 0:
    Serial.print(",S:0");
    break;
  case 1:
    Serial.print(",S:1");
    break;
  case 2:
    Serial.print(",D:");
    Serial.print(receivedCode.dimLevel);
    break;
  }

  Serial.print(",P:");
  Serial.print(receivedCode.period);
  Serial.println("#");
}




// process the HELLO command from USB
void SayHello()
{
  char *arg;  
  arg = SCmd.next();    // Get the next argument from the SerialCommand object buffer
  if (arg != NULL)      // As long as it existed, take it
  {
    Serial.print("RE:");
    Serial.println(arg);
  }
  else {
    Serial.println("HomeSecurityRFbridge running on Arduino Nano V3.0");
  }
}


// process the serial data from USB
void process_command()    
{
  long aNumber;  
  char *arg;

  //Serial.println("arduino is in process_command");
  arg = SCmd.next();
  if (arg != NULL)
  {
    aNumber=atoi(arg);    // Converts a char string to an integer
    //Serial.print("First argument was: ");
    //Serial.println(aNumber);
  }
  else {
    //Serial.println("No arguments");
  }

  arg = SCmd.next();
  if (arg != NULL)
  {
    aNumber=atol(arg);
    //Serial.print("Second argument was: ");
    //Serial.println(aNumber);
  }
  else {
    //Serial.println("No second argument");
  }

}


// process the switch command from USB
void process_switch_command()    
{
  unsigned long CodeToSend;  
  char *arg1;
  int PeriodToSend;  
  char *arg2;

  //Serial.println("arduino is in process_switch_command");
  arg1 = SCmd.next();
  if (arg1 != NULL)
  {
    CodeToSend=atol(arg1);    // Converts a char string to an integer
    //Serial.print("code to send: ");
    //Serial.println(CodeToSend);

    arg2 = SCmd.next();
    if (arg2 != NULL)
    {
      PeriodToSend=atol(arg2);
      //Serial.print("period: ");
      //Serial.println(PeriodToSend);
    }
    else {
      //Serial.println("using standard period");
      PeriodToSend = period;
    }
    send_switch(CodeToSend,PeriodToSend);
  }
  else {
    //Serial.println("No arguments -> no send!");
  }



}


// This gets set as the default handler, and gets called when no other command matches.
void unrecognized(const char *command) {
  Serial.println("What?");
}





void send_switch(unsigned long CodeToSend, unsigned int PeriodToSend)    
{
  // Disable the receiver; otherwise it might pick up the retransmit as well.
  RemoteReceiver::disable();

  // Need interrupts for delay()
  interrupts();

  // Retransmit the signal 8 times ( == 2^3) on pin 11. Note: no object was created!
  RemoteTransmitter::sendCode(transmitterPin, CodeToSend, PeriodToSend, 3);

  RemoteReceiver::enable();
}
« Last Edit: July 05, 2013, 02:25:11 am by infantilo » Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The symptoms suggest you're suffering from memory corruption. I don't know what any of those libraries do so it would be worth reviewing that library code for any unsafe practices, especially the SerialCommand library. Anything using malloc(), free(), the new operator or the String class would be an immediate concern. Have you measured how much free memory you have at runtime?
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Newbie
*
Karma: 1
Posts: 46
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hy PeterH,
MANY thanks for reply!  smiley
Thanks for the hint..i'll try to write my own serial command parser and see if this solves the problem.
I've just written an simple version ()before reading your reply) that only uses newtone and the serialCommand lib and i've expierience a similar problem.
Logged

Pages: [1]   Go Up
Jump to: