Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add ablity to adjust speed (specified by either WPM or dot-duration).…

… Add an example for changing speed.
  • Loading branch information...
commit 880dd7940a7db2b93671dfd6add33048f1269d81 1 parent 58a65b0
@markfickett authored
View
2  examples/concurrent/concurrent.pde
@@ -11,7 +11,7 @@
#include <morse.h>
#define PIN_STATUS 13
-#define PIN_SPEAKER 2
+#define PIN_SPEAKER 3
LEDMorseSender readySender(PIN_STATUS);
MorseSender *cqSender, *callsignSender;
View
51 examples/speeds/speeds.pde
@@ -0,0 +1,51 @@
+/**
+ * Send Morse messages at different speeds; also use a different pitch.
+ */
+
+#include <morse.h>
+
+#define PIN_STATUS 13
+#define PIN_SPEAKER 3
+
+// C above middle C
+#define FREQUENCY 523
+
+#define NUM_SPEEDS 5
+const float wpms[] = {5.0, 7.5, 10.0, 13.0, 15.0};
+const morseTiming_t durations[] = {34.29, 40.0, 48.0, 60.0, 80.0};
+
+LEDMorseSender slowerSender(PIN_STATUS, wpms[0]);
+SpeakerMorseSender fasterSender(PIN_SPEAKER, FREQUENCY, durations[0]);
+
+unsigned int speedIndex = 0;
+unsigned int lastChangeTime;
+
+void setup()
+{
+ slowerSender.setup();
+ fasterSender.setup();
+ slowerSender.setMessage(String("qst de w1aw "));
+ fasterSender.setMessage(String("what hath god wrought? "));
+ lastChangeTime = millis();
+}
+
+void loop()
+{
+ if (!slowerSender.continueSending())
+ {
+ slowerSender.startSending();
+ }
+ if (!fasterSender.continueSending())
+ {
+ fasterSender.startSending();
+ }
+
+ if((millis() - lastChangeTime) > 5000)
+ {
+ speedIndex++;
+ lastChangeTime = millis();
+ slowerSender.setWPM(wpms[speedIndex % NUM_SPEEDS]);
+ fasterSender.setSpeed(durations[speedIndex % NUM_SPEEDS]);
+ }
+}
+
View
24 morse.cpp
@@ -89,9 +89,24 @@ unsigned int MorseSender::fillTimings(char c)
void MorseSender::setOn() {};
void MorseSender::setOff() {};
-MorseSender::MorseSender(unsigned int outputPin): pin(outputPin) {}
+MorseSender::MorseSender(unsigned int outputPin, float wpm) :
+ pin(outputPin)
+{
+ setWPM(wpm);
+}
void MorseSender::setup() { pinMode(pin, OUTPUT); }
+
+void MorseSender::setWPM(float wpm)
+{
+ setSpeed((morseTiming_t)(1000.0*60.0/(max(1.0, wpm)*DITS_PER_WORD)));
+}
+
+void MorseSender::setSpeed(morseTiming_t duration)
+{
+ DIT = max(1, duration);
+ DAH = 3*DIT;
+}
void MorseSender::setMessage(const String newMessage)
{
@@ -161,13 +176,14 @@ void MorseSender::operator delete(void* ptr) { if (ptr) free(ptr); }
void SpeakerMorseSender::setOn() { tone(pin, frequency); }
void SpeakerMorseSender::setOff() { noTone(pin); }
SpeakerMorseSender::SpeakerMorseSender(
- int outputPin, unsigned int toneFrequency)
-: MorseSender(outputPin), frequency(toneFrequency) {};
+ int outputPin, unsigned int toneFrequency, float wpm)
+: MorseSender(outputPin, wpm), frequency(toneFrequency) {};
// LEDMorseSender
void LEDMorseSender::setOn() { digitalWrite(pin, HIGH); }
void LEDMorseSender::setOff() { digitalWrite(pin, LOW); }
-LEDMorseSender::LEDMorseSender(int outputPin) : MorseSender(outputPin) {};
+LEDMorseSender::LEDMorseSender(int outputPin, float wpm)
+ : MorseSender(outputPin, wpm) {};
View
26 morse.h
@@ -20,10 +20,9 @@
// Arduino language types
#include "WProgram.h"
-// define lengths
-#define UNIT 100
-#define DIT UNIT
-#define DAH 3*UNIT
+#define WPM_DEFAULT 12.0
+// PARIS WPM measurement: 50; CODEX WPM measurement: 60 (Wikipedia:Morse_code)
+#define DITS_PER_WORD 50
// Bitmasks are 1 for dah and 0 for dit, in left-to-right order;
// the sequence proper begins after the first 1 (a sentinel).
@@ -99,6 +98,7 @@ class MorseSender {
virtual void setOff();
private:
+ morseTiming_t DIT, DAH;
String message;
// on,off,...,wait,0 list, millis
@@ -129,7 +129,7 @@ class MorseSender {
/**
* Create a sender which will output to the given pin.
*/
- MorseSender(unsigned int outputPin);
+ MorseSender(unsigned int outputPin, float wpm=WPM_DEFAULT);
/**
* To be called during the Arduino setup(); set the pin as OUTPUT.
@@ -137,6 +137,15 @@ class MorseSender {
void setup();
/**
+ * Set the words per minute (based on PARIS timing).
+ */
+ void setWPM(float wpm);
+ /**
+ * Set the duration, in milliseconds, of a DIT.
+ */
+ void setSpeed(morseTiming_t duration);
+
+ /**
* Set the message to be sent.
* This halts any sending in progress.
*/
@@ -156,7 +165,7 @@ class MorseSender {
* Switch outputs on and off (and refill the internal timing buffer)
* as necessary to continue with the sending of the current message.
* This should be called every few milliseconds (at a significantly
- * smaller interval than UNIT) to produce a legible fist.
+ * smaller interval than a DIT) to produce a legible fist.
*
* @see startSending, which must be called first
* @return false if sending is complete, otherwise true (keep sending)
@@ -177,7 +186,8 @@ class SpeakerMorseSender: public MorseSender {
public:
// concert A = 440
// middle C = 261.626; higher octaves = 523.251, 1046.502
- SpeakerMorseSender(int outputPin, unsigned int toneFrequency=1046);
+ SpeakerMorseSender(int outputPin, unsigned int toneFrequency=1046,
+ float wpm=WPM_DEFAULT);
};
@@ -186,6 +196,6 @@ class LEDMorseSender: public MorseSender {
virtual void setOn();
virtual void setOff();
public:
- LEDMorseSender(int outputPin);
+ LEDMorseSender(int outputPin, float wpm=WPM_DEFAULT);
};
Please sign in to comment.
Something went wrong with that request. Please try again.