HamShield/examples/Morse/Morse.ino

129 lines
3.8 KiB
Arduino
Raw Normal View History

2018-08-12 02:39:31 +00:00
/* Hamshield
* Example: Morse Code Transceiver
*
2018-08-16 02:30:20 +00:00
* Serial to Morse transceiver. Sends characters from the Serial
2018-08-12 02:39:31 +00:00
* port over the air, and vice versa.
* Connect the HamShield to your Arduino. Screw the antenna
* into the HamShield RF jack. Connect the Arduino to wall
* power and then to your computer via USB. After uploading
* this program to your Arduino, open the Serial Monitor to
* monitor the status of the beacon. To test, set a HandyTalkie
* to 438MHz. You should hear the message " CALLSIGN HAMSHIELD"
* in morse code.
2019-06-01 22:25:56 +00:00
*
*
* Note: only upper case letters, numbers, and a few symbols
* are supported.
* Supported symbols: &/+(=:?";@`-._),!$
*
* If you're having trouble accurately decoding, you may want to
* tweak the min/max . and - times. You can also uncomment
* the Serial.print debug statements that can tell you when tones
* are being detected, how long they're detected for, and whether
* the tones are decoded as a . or -.
*
2018-08-12 02:39:31 +00:00
*/
#define DDS_REFCLK_DEFAULT 9600
#include <HamShield.h>
2019-03-15 17:43:15 +00:00
#define MIC_PIN 3
2018-08-12 02:39:31 +00:00
#define RESET_PIN A3
#define SWITCH_PIN 2
#define MORSE_FREQ 600
2019-05-11 22:32:13 +00:00
#define MORSE_DOT 150 // ms
2018-08-12 02:39:31 +00:00
// Note that all timing is defined in terms of MORSE_DOT relative durations
2018-10-22 02:27:24 +00:00
// You may want to tweak those timings below
2018-08-12 02:39:31 +00:00
HamShield radio;
// Run our start up things here
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 02:39:31 +00:00
// prep the switch
pinMode(SWITCH_PIN, INPUT_PULLUP);
// set up the reset control pin
2019-12-23 16:14:37 +00:00
// NOTE: HamShieldMini doesn't have a reset pin, so this has no effect
2018-08-12 02:39:31 +00:00
pinMode(RESET_PIN, OUTPUT);
digitalWrite(RESET_PIN, HIGH);
delay(5); // wait for device to come up
// Set up the serial port at 9600 Baud
Serial.begin(9600);
// Send a quick serial string
Serial.println("HamShield Morse Example Sketch");
Serial.print("Radio status: ");
int result = radio.testConnection();
Serial.println(result,DEC);
// Tell the HamShield to start up
radio.initialize();
// Set the transmit power level (0-8)
radio.setRfPower(0);
// Set the morse code characteristics
radio.setMorseFreq(MORSE_FREQ);
radio.setMorseDotMillis(MORSE_DOT);
radio.lookForTone(MORSE_FREQ);
2019-07-14 20:37:14 +00:00
radio.setupMorseRx();
// Configure the HamShield frequency
radio.frequency(432100); // 70cm calling frequency
2018-08-12 02:39:31 +00:00
radio.setModeReceive();
Serial.println("Radio Configured.");
}
void loop() {
2019-07-14 20:37:14 +00:00
char rx_char = radio.morseRxLoop();
if (rx_char != 0) {
Serial.print(rx_char);
2018-08-12 02:39:31 +00:00
}
// should we send anything
if (Serial.available()) {
Serial.println("checking channel");
// We'll wait up to 30 seconds for a clear channel, requiring that the channel is clear for 2 seconds before we transmit
if (radio.waitForChannel(30000,2000,-5)) {
// If we get here, the channel is clear.
2019-06-01 22:25:56 +00:00
Serial.println("sending");
2018-08-12 02:39:31 +00:00
// Start transmitting by putting the radio into transmit mode.
radio.setModeTransmit();
2019-06-01 22:25:56 +00:00
Serial.println("tx");
2018-08-12 02:39:31 +00:00
unsigned int MORSE_BUF_SIZE = 128;
char morse_buf[MORSE_BUF_SIZE];
unsigned int morse_idx;
morse_buf[morse_idx++] = ' '; // start with space to let PA come up
while (Serial.available() && morse_idx < MORSE_BUF_SIZE) {
morse_buf[morse_idx++] = Serial.read();
}
morse_buf[morse_idx] = '\0'; // null terminate
// Send a message out in morse code
radio.morseOut(morse_buf);
// We're done sending the message, set the radio back into recieve mode.
2019-07-14 20:37:14 +00:00
Serial.println("sent");
2018-08-12 02:39:31 +00:00
radio.setModeReceive();
2019-05-11 22:32:13 +00:00
radio.lookForTone(MORSE_FREQ);
2018-08-12 02:39:31 +00:00
} else {
// If we get here, the channel is busy. Let's also print out the RSSI.
Serial.print("The channel was busy. RSSI: ");
Serial.println(radio.readRSSI());
}
}
}