Pcomp video notes

Asynchronous serial communication

  • Devices keep time independently, but transmit/receive at the same rate
  • Devices need to agree on:
    1. rate at which data is sent and read
    2. voltage levels (representing 1 or 0 bit)
    3. voltage logic (regular or inverted)
  • Devices connected by three connections:
    1. common ground (so devices have a common reference point to measure voltage by)
    2. one wire as transmit line, for the sender
    3. one wire as receive line, for the reader
  • Arduino transmits spikes in voltage (1 bit * 8 = 1 byte per character) that correspond to binary; binary translates to ASCII
    • two bytes for return and new line characters
  • DIY Protocol: Serial.available(): tells us how many bytes have been received by the Arduino (and stored in the buffer), but not processed
  • analogRead() is 10 bits (0-1023), needs to be divided by 4 inside Serial.write() because Arduino transmits one byte (8 bits, or 0-255) at a time
    • Serial.write() sends data as raw binary, serial monitor in Arduino interprets as ASCII values (interpretation is built-in, makes sending strings easier)
    • println() takes a string, converts it to ASCII
    • Serial.write() sends binary representation, serial monitor converts to ASCII
    • Dec value is base 10, Hex is base 16

Serial Output from Arduino to p5.js

  • control over transmission and reception (we are both sender and receiver)
    • can decide whether an incoming byte should be interpreted as a byte (numerical value), or convert it to ASCII (for a message)
  • Serial handshaking:
    • Arduino sends info 100x/second, which is faster than receiver is processing it
    • handshaking avoids filling up the buffer
    • receiver (p5) sends a byte to transmitter once it’s ready to receive more data: Serial.write('x');
    • transmitter (arduino) waits for the signal: Serial.available() > 0
      • has to send initial signal
  • Reading strings:
    • instead of using Serial.write (which sends raw binary; only holds a single byte so needs to be divided by 4, ie compressed), use print and println to send ASCII data and interpret the string received in p5 as a float

Leave a Reply

Your email address will not be published. Required fields are marked *