Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 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. for (int i=0; i<20; i++){
  82. analogWrite(LedPin, 255);
  83. delay(50);
  84. }
  85. }
  86. void Write(String text, int LedPin) {
  87. HandShake(LedPin);
  88. String Binary = convertStringToBinary(text);
  89. for (int i = 0; i<Binary.length(); i++) {
  90. if (Binary.charAt(i) == '1') {
  91. analogWrite(LedPin, 255);
  92. } else {
  93. analogWrite(LedPin, 0);
  94. }
  95. delay(50);
  96. }
  97. }
  98.  
  99. int clampToBinary(double value) {
  100. double var = reMap(value, 300, 400, 0, 1);
  101. if (var < 0) {
  102. return 0;
  103. } else if (var > 0) {
  104. return 1;
  105. }
  106. }
  107. String convertStringToBinary(String Text){
  108. String Result;
  109. for(int i=0; i<Text.length(); i++){
  110. char myChar = Text.charAt(i);
  111. for(int i=7; i>=0; i--){
  112. byte bytes = bitRead(myChar,i);
  113. Result = Result+bytes;
  114. }
  115. }
  116. return Result;
  117. }
  118. String convertBinaryToString(String Text) {
  119. String Result;
  120. String currentData = "";
  121. int Counter = 0;
  122. for(int i=0;i<Text.length();i++){
  123. char currentChar = Text.charAt(i);
  124. currentData = currentData + currentChar;
  125. Counter = Counter + 1;
  126. if(Counter == 8){
  127. Counter = 0;
  128. Serial.println(currentData);
  129. currentData = "";
  130. }
  131. }
  132. Serial.println("--------");
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement