Stap 4: Code: Arduino
de definitieve code kan hier gevonden worden (zorg ervoor dat BinaryTree en knooppunt klassen toevoegen aan MorseCode.ino bestand)
de code is onderverdeeld in 3 stappen:
1 - schakelen tussen lezen staat en de schrijven staat door middel van een drukknop
2 - (schrijven staat) tekst invoer via het toetsenbord en omzetten in een reeks puntjes en streepjes uitgedrukt als LED-licht
3-(lezing staat) volgen de LDR lichte invoerfilter, onderscheid maken tussen stip, streepje en de afstand tussen letters en woorden, zodra ik heb een wachtrij van mijn stip en dash sequentie die ik wil gebruiken een binaire boom te achterhalen van de vertegenwoordiging van de brief.
#include <Keypad.h>#include "BinaryTree.h" #include "Node.h" #include
const byte rows = 4; const byte columns = 4; const int LedPin = 4; int holdDelay = 500; //how long each hold will last int dotDelay = 1000; int n = 2; // how many letters we want each key to represent int state = 0; //used to indicate if hold occured and for how long, state =0 no hold ,state =1 short hold , state = 2 long hold char key = 0; String text = ""; String Array[26] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.." };
//we will definne the key map as on the key pad: * notice each letter is spaced by 3 which our n char keys[rows][columns] = { {'a', 'c', 'e', 'g'}, {'i', 'k', 'm', 'o'}, {'q', 's', 'u', 'w'}, {'y', ' ', '#', '#'} };
byte rowPins[rows] = {12, 11, 10, 9}; byte columnPins[columns] = {8, 7, 6, 5};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, columnPins, rows, columns);
const int ButtonPIN = 3; bool isReading = false;
QueueList words; bool sendData = false; bool lightOn = false; unsigned long elapsedTime = 0; unsigned long elapsedTimeOff = 0; unsigned long previousTime = 0; unsigned long previousTimeOff = 0; char data; BinaryTree* tree; void setup() { // put your setup code here, to run once: Serial.begin(9600); pinMode(ButtonPIN, INPUT); pinMode(LedPin, OUTPUT); digitalWrite(ButtonPIN, HIGH);
tree = NULL; tree = new BinaryTree(); }
void loop() { // put your main code here, to run repeatedly: if (digitalRead(ButtonPIN) == LOW) { isReading = !isReading; Serial.println("0"); delay(500); }
if (isReading) { char temp = keypad.getKey();
if ((int)keypad.getState() == PRESSED) {
if (temp != 0) { key = temp; } } if ((int)keypad.getState() == HOLD) { state++; state = constrain(state, 1, n - 1); delay(holdDelay); }
if ((int)keypad.getState() == RELEASED) {
if (key == '#') { textToLight(text); text = ""; Serial.println("#"); } else if (key == ' ') { text += " "; Serial.println(" "); } else {
key += state; int index = key - 97; text += Array[index]; Serial.println(key); } state = 0;
}
} else { int data = analogRead(A0); if ( data < 100 && !lightOn) {//the moment the light went On elapsedTimeOff = millis() - previousTimeOff; previousTime = millis(); lightOn = true; if ( elapsedTimeOff > (7 - 1) * dotDelay ) { words.push('*'); } else if (elapsedTimeOff > 3 * dotDelay) { words.push(' '); } else {int gegevens = analogRead(A0); als (gegevens < 100 & &! lightOn) {//the moment het licht ging op elapsedTimeOff = millis() - previousTimeOff; previousTime = millis(); lightOn = true; als (elapsedTimeOff > (7 - 1) * dotDelay) {words.push('*'); /*else if (elapsedTimeOff >(3-1)*dotDelay) { anders als (elapsedTimeOff > 3 * dotDelay) {words.push(' '); }*/ }
if (lightOn) {//while light is On
}*/ if (data > 70) {//the moment the light went off lightOn = false; previousTimeOff = millis(); elapsedTime = millis() - previousTime;
if (elapsedTime > (7 - 1)*dotDelay) { readData(); } else if (elapsedTime > (3 - 1)*dotDelay) { words.push('-');
} else { words.push('.'); } }
Als (VerstrekenTijd > (7 - 1) * dotDelay) {readData(); }
} else {words.push('.'); delay(100);
} void textToLight(String text) {
for (int i = 0; i < text.length(); i++) { if (text.charAt(i) == '.') { digitalWrite(LedPin, HIGH); delay(dotDelay); digitalWrite(LedPin, LOW); delay(dotDelay * 3);
} else if ( text.charAt(i) == '-') { digitalWrite(LedPin, HIGH); delay(dotDelay * 3); digitalWrite(LedPin, LOW); delay(dotDelay * 3); } else { delay(dotDelay * 3);
} }
}
void readData() { else if (text.charAt(i) == '-') {digitalWrite (LedPin, hoge); delay(dotDelay * 3); digitalWrite (LedPin, laag); delay(dotDelay * 3); Node* focus = tree->root; while (!words.isEmpty()) { char letter = words.pop(); if (letter == '.') { focus = focus->right;
} else if (letter == '-') { focus = focus->left;
} else if (letter == ' ') { char letter = focus->get_Key(); Serial.println(letter); focus = tree->root;
} else { char letter = focus->get_Key(); Serial.println(letter); focus = tree->root; Serial.println(" "); } } char letter = focus->get_Key(); Serial.println(letter);
}
#include "arduino.h"#include "Node.h" #include
class BinaryTree {
public: BinaryTree(); void initialize(); else {char brief = focus -> get_Key(); Serial.println(letter); focus = boom -> wortel; Serial.println(""); char letters[32] = {' ', 'e', 't', 'i', 'a', 'n', 'm', 's', 'u', 'r', 'w', 'd', 'k', 'g', 'o', 'h', 'v', 'f', ' ', 'l', ' ', 'p', 'j', 'b', 'x', 'c', 'y', 'z', 'q', ' ', ' ' }; QueueList words; Node* root;
};
BinaryTree klasse
BinaryTree::BinaryTree() {
root =NULL; root = new Node(letters[0]); initialize();
}
void BinaryTree::initialize() { QueueList nodes ; int counter = 0; nodes.push(root);
for (int i = 0; i < 15; i++) { Node* focus = nodes.pop(); focus->right =new Node(letters[i + 1 + counter]); focus->left =new Node(letters[i + 2 + counter]);
nodes.push(focus->right); nodes.push(focus->left);
counter++;
} }
#ifndef Node_h#define Node_h
#include "arduino.h" class Node; class Node {
public: Node(char _key); Node* right; Node* left; char get_Key(); private: char key; //Node child; };
Node::Node(char _key) { key = _key;}
char Node::get_Key() {
Knooppunt klasse
return key; }
#endif
publiek: knooppunt (char _key); Knooppunt * recht; Knooppunt * links; char get_Key(); privé: char sleutel; Knooppunt kind; using UnityEngine;using System.Collections; using UnityEngine.UI; using System.IO.Ports; public class Controller : MonoBehaviour {
public Sprite onSprite, offSprite; public Image readImage, writeImage; public Text lcd;
private SerialPort port = new SerialPort( + "COM11", 9600); private bool isReading = false; // Use this for initialization void Start () { port.Open(); port.ReadTimeout = 25; StartCoroutine(readString()); } // Update is called once per frame void Update () {
terug toets; if (!isReading) { readImage.sprite = onSprite; writeImage.sprite = offSprite;
}