Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Se toma como pin de pruebas al pin número 11
- int ledPin = 11;
- int LDRPin = A0;
- void setup() {
- // Se cambia el modo del pin a "Salida"
- Serial.begin(9600);
- while (!Serial) {
- ;
- }
- pinMode(ledPin, OUTPUT);
- pinMode(LDRPin, INPUT);
- }
- void loop() {
- String TextoAEscribir = Serial.readString();
- if (TextoAEscribir == NULL) {
- analogWrite(ledPin, 0);
- Read(LDRPin);
- } else {
- Write(TextoAEscribir, ledPin);
- }
- }
- void Read(int PinFotoReceptor) {
- int ValorLeido;
- int clampedValue;
- int vecesDeCeros = 0;
- String TextoLeido = "";
- while (true) {
- ValorLeido = analogRead(LDRPin);
- //Serial.println("Leyendo");
- //Serial.println("Valor Leído: "+ValorLeido);
- clampedValue = clampToBinary(ValorLeido);
- if (clampedValue == 0) {
- //Serial.println("Es un cero");
- vecesDeCeros += 1;
- TextoLeido = TextoLeido+clampedValue;
- if (vecesDeCeros == 8) {
- break;
- }
- } else if (clampedValue == 1) {
- TextoLeido = TextoLeido+clampedValue;
- vecesDeCeros = 0;
- //Serial.println("Es un uno");
- }
- delay(50);
- }
- Serial.println(TextoLeido);
- Serial.println(isHandShake(TextoLeido));
- //SepararHandShake(TextoLeido);
- TextoLeido= "";
- }
- double reMap(double value, double low1, double high1, double low2, double high2) {
- return low2 + (high2 - low2) * (value - low1) / (high1 - low1);
- }
- boolean isHandShake(String Text) {
- int VecesCorrectas = 0 ;
- boolean Success = false;
- if (Text.indexOf("01010101") != -1) {
- Success = true;
- }
- return Success;
- }
- void SepararHandShake(String BinaryText) {
- String currentPattern = "";
- for (int i = 0; i<BinaryText.length()-4; i++) {
- currentPattern = BinaryText.charAt(i)+BinaryText.charAt(i+1)+BinaryText.charAt(i+2)+BinaryText.charAt(i+3);
- if (currentPattern != "1010" || currentPattern != "0101") {
- Serial.print(BinaryText.charAt(i)+BinaryText.charAt(i+1)+BinaryText.charAt(i+2)+BinaryText.charAt(i+3));
- }
- }
- }
- void HandShake(int LedPin) {
- for (int i=0; i<20; i++){
- analogWrite(LedPin, 255);
- delay(50);
- analogWrite(LedPin, 0);
- delay(50);
- }
- for (int i=0; i<20; i++){
- analogWrite(LedPin, 255);
- delay(50);
- }
- }
- void Write(String text, int LedPin) {
- HandShake(LedPin);
- String Binary = convertStringToBinary(text);
- for (int i = 0; i<Binary.length(); i++) {
- if (Binary.charAt(i) == '1') {
- analogWrite(LedPin, 255);
- } else {
- analogWrite(LedPin, 0);
- }
- delay(50);
- }
- }
- int clampToBinary(double value) {
- double var = reMap(value, 300, 400, 0, 1);
- if (var < 0) {
- return 0;
- } else if (var > 0) {
- return 1;
- }
- }
- String convertStringToBinary(String Text){
- String Result;
- for(int i=0; i<Text.length(); i++){
- char myChar = Text.charAt(i);
- for(int i=7; i>=0; i--){
- byte bytes = bitRead(myChar,i);
- Result = Result+bytes;
- }
- }
- return Result;
- }
- String convertBinaryToString(String Text) {
- String Result;
- String currentData = "";
- int Counter = 0;
- for(int i=0;i<Text.length();i++){
- char currentChar = Text.charAt(i);
- currentData = currentData + currentChar;
- Counter = Counter + 1;
- if(Counter == 8){
- Counter = 0;
- Serial.println(currentData);
- currentData = "";
- }
- }
- Serial.println("--------");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement