Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. // Se toma como pin de pruebas al pin número 11
  2. int ledPin = 11;
  3. int LDRPin = A0;
  4.  
  5. void setup() {
  6. // Se cambia el modo del pin a "Salida"
  7. Serial.begin(9600);
  8. while (!Serial) {
  9. ;
  10. }
  11. pinMode(ledPin, OUTPUT);
  12. pinMode(LDRPin, INPUT);
  13. }
  14.  
  15. void loop() {
  16. String TextoAEscribir = Serial.readString();
  17. if (TextoAEscribir == NULL) {
  18. analogWrite(ledPin, 0);
  19. Read(LDRPin);
  20. } else {
  21. Write(TextoAEscribir, ledPin);
  22. }
  23.  
  24. }
  25. void Read(int PinFotoReceptor) {
  26. int ValorLeido;
  27. int clampedValue;
  28. int vecesDeCeros = 0;
  29. String TextoLeido = "";
  30. while (true) {
  31. ValorLeido = analogRead(LDRPin);
  32. //Serial.println("Leyendo");
  33. //Serial.println("Valor Leído: "+ValorLeido);
  34. clampedValue = clampToBinary(ValorLeido);
  35. if (clampedValue == 0) {
  36. //Serial.println("Es un cero");
  37. vecesDeCeros += 1;
  38. TextoLeido = TextoLeido+clampedValue;
  39. if (vecesDeCeros == 8) {
  40. break;
  41. }
  42. } else if (clampedValue == 1) {
  43. TextoLeido = TextoLeido+clampedValue;
  44. vecesDeCeros = 0;
  45. //Serial.println("Es un uno");
  46. }
  47. delay(50);
  48. }
  49. Serial.println(TextoLeido);
  50. Serial.println(isHandShake(TextoLeido));
  51. SepararHandShake(TextoLeido);
  52. TextoLeido= "";
  53. }
  54. double reMap(double value, double low1, double high1, double low2, double high2) {
  55. return low2 + (high2 - low2) * (value - low1) / (high1 - low1);
  56. }
  57. boolean isHandShake(String Text) {
  58. int VecesCorrectas = 0 ;
  59. boolean Success = false;
  60. if (Text.indexOf("01010101") != -1) {
  61. Success = true;
  62. }
  63. return Success;
  64. }
  65. void SepararHandShake(String BinaryText) {
  66. String currentPattern = "";
  67. for (int i = 0; i<BinaryText.length()-4; i++) {
  68. currentPattern = BinaryText.charAt(i)+BinaryText.charAt(i+1)+BinaryText.charAt(i+2)+BinaryText.charAt(i+3);
  69. if (currentPattern != "1010" || currentPattern != "0101") {
  70. Serial.print(BinaryText.charAt(i)+BinaryText.charAt(i+1)+BinaryText.charAt(i+2)+BinaryText.charAt(i+3));
  71. }
  72. }
  73. }
  74. void HandShake(int LedPin) {
  75. for (int i=0; i<20; i++){
  76. analogWrite(LedPin, 255);
  77. delay(50);
  78. analogWrite(LedPin, 0);
  79. delay(50);
  80. }
  81.  
  82. }
  83. void Write(String text, int LedPin) {
  84. HandShake(LedPin);
  85. String Binary = convertStringToBinary(text);
  86. for (int i = 0; i<Binary.length(); i++) {
  87. if (Binary.charAt(i) == '1') {
  88. analogWrite(LedPin, 255);
  89. } else {
  90. analogWrite(LedPin, 0);
  91. }
  92. delay(50);
  93. }
  94. }
  95.  
  96. int clampToBinary(double value) {
  97. double var = reMap(value, 300, 400, 0, 1);
  98. if (var < 0) {
  99. return 0;
  100. } else if (var > 0) {
  101. return 1;
  102. }
  103. }
  104. String convertStringToBinary(String Text){
  105. String Result;
  106. for(int i=0; i<Text.length(); i++){
  107. char myChar = Text.charAt(i);
  108. for(int i=7; i>=0; i--){
  109. byte bytes = bitRead(myChar,i);
  110. Result = Result+bytes;
  111. }
  112. }
  113. return Result;
  114. }
  115. String convertBinaryToString(String Text) {
  116. String Result;
  117. String currentData = "";
  118. int Counter = 0;
  119. for(int i=0;i<Text.length();i++){
  120. char currentChar = Text.charAt(i);
  121. currentData = currentData + currentChar;
  122. Counter = Counter + 1;
  123. if(Counter == 8){
  124. Counter = 0;
  125. Serial.println(currentData);
  126. currentData = "";
  127. }
  128. }
  129. Serial.println("--------");
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement