Étape 4: Code : Arduino
le code final peut être trouvé ici (veillez à ajouter des classes BinaryTree et nœud au fichier MorseCode.ino)
le code est divisé en 3 grandes étapes :
1 - basculer entre l’état de lecture et d’écriture état grâce à un bouton poussoir
2 - (écriture état) prendre une saisie de texte à partir du tableau et convertir une séquence de points et de tirets exprimées en lumière LED
3-(état de la lecture) surveiller la LDR pour entrée de lumière, faire la distinction entre point, tiret et l’espacement entre les lettres et les mots, une fois que j’ai une file d’attente de ma séquence de dot et tableau de bord que je veux utiliser un arbre binaire, à savoir la représentation de la lettre.
#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 {données int = analogRead(A0) ; if (données < 100 & &! lightOn) {//the moment la lumière passait elapsedTimeOff = millis() - previousTimeOff ; previousTime = millis() ; lightOn = true ; if (elapsedTimeOff > (7 - 1) * dotDelay) {words.push('*') ; /*else if (elapsedTimeOff >(3-1)*dotDelay) { ElseIf (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('.'); } }
Si (elapsedTime > (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() { ElseIf (text.charAt(i) == '-') {digitalWrite (LedPin, HIGH); delay(dotDelay * 3) ; digitalWrite (LedPin, LOW); 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 {lettre char = focus -> get_Key() ; Serial.println(letter) ; mise au point = arbre -> racine ; 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;
};
Classe BinaryTree
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() {
Classe de nœud
return key; }
#endif
public : nœud (char _key) ; Noeud * droite ; Noeud * gauche ; char get_Key() ; privé : clé de char ; Enfant du nœud ; 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 () {
retourner la clé ; if (!isReading) { readImage.sprite = onSprite; writeImage.sprite = offSprite;
}