Advertisement
Judge_Dredds_Smile

Bluetooth 6 clicks

Mar 9th, 2016
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.11 KB | None | 0 0
  1. #include <Wire.h>
  2. #include "rgb_lcd.h"
  3. #include <SoftwareSerial.h>
  4.  
  5. SoftwareSerial mySerial(10, 11);
  6. rgb_lcd lcd;
  7.  
  8. #define DOT false
  9. #define DASH true
  10.  
  11. #define MAX_CHAR_LENGTH 6
  12.  
  13. #define BUTTON A0
  14. #define LIGHTSENSOR A1
  15. #define pinLED 5
  16. #define BUZZER 4
  17.  
  18. #define PULSE_THRESHOLD 250
  19. #define LETTER_SEPARATION 600
  20. #define WORD_SEPARATION 8000
  21.  
  22. String readString;
  23.  
  24. const struct MorseTree *tree = NULL;
  25.  
  26. int ledPin = 13;
  27.  
  28. int colorR = 0;
  29. int colorG = 0;
  30. int colorB = 100;
  31.  
  32. String answerString = String("");
  33. String answer = "Yellow";
  34. int answerLength = answer.length();
  35.  
  36. int value;
  37. int button = -1;
  38. int pulseThreshold = 0;
  39. int letterSeparation = 0;
  40. int wordSeparation = 0;
  41.  
  42.  
  43.  
  44. void initMorse(int lbutton, int lpulseThreshold, int lletterSeparation, int
  45. lwordSeparation)
  46. {
  47. tree = generateMorseTree();
  48. button = lbutton;
  49. pulseThreshold = lpulseThreshold;
  50. letterSeparation = lletterSeparation;
  51. wordSeparation = lwordSeparation;
  52. }
  53.  
  54.  
  55. struct MorseTree
  56. {
  57. // '-' unless this is a leaf node
  58. char character;
  59. struct MorseTree *dotChild;
  60. struct MorseTree *dashChild;
  61. };
  62.  
  63. struct MorseData
  64. {
  65. char character;
  66. bool code[MAX_CHAR_LENGTH];
  67. byte codeLength;
  68. };
  69.  
  70. void initMorseTree(struct MorseTree *tree)
  71. {
  72. tree -> character = '-';
  73. tree -> dotChild = NULL;
  74. tree -> dashChild = NULL;
  75. }
  76.  
  77. struct MorseTree *newMorseTree()
  78. {
  79. struct MorseTree *tree = (struct MorseTree *) malloc(sizeof(struct
  80. MorseTree));
  81. initMorseTree(tree);
  82. return tree;
  83. }
  84.  
  85. void addTreeMember(struct MorseTree *tree, struct MorseData &data)
  86. {
  87. struct MorseTree *current = tree;
  88. for (byte i = 0; i < data.codeLength; i++)
  89. {
  90. boolean currentSymbol = data.code[i];
  91. if (currentSymbol == DOT)
  92. {
  93. if (current -> dotChild == NULL)
  94. current -> dotChild = newMorseTree();
  95. current = current -> dotChild;
  96. }
  97. else
  98. {
  99. if (current -> dashChild == NULL)
  100. current -> dashChild = newMorseTree();
  101. current = current -> dashChild;
  102. }
  103. }
  104.  
  105. // now current must be a leaf node
  106. current -> character = data.character;
  107. }
  108.  
  109. void addTreeMembers(struct MorseTree *tree, struct MorseData data[],
  110. byte dataLength)
  111. {
  112. for (byte i = 0; i < dataLength; i++)
  113. addTreeMember(tree, data[i]);
  114. }
  115.  
  116. void addAlphabet(struct MorseTree *tree)
  117. {
  118. struct MorseData data[] = {
  119. {'A', {DOT, DASH}, 2},
  120. {'B', {DASH, DOT, DOT, DOT}, 4},
  121. {'C', {DASH, DOT, DASH, DOT}, 4},
  122. {'D', {DASH, DOT, DOT}, 3},
  123. {'E', {DOT}, 1},
  124. {'F', {DOT, DOT, DASH, DOT}, 4},
  125. {'G', {DASH, DASH, DOT}, 3},
  126. {'H', {DOT, DOT, DOT, DOT}, 4},
  127. {'I', {DOT, DOT}, 2},
  128. {'J', {DOT, DASH, DASH, DASH}, 4},
  129. {'K', {DASH, DOT, DASH}, 3},
  130. {'L', {DOT, DASH, DOT, DOT}, 4},
  131. {'M', {DASH, DASH}, 2},
  132. {'N', {DASH, DOT}, 2},
  133. {'O', {DASH, DASH, DASH}, 3},
  134. {'P', {DOT, DASH, DASH, DOT}, 4},
  135. {'Q', {DASH, DASH, DOT, DASH}, 4},
  136. {'R', {DOT, DASH, DOT}, 3},
  137. {'S', {DOT, DOT, DOT}, 3},
  138. {'T', {DASH}, 1},
  139. {'U', {DOT, DOT, DASH}, 3},
  140. {'V', {DOT, DOT, DOT, DASH}, 4},
  141. {'W', {DOT, DASH, DASH}, 3},
  142. {'X', {DASH, DOT, DOT, DASH}, 4},
  143. {'Y', {DASH, DOT, DASH, DASH}, 4},
  144. {'Z', {DASH, DASH, DOT, DOT}, 4},
  145. };
  146.  
  147. addTreeMembers(tree, data, 26);
  148. }
  149.  
  150. void addNumbers(struct MorseTree *tree)
  151. {
  152. struct MorseData data[] = {
  153. {'1', {DOT, DASH, DASH, DASH, DASH}, 5},
  154. {'2', {DOT, DOT, DASH, DASH, DASH}, 5},
  155. {'3', {DOT, DOT, DOT, DASH, DASH}, 5},
  156. {'4', {DOT, DOT, DOT, DOT, DASH}, 5},
  157. {'5', {DOT, DOT, DOT, DOT, DOT}, 5},
  158. {'6', {DASH, DOT, DOT, DOT, DOT}, 5},
  159. {'7', {DASH, DASH, DOT, DOT, DOT}, 5},
  160. {'8', {DASH, DASH, DASH, DOT, DOT}, 5},
  161. {'9', {DASH, DASH, DASH, DASH, DOT}, 5},
  162. {'0', {DASH, DASH, DASH, DASH, DASH}, 5},
  163. };
  164.  
  165. addTreeMembers(tree, data, 10);
  166. }
  167.  
  168. void addPunctuation(struct MorseTree *tree)
  169. {
  170. struct MorseData data[] = {
  171. {'.', {DOT, DASH, DOT, DASH, DOT, DASH}, 6},
  172. {',', {DASH, DASH, DOT, DOT, DASH, DASH}, 6},
  173. {'?', {DOT, DOT, DASH, DASH, DOT, DOT}, 6},
  174. };
  175.  
  176. addTreeMembers(tree, data, 3);
  177. }
  178.  
  179. struct MorseTree *generateMorseTree()
  180. {
  181.  
  182. struct MorseTree *tree = newMorseTree();
  183. initMorseTree(tree);
  184.  
  185. addAlphabet(tree);
  186. addNumbers(tree);
  187. addPunctuation(tree);
  188.  
  189. return tree;
  190. }
  191.  
  192.  
  193. void waitFor(int pin, int state)
  194. {
  195. do
  196. {
  197. // spin until the pin reads the given state
  198. while (digitalRead(pin) != state) { }
  199. // delay, to verify the reading
  200. delay(5);
  201. // continue if the reading has changed
  202. } while (digitalRead(pin) != state);
  203. }
  204.  
  205. boolean getNextSymbol()
  206. {
  207.  
  208. waitFor(button, HIGH);
  209. digitalWrite(pinLED, HIGH);
  210. digitalWrite(BUZZER, HIGH);
  211. unsigned long start = millis();
  212.  
  213. waitFor(button, LOW);
  214. digitalWrite(pinLED, LOW);
  215. digitalWrite(BUZZER, LOW);
  216. unsigned long end = millis();
  217.  
  218. unsigned long pulseLength = end - start;
  219.  
  220.  
  221. // Serial.println(pulseLength);
  222. if (pulseLength > pulseThreshold)
  223. return DASH;
  224. else
  225. return DOT;
  226. }
  227.  
  228. boolean shouldTimeOut()
  229. {
  230. unsigned long start = millis();
  231. while (digitalRead(BUTTON) == LOW)
  232. {
  233. if (millis() - start > letterSeparation)
  234. return true;
  235. }
  236.  
  237. return false;
  238. }
  239.  
  240. boolean shouldBeSpace()
  241. {
  242. unsigned long start = millis();
  243. while (digitalRead(BUTTON) == LOW)
  244. {
  245. if (millis() - start > wordSeparation)
  246. return true;
  247. }
  248.  
  249. return false;
  250. }
  251.  
  252. char getNextChar()
  253. {
  254. static boolean lastCharWasSpace = false;
  255.  
  256. const struct MorseTree *current = tree;
  257.  
  258. byte symbolCount = 0;
  259.  
  260. if (!lastCharWasSpace && shouldBeSpace())
  261. {
  262. lastCharWasSpace = true;
  263. return ' ';
  264. }
  265.  
  266. lastCharWasSpace = false;
  267.  
  268. while (true)
  269. {
  270. symbolCount++;
  271. boolean currentSymbol = getNextSymbol();
  272. // Serial.println(currentSymbol == DOT ? "DOT" : "DASH");
  273. if (currentSymbol == DOT)
  274. current = current -> dotChild;
  275. else
  276. current = current -> dashChild;
  277.  
  278. if (current == NULL)
  279. return '-';
  280.  
  281. if (shouldTimeOut() || symbolCount >= MAX_CHAR_LENGTH)
  282. return current -> character;
  283. }
  284.  
  285. // should never get here
  286. return '!';
  287. }
  288.  
  289. void lcdReset()
  290. {
  291. delay(1000);
  292. answerString.remove(0, 10);
  293. lcd.clear();
  294. startUp();
  295. }
  296.  
  297. void startUp()
  298. {
  299.  
  300. colorG = 0;
  301. colorB = 100;
  302. colorR = 0;
  303. lcd.setRGB(colorR, colorG, colorB);
  304. lcd.print("Yellow");
  305. lcd.setCursor(0, 1);
  306.  
  307. }
  308.  
  309.  
  310. void setup()
  311. {
  312. pinMode(BUTTON, INPUT);
  313. pinMode(LIGHTSENSOR, INPUT);
  314. pinMode(pinLED, OUTPUT);
  315. pinMode(ledPin, OUTPUT);
  316.  
  317. value = analogRead(LIGHTSENSOR);
  318.  
  319. initMorse(BUTTON, PULSE_THRESHOLD, LETTER_SEPARATION, WORD_SEPARATION);
  320.  
  321. Serial.begin(57600);
  322. while (!Serial) {
  323. ; // wait for serial port to connect. Needed for native USB port only
  324. }
  325.  
  326.  
  327. mySerial.begin(9600);
  328.  
  329. lcd.begin(16, 2);
  330. startUp();
  331.  
  332.  
  333. }
  334.  
  335. void loop()
  336. { while ( mySerial.available() )
  337. { // While there is data in the buffer
  338. //delay( 3 );
  339. char cc = mySerial.read();
  340. readString += cc; // build the string - β€œon” or β€œoff”
  341. }
  342. if ( readString.length() >0 )
  343. {
  344. mySerial.println( readString );
  345. if ( readString == "on" )
  346. {
  347. digitalWrite( ledPin, HIGH) ;
  348. }
  349. if ( readString == "off" )
  350. {
  351. digitalWrite( ledPin, LOW );
  352. }
  353. readString="";
  354. }
  355.  
  356. value = analogRead(LIGHTSENSOR);
  357. char c = getNextChar();
  358.  
  359.  
  360.  
  361. if(value<1)
  362. {
  363. lcd.clear();
  364. answerString.remove(0, 10);
  365. startUp();
  366. }
  367. else
  368. {
  369. answerString.concat(c);
  370. Serial.print(answerString);
  371. lcd.print(c);
  372.  
  373. }
  374.  
  375. if(answerString == answer)
  376. {
  377. delay(1000);
  378. lcd.clear();
  379. colorG = 100;
  380. colorB = 0;
  381. colorR = 0;
  382. lcd.setRGB(colorR, colorG, colorB);
  383. lcd.print("Correct!");
  384. delay(1000);
  385. answerString.remove(0, 10);
  386. lcdReset();
  387.  
  388. }
  389. else if(answerString.length() > answerLength)
  390. {
  391. delay(1000);
  392. lcd.clear();
  393. colorR = 100;
  394. colorB = 0;
  395. colorG = 0;
  396. lcd.setRGB(colorR, colorG, colorB);
  397. lcd.print("Incorrect!");
  398. lcdReset();
  399. }
  400.  
  401.  
  402.  
  403. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement