Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.56 KB | None | 0 0
  1. String input; //first lab
  2.  
  3. void setup() {
  4. Serial.begin(9600);
  5. }
  6.  
  7. void loop() {
  8. while(Serial.available()==0){}
  9. input=Serial.readString();
  10.  
  11.  
  12. int numsize=input.length();
  13.  
  14. char Str[numsize]; //array to store the digits of the input
  15. int i;
  16. for(i=0;i<numsize;i++){
  17. Str[i]=input.charAt(i);
  18. }
  19.  
  20. input.remove(0,1);
  21. Serial.println(input);
  22. }
  23. ////////////////////////////////////////////////////////////
  24.  
  25.  
  26.  
  27. String input; //the user input //second lab
  28.  
  29. void setup() {
  30. Serial.begin(9600);
  31. pinMode(13,OUTPUT);
  32.  
  33. }
  34.  
  35. void loop() {
  36.  
  37. while(Serial.available()==0){}
  38. input=Serial.readString();
  39. if(input.charAt(0)=='-'){
  40. input.remove(0,1); //remove sign
  41. }
  42.  
  43. int numsize=input.length();
  44.  
  45. char Str[numsize]; //array to store the digits of the input
  46. int i;
  47. for(i=0;i<numsize;i++){
  48. Str[i]=input.charAt(i);
  49. }
  50.  
  51. int f;
  52. for(f=0;f<=numsize;f++){
  53. int flag=1; //to check if palindrome
  54. int j;
  55. char sub[f]; //subarray of Str[]
  56.  
  57. for(j=0;j<sizeof(sub);j++){ //store the first f elements of Str[] in sub[]
  58. sub[j]=Str[j];
  59. }
  60.  
  61.  
  62.  
  63. for(j=0;j<sizeof(sub)/2;j++){ //check that sub[] is palindrome
  64. if(sub[j]!=sub[sizeof(sub)-j-1]){ flag=0;break; }
  65. }
  66.  
  67. Serial.print(flag);
  68. if(flag==0){
  69. digitalWrite(13,LOW);
  70. delay(1000);
  71. }
  72. else if(flag==1){
  73. digitalWrite(13,HIGH);
  74. delay(1000);
  75. digitalWrite(13,LOW);
  76. delay(1000);
  77. }
  78.  
  79.  
  80. }
  81.  
  82.  
  83. Serial.println();
  84.  
  85. }
  86. ///////////////////////////////////////////////////////
  87.  
  88.  
  89. String input; //the user input //third lab
  90.  
  91. void setup() {
  92.  
  93. pinMode(13,OUTPUT);
  94. Serial.begin(9600);
  95.  
  96. }
  97.  
  98. void loop() {
  99.  
  100.  
  101.  
  102. while(Serial.available()==0){}
  103. input=Serial.readString();
  104. char word[input.length()]; //array to store the characters of the input
  105. String morse;
  106. int i;
  107. for(i=0;i<input.length();i++){
  108. word[i]=input.charAt(i);
  109. if(isWhitespace(word[i])){
  110. morse+='w'; //'w' is used to separate between words
  111. }
  112. else{
  113. morse+=convertToMorse(word[i])+'/'; //create the morse code of the whole input, '/' is used to separate between letters
  114. }
  115. }
  116.  
  117.  
  118.  
  119. char morseChar[morse.length()]; //array to store the characters of morse
  120. for(i=0;i<morse.length();i++){
  121. morseChar[i]=morse.charAt(i);
  122. if(morseChar[i]=='.'){
  123. digitalWrite(13,HIGH);
  124. delay(300);
  125. digitalWrite(13,LOW);
  126. delay(300);
  127. }
  128. else if(morseChar[i]=='-'){
  129. digitalWrite(13,HIGH);
  130. delay(1000);
  131. digitalWrite(13,LOW);
  132. delay(300);
  133. }
  134. else if(morseChar[i]=='/'){
  135. digitalWrite(13,LOW);
  136. delay(1000);
  137. }
  138. else if(morseChar[i]=='w'){
  139. digitalWrite(13,LOW);
  140. delay(2100);
  141. }
  142. }
  143.  
  144.  
  145.  
  146. }
  147.  
  148.  
  149. String convertToMorse(char a){ //convert each character to its morse code
  150. switch(a){
  151. case 'A': return ".-"; break;
  152. case 'B': return "-..."; break;
  153. case 'C': return "-.-."; break;
  154. case 'D': return "-.."; break;
  155. case 'E': return"."; break;
  156. case 'F': return"..-."; break;
  157. case 'G': return"--."; break;
  158. case 'H': return"...."; break;
  159. case 'I': return".."; break;
  160. case 'J': return".---"; break;
  161. case 'K': return"-.-"; break;
  162. case 'L': return".-.."; break;
  163. case 'M': return"--"; break;
  164. case 'N': return"-."; break;
  165. case 'O': return"---"; break;
  166. case 'P': return".--."; break;
  167. case 'Q': return"--.-"; break;
  168. case 'R': return".-."; break;
  169. case 'S': return"..."; break;
  170. case 'T': return"-"; break;
  171. case 'U': return"..-"; break;
  172. case 'V': return"...-"; break;
  173. case 'W': return".--"; break;
  174. case 'X': return"-..-"; break;
  175. case 'Y': return"-.--"; break;
  176. case 'Z': return"--.."; break;
  177. case '0': return"-----"; break;
  178. case '1': return".----"; break;
  179. case '2': return"..---"; break;
  180. case '3': return"...--"; break;
  181. case '4': return"....-"; break;
  182. case '5': return"....."; break;
  183. case '6': return"-...."; break;
  184. case '7': return"--..."; break;
  185. case '8': return"---.."; break;
  186. case '9': return"----."; break;
  187. default: return;break;
  188. }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement