selective call system with musical notes.

Hi need some help, im trying to build a 5 tone generator for a selective call system, one I can use over a transmitter i.e. Ham/CB radio. The idea is to hit one button that activates a relay for 2 seconds within that 2 second it plays 5 musical notes over Ham/CB radio. Now at the other end the Ham/CB radio will receive this 5 musical notes and trigger a buzzer, 10 seconds after receiving the 5 musical notes this Ham/CB radio (if the correct ones) will transmit a different set of musical notes that will trigger the buzzer in the senders Ham/CB radio to confirm that sent musical notes was received by the Ham/CB radio. The buzzer in the Ham/CB radio will be reset when the microphone/key is activated.

I’ve modified this sketch it works im only posting this one so you can see what im trying to achieve.
If it works what’s the problem I need it to play fixed notes like on a keyboard without playing around with frequency numbers and alike.

  Push button play 5 tones within a 2 second delay.

// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
int Buzzer1 = 8;
int Pin = 9;                  // Relay connected to pin 9
int ledPin = 13;                // LED is connected to pin 13
// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the piezo as output:
  pinMode(Buzzer1, OUTPUT);  
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);     

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // buzzer will play a frequency between the two values (Buzzer1,300,150)
     digitalWrite(Pin, HIGH);    // turn Relay ON
    digitalWrite(ledPin, HIGH);    // turn LED ON
      delay(2000);  // wait for a 2 second
      digitalWrite(Pin, LOW);   // turn Relay OFF
      digitalWrite(ledPin, LOW);   // turn LED OFF
      delay(2000);  // wait for a 2 second


This is the code im working on again it works in a fashion but im having trouble working out how to make it play the notes when the relay is activated. at the moment when power is supplied to the Arduino it plays the notes an then sits there as it should, I press the button the relay is activated for 2 seconds the onboard LED lights to confirm but no musical notes. I’ve been trying to sort this for weeks. Help please .

  Push button play 5 musical notes within a 2 second delay.

 #include "pitches.h"

// notes in the melody: 
int Pin = 9;                      // Relay connected to pin 9. 
int PinLED = 13;                 // LED connected to pin 13. 
int switchPin = 2;              // switch is connected to pin 2.

int val;                       // variable for reading the pin status.

int melody[] = { NOTE_E7, NOTE_CS7 ,NOTE_B7, NOTE_G7, NOTE_A7}; 

// note durations: 4 = quarter note, 8 = eighth note, etc.
int noteDurations[] = {
  9, 9, 9, 9, 9 };

void setup() {
   pinMode(Pin, OUTPUT);         // Set the relay pin as output
  pinMode(switchPin, INPUT);    // Set the switch pin as input
  // iterate over the notes of the melody:{
  for (int thisNote = 0; thisNote < 5; thisNote++) { 

    // to calculate the note duration, take one second 
    // divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 600/noteDurations[thisNote];
    tone(8, melody[thisNote],noteDuration); 

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * 1.30;
    // stop the tone playing:

void loop() {
    val = digitalRead(switchPin);   // read input value and store it in val
  if (val == HIGH) {               // check if the button is pressed
    digitalWrite(Pin, HIGH);   // turn relay on
    digitalWrite(PinLED, HIGH);   // turn LED on
     delay(2000);  // wait for a 2 second

 if (val == LOW) {              // check if the button is not pressed
    digitalWrite(Pin, LOW);    // turn relay off
   digitalWrite(PinLED, LOW);   // turn LED off
     // no need to repeat the melody.

In your first sketch, the tone-playing code is in loop(), and it's called when the button's pressed. In the second sketch, the tone-playing code is in setup(), and it's called once at startup, but never called thereafter.

If your ultimate vision for this project can tolerate locking the program for the two-second delay, a solution would be to move the tone-playing code to a function, and call it from setup() for confirmation, and from loop() for operation. Otherwise, you might want to use the "blink-without-delay" technique, establishing the time for the next action - turning the tone on or off, and operating the relay - monitoring the current time in loop, and executing the proper action when the time hits that value.

There are some problems you don't seem to have considered.

  1. The tone that is generated is a pulse-width modulated square wave - not a sine wave. Transmitting a square wave over a radio will cause a lot of interference. You would have to filter the signal to remove harmonics before injecting it into a microphone.

  2. Existing tone generation systems rely on hardware within the radio to control the squelch. A sub-audible tone decoder only opens the squelch if the selected tone is received. In your scheme the squelch would have to be open all the time and everyone on the same frequency will hear the tones you are sending. Apart from being annoying, it might be illegal.

  3. How are you going to decode the tones at the receiving end? This is not a trivial process, especially when looking for a sequence of 5 tones.