HamShield/examples/CTCSS/CTCSS.ino

182 lines
4.5 KiB
Arduino
Raw Normal View History

2018-08-12 03:30:09 +00:00
/* Hamshield
* Example: CTCSS
* This is a simple example to demonstrate HamShield receive
* and transmit functionality using CTCSS. The HamShield will
* have audio output muted until it receives the correct
* sub-audible tone. It does this by polling a tone detection
* flag on the HamShield, but it's also possible to do this
* using interrupts if you connect GPIO0 from the HamShield
* to your Arduino (code for that not provided).
*
* Setup:
* Connect the HamShield to your Arduino. Screw the antenna
* into the HamShield RF jack. Plug a pair of headphones into
* the HamShield. Connect the Arduino to wall power and then
* to your computer via USB. Set the CTCSS tone that you
* want to use in the setup() function below.
* After uploading this program to your Arduino, open the
* Serial Monitor. Press the button on the HamShield to begin
* setup. The sketch then works exactly like the HandyTalkie
* example, with the exception that only valid CTCSS coded
* receptions are put out to the headset.
*/
#include <HamShield.h>
// create object for radio
HamShield radio;
#define LED_PIN 13
#define RSSI_REPORT_RATE_MS 5000
2019-03-15 17:43:15 +00:00
#define MIC_PIN 3
2018-08-12 03:30:09 +00:00
#define RESET_PIN A3
#define SWITCH_PIN 2
bool currently_tx;
uint32_t freq;
float ctcss_tone;
bool muted;
unsigned long rssi_timeout;
void setup() {
// NOTE: if not using PWM out, it should be held low to avoid tx noise
2019-03-15 17:43:15 +00:00
pinMode(MIC_PIN, OUTPUT);
digitalWrite(MIC_PIN, LOW);
2018-08-12 03:30:09 +00:00
// prep the switch
pinMode(SWITCH_PIN, INPUT_PULLUP);
// set up the reset control pin
pinMode(RESET_PIN, OUTPUT);
digitalWrite(RESET_PIN, LOW);
// initialize serial communication
Serial.begin(9600);
Serial.println("press the switch to begin...");
while (digitalRead(SWITCH_PIN));
// let the AU ot of reset
digitalWrite(RESET_PIN, HIGH);
delay(5); // wait for device to come up
Serial.println("beginning radio setup");
// verify connection
Serial.println("Testing device connections...");
2019-08-20 21:34:20 +00:00
Serial.println(radio.testConnection() ? "radio connection successful" : "radio connection failed");
2018-08-12 03:30:09 +00:00
// initialize device
2019-08-20 21:34:20 +00:00
Serial.println("Initializing radio device...");
2018-08-12 03:30:09 +00:00
radio.initialize(); // initializes automatically for UHF 12.5kHz channel
Serial.println("setting default Radio configuration");
// set frequency
Serial.println("changing frequency");
2019-07-14 20:37:14 +00:00
freq = 432100; // 70cm calling frequency
2018-08-12 03:30:09 +00:00
radio.frequency(freq);
// set to receive
radio.setModeReceive();
currently_tx = false;
Serial.print("config register is: ");
Serial.println(radio.readCtlReg());
Serial.println(radio.readRSSI());
2019-07-14 20:37:14 +00:00
// set up squelch
radio.setSQLoThresh(-80);
radio.setSQHiThresh(-70);
radio.setSQOn();
2018-08-12 03:30:09 +00:00
radio.setRfPower(0);
// CTCSS Setup code
2018-10-22 02:27:24 +00:00
ctcss_tone = 131.8;
2018-08-12 03:30:09 +00:00
radio.setCtcss(ctcss_tone);
radio.enableCtcss();
2018-08-16 02:30:20 +00:00
Serial.print("ctcss tone: ");
Serial.println(radio.getCtcssFreqHz());
2018-08-12 03:30:09 +00:00
// mute audio until we get a CTCSS tone
2018-08-18 02:25:38 +00:00
radio.setMute();
2018-08-12 03:30:09 +00:00
muted = true;
// configure Arduino LED for
pinMode(LED_PIN, OUTPUT);
rssi_timeout = 0;
}
void loop() {
// handle CTCSS tone detection
if (!currently_tx) {
// check for CTCSS tone
if (radio.getCtcssToneDetected()) {
if (muted) {
muted = false;
2018-08-18 02:25:38 +00:00
radio.setUnmute();
2018-08-12 03:30:09 +00:00
Serial.println("tone");
}
} else {
if (!muted) {
muted = true;
2018-08-18 02:25:38 +00:00
radio.setMute();
2018-08-12 03:30:09 +00:00
Serial.println("no tone");
}
}
}
// handle manual transmit
if (!digitalRead(SWITCH_PIN))
{
if (!currently_tx)
{
currently_tx = true;
// set to transmit
radio.setModeTransmit();
Serial.println("Tx");
2018-08-18 02:25:38 +00:00
radio.setUnmute(); // can't mute during transmit
muted = false;
2018-08-12 03:30:09 +00:00
}
} else if (currently_tx) {
radio.setModeReceive();
currently_tx = false;
Serial.println("Rx");
2018-08-18 02:25:38 +00:00
radio.setMute(); // default to mute during rx
muted = true;
2018-08-12 03:30:09 +00:00
}
// handle serial commands
if (Serial.available()) {
if (Serial.peek() == 'r') {
Serial.read();
digitalWrite(RESET_PIN, LOW);
delay(1000);
digitalWrite(RESET_PIN, HIGH);
radio.initialize(); // initializes automatically for UHF 12.5kHz channel
} else {
Serial.setTimeout(40);
freq = Serial.parseInt();
2019-09-08 02:43:11 +00:00
while (Serial.available()) Serial.read();
2018-08-12 03:30:09 +00:00
radio.frequency(freq);
Serial.print("set frequency: ");
Serial.println(freq);
}
}
// periodically read RSSI and print to screen
if (!currently_tx && (millis() - rssi_timeout) > RSSI_REPORT_RATE_MS)
{
Serial.println(radio.readRSSI());
rssi_timeout = millis();
}
2018-08-16 02:30:20 +00:00
}