For Loops and String in a weird communication protocol

Hi, I’m trying to make two Arduinos communicate without the serial port and using a relay for transmit data.

I’m receiving some bits for pin8 and a want to put the bits together in bytes.

These ones are the bytes that I’m receiving

01001000

01100101

01101100

01101100

01101111

00100000

01010111

01101111

01110010

01101100

01100100

they come one bit at time

And this is the code that I made for the receiver, but it doesn’t work.

The Arduino just pint “ □□□□□□□□” in the serial and never stops

#define rxPin 8
int bitTime=40;
int Bit;
char Byte[8]="________";


void setup() {
  Serial.begin(9600);
  pinMode(rxPin, INPUT);
  
}

void loop() {
  while(rxPin==0){digitalWrite(1,1);}
  
  for(int c=7; c>=0; c--){ 
    ler();
    Byte[c] = Bit;
    
    }
    Serial.print(Byte);
}

void ler(){
  if (digitalRead(rxPin) == 1){ //bit 1
    delay(bitTime/2);
    if (digitalRead(rxPin) == 0){
      Bit = "1";
      delay(bitTime/2);
    }
    else{return;}
  }
    
  if (digitalRead(rxPin) == 0){
    delay(bitTime/2);
    if (digitalRead(rxPin) == 1){
      Bit = "0";
      delay(bitTime/2);
    } 
  }
  else{return;} 
}

Are you have any suggestion form how I can improve it?

your description doesn't say what you really want, nor how your circuits look like or why you want to reinvent the wheel and set up a curious communication.

but I can ask you

while(rxPin==0){digitalWrite(1,1);}

what do you expect that this line should do?
rxPin was defined as pin 8 ... so it will never become 0.
digitalWrite(1 ... ... will influence the pins used for Serial communication. This makes no sense for me.

Serial.print(Byte);

try to write in binary:

Serial.print(Byte, BIN);

What does Serial.println(Byte, BIN); produce?

Actually... on closer inspection, as per @noiasca's comments... your code doesn't really make any sense. Your timing logic is quite bizarre.

It would be helpful to know how the data is being sent from the other Arduino, then maybe we could figure out the best way to read it.

conventional asynchronous (without a clock) serial receivers use start/stop bit to guarantee and change in state indicating the start of transmission of a limited # of bits. the stop bit value (e.g. 1) is typically the opposite of the start bit.

the 2nd thing to consider is that when the start bit is recognizes, reading the first bit, the start bit, requires a delay of half a bit period to read the bit in its' center, but all subsequent bit read require a full bit period delay.

consider
the long BitPeriod allows me to test by toggling a button switch


const byte PinRx  = A1;

const unsigned long BitPeriod = 800;

enum { Off = HIGH, On = LOW };

// -----------------------------------------------------------------------------
byte
receive (void)
{
    byte rxData;

    while (HIGH == digitalRead (PinRx))
        ;

    delay (BitPeriod / 2);

    for (int n = 0; n < 9; n++)  {
        byte bit = digitalRead (PinRx);
        rxData = rxData << 1 | bit;

        digitalWrite (LED_BUILTIN, On);
        delay (100);
        digitalWrite (LED_BUILTIN, Off);
        delay (BitPeriod - 100);
    }

    return rxData;
}

// -----------------------------------------------------------------------------
void
loop (void)
{
    Serial.println (receive(), HEX);
}

// -----------------------------------------------------------------------------
void
setup (void)
{
    Serial.begin (9600);

    pinMode (PinRx,       INPUT_PULLUP);
    pinMode (LED_BUILTIN, OUTPUT);
    digitalWrite (LED_BUILTIN, Off);
}

Be sure to set your warning level to "All" in Preferences. That will get you a number of helpful warnings showing places where your code may not be doing what you want.

4:16: warning: initializer-string for array of chars is too long [-fpermissive]
 char Byte[8] = "________";
                ^~~~~~~~~~

In function 'void ler()':
37:13: warning: invalid conversion from 'const char*' to 'int' [-fpermissive]
       Bit = "1";
             ^~~

51:13: warning: invalid conversion from 'const char*' to 'int' [-fpermissive]
       Bit = "0";
             ^~~

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.