/* * My first Arduino program : LM (Rev.1 - 5-letter groups) * (Rev.2 - Speed control ) * (Rev.3 - Add '/' character + minor cleanup) */ // Constants - const boolean TEST = false; // Canned sentence (all letters and period) const boolean incNum = false; // Include numbers in 5-character groups // const int keyPressPin = 13; const int timeConstant = 850; // Experimentally determined const int minSpeed = 5; // Prevent possible divide error and extend max speed // Digital pins to read speed as binary number (Add minSpeed to value) const int d0 = 2; const int d1 = 3; const int d2 = 4; const int d3 = 5; const int d4 = 6; // Analog pin for speed potentiometer const int a0 = 0; // const String testString = "The quick brown fox jumped over the lazy dog's back."; // Variables - int sndSpeed = 9; // Will be revalued as read from Arduino int dotTime; // Valued in setSpeed() int dashTime; // = dotTime + dotTime + dotTime; int charTime; // = dashTime; int wordTime; // = dashTime + dashTime + dotTime; void setup() { // Runs once pinMode(keyPressPin, OUTPUT); pinMode(LED_BUILTIN, OUTPUT); // Visual indicator // pinMode(d0, INPUT); // Binary speed specification pinMode(d1, INPUT); pinMode(d2, INPUT); pinMode(d3, INPUT); pinMode(d4, INPUT); // digitalWrite(d0, LOW); // Not sure this does anything.. digitalWrite(d1, LOW); // Each pin will be grounded digitalWrite(d2, LOW); // through a 10K resistor if used digitalWrite(d3, LOW); digitalWrite(d4, LOW); // digitalWrite(keyPressPin, LOW); // key up digitalWrite(LED_BUILTIN, LOW); // LED off delay(2000); // Dark after upload randomSeed(analogRead(5)); // Initialize random number generator // (floating analog pin) } void loop() { // Runs repeatedly // Adjust speed analogReadSpeed(); setSpeed(); // if (TEST) { outputString(testString); delay(4000); // Between repetitions of test string } else outputString(fiveCharacterGroup() + " "); } String fiveCharacterGroup() { byte b; String s = ""; for (int i=0; i<5; i++) { if (incNum) { b = random(36); // Can be changed to < 36 for partial numbers if (b < 26) b = 65 + b; else b = 22 + b; } else b = 65 + random(26); s = s + (char) b; } return s; } void dot() { digitalWrite(LED_BUILTIN, HIGH); // Visual indicator on digitalWrite(keyPressPin, HIGH); // key down delay(dotTime); digitalWrite(LED_BUILTIN, LOW); // Visual indicator off digitalWrite(keyPressPin, LOW); // key up delay(dotTime); } void dash() { digitalWrite(LED_BUILTIN, HIGH); // Visual indicator on digitalWrite(keyPressPin, HIGH); // key down delay(dashTime); digitalWrite(LED_BUILTIN, LOW); // Visual indicator off digitalWrite(keyPressPin, LOW); // key up delay(dotTime); } void outputString(String str) { char c; int i; str.toLowerCase(); for (i = 0; i < str.length(); i ++) { c = str.charAt(i); if (c == ' ') delay(wordTime); else outputChar(c); } } void outputChar(char chr) { switch (chr) { case 'a' : dot(); dash(); break; case 'b' : dash(); dot(); dot(); dot(); break; case 'c' : dash(); dot(); dash(); dot(); break; case 'd' : dash(); dot(); dot(); break; case 'e' : dot(); break; case 'f' : dot(); dot(); dash(); dot(); break; case 'g' : dash(); dash(); dot(); break; case 'h' : dot(); dot(); dot(); dot(); break; case 'i' : dot(); dot(); break; case 'j' : dot(); dash(); dash(); dash(); break; case 'k' : dash(); dot(); dash(); break; case 'l' : dot(); dash(); dot(); dot();; break; case 'm' : dash(); dash(); break; case 'n' : dash(); dot(); break; case 'o' : dash(); dash(); dash(); break; case 'p' : dot(); dash(); dash(); dot(); break; case 'q' : dash(); dash(); dot(); dash(); break; case 'r' : dot(); dash(); dot(); break; case 's' : dot(); dot(); dot(); break; case 't' : dash(); break; case 'u' : dot(); dot(); dash(); break; case 'v' : dot(); dot(); dot(); dash(); break; case 'w' : dot(); dash(); dash(); break; case 'x' : dash(); dot(); dot(); dash(); break; case 'y' : dash(); dot(); dash(); dash(); break; case 'z' : dash(); dash(); dot(); dot(); break; case '0' : dash(); dash(); dash(); dash(); dash(); break; case '1' : dot(); dash(); dash(); dash(); dash(); break; case '2' : dot(); dot(); dash(); dash(); dash(); break; case '3' : dot(); dot(); dot(); dash(); dash(); break; case '4' : dot(); dot(); dot(); dot(); dash(); break; case '5' : dot(); dot(); dot(); dot(); dot(); break; case '6' : dash(); dot(); dot(); dot(); dot(); break; case '7' : dash(); dash(); dot(); dot(); dot(); break; case '8' : dash(); dash(); dash(); dot(); dot(); break; case '9' : dash(); dash(); dash(); dash(); dot(); break; case ',' : dash(); dash(); dot(); dot(); dash(); dash(); break; case '.' : dot(); dash(); dot(); dash(); dot(); dash(); break; case '?' : dot(); dot(); dash(); dash(); dot(); dot(); break; case '/' : dash(); dot(); dot(); dash(); dot(); default : dash(); dot(); dot(); dot(); dash(); } delay(charTime); } void readSpeed() { // As binary number (words per minute) // Could do as int, however, sndSpeed is global variable int sum = 0; if (digitalRead(d0) == HIGH) sum = 1; if (digitalRead(d1) == HIGH) sum = sum + 2; if (digitalRead(d2) == HIGH) sum = sum + 4; if (digitalRead(d3) == HIGH) sum = sum + 8; if (digitalRead(d4) == HIGH) sum = sum + 16; if (sum > 0) sndSpeed = sum + minSpeed; // words per minute } void analogReadSpeed() { // As voltage * 1024 / 5 sndSpeed = analogRead(a0) / 32 + minSpeed; // 0-1023 to w.p.m. } void setSpeed() { // After initialization, based on input from board dotTime = timeConstant / sndSpeed; dashTime = dotTime + dotTime + dotTime; charTime = dashTime; wordTime = dashTime + dashTime + dotTime; }