Go Down

Topic: Binary Data Transmission using the onboard LED (Read 8 times) previous topic - next topic

ElectricPuha

This is my first Arduino sketch for your enjoyment. I only have the Arduino Uno and no other hardware, so no other components are necessary.

The sketch takes a string of your choosing, and then 'transmits' it using the builtin LED as binary flashes.

Code: [Select]
// a simple sketch that converts an ascii string into bits that are then used to blink the onboard led.
// eg "hello world" becomes "001000000110100001100101011011000110110001101111001000000111011101101111011100100110110001100100"

// initialise our delay intervals for turning the led on and off in ms
int myDelay = 90;             // the linger time for each data bit pulse
int myBriefDelay =10;         // the inter-bit delay for real data
int myJabberDelay = 20;       // the delay between each jabber signal pulse
int myJabberBoundary = 750;   // the delay between the jabber signal and real data

// initialise our other variables
String myString = String("hello world");                          // our string we want to transmit
unsigned int myStringLength = myString.length();                  // the length of our string
char currentChar = char('1');                                     // just a placeholder char to be replaced later
int myJabberLength = 25;                                          // the number of pulses in the jabber signal

void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  // we send a jabber sequence so we know some real data is coming after a brief delay
  for (int i = 0; i <= myJabberLength; i++) {
    digitalWrite(LED_BUILTIN,HIGH);
    delay(myJabberDelay);
    digitalWrite(LED_BUILTIN, LOW);
    delay(myJabberDelay);
  }
  delay(myJabberBoundary);
 
  // gets the currentChar at each position within myString
  for ( int i = 0; i<= myStringLength; i++) {

    // get the char at the current position in myString
    currentChar = myString.charAt(i);
 
    // for the current char we do the following to break it from an ascii char to 8 bits ready for binary transmission
    for  ( uint8_t bitMask = 128; bitMask !=0; bitMask = bitMask >> 1) {
      if ( currentChar & bitMask ) {
        digitalWrite(LED_BUILTIN, HIGH);
        delay(myDelay);
      } else {
        digitalWrite(LED_BUILTIN, LOW);
        delay(myDelay);
      }
      // a minor flash between valid signals so we can differentiate between consecutive high/high and low/low bits
        digitalWrite(LED_BUILTIN,HIGH);
        delay(myBriefDelay);
        digitalWrite(LED_BUILTIN,LOW);
        delay(myBriefDelay);
    }
  }
 // we don't need a repeat of the entire jabber sequence since we are in a massive loop anyway, and its up next.
 delay(myJabberDelay);
}

Go Up