Advertisement
Khadija_Assem

Untitled

Dec 13th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. #include <Arduino.h>
  2. #include <Wire.h>
  3. #include <SoftwareSerial.h>
  4.  
  5. int ledPin = 13;
  6.  
  7. //For letters
  8. char* letters[] = {
  9. ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", // A-I
  10. ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", // J-R
  11. "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.." // S-Z
  12. };
  13.  
  14. //For Numbers
  15. char* numbers[] = {
  16. "-----", ".----", "..---", "...--", "....-", ".....",
  17. "-....", "--...", "---..", "----."
  18. };
  19. int dotDelay = 200;
  20.  
  21. void setup() {
  22. // put your setup code here, to run once:
  23.  
  24. pinMode(ledPin, OUTPUT);
  25. Serial.begin(9600);
  26.  
  27. }
  28.  
  29. void loop() {
  30. // put your main code here, to run repeatedly:
  31. char ch;
  32. if (Serial.available()){
  33. ch = Serial.read(); // read a single letter if (ch >= 'a' && ch <= 'z')
  34. if (ch >= 'a' && ch <= 'z')
  35. {
  36. flashSequence(letters[ch - 'a']);
  37. }
  38. else if (ch >= 'A' && ch <= 'Z') {
  39. flashSequence(letters[ch - 'A']); }
  40. else if (ch >= '0' && ch <= '9') {
  41. flashSequence(numbers[ch - '0']); }
  42. else if (ch == ' ') {
  43. delay(dotDelay * 4);
  44. }
  45. }
  46.  
  47. }
  48.  
  49. void flashSequence(char* sequence) {
  50. int i = 0;
  51. while (sequence[i] != NULL) {
  52. flashDotOrDash(sequence[i]);
  53. i++;
  54. }
  55. delay(dotDelay * 3);
  56. }
  57.  
  58.  
  59. void flashDotOrDash(char dotOrDash) {
  60. digitalWrite(ledPin, HIGH);
  61. if (dotOrDash == '.'){
  62. delay(dotDelay);
  63. }
  64. else { // must be a -
  65. delay(dotDelay * 3); }
  66. digitalWrite(ledPin, LOW); delay(dotDelay);
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement