Sketch

The Arduino code in base is pretty simple..

const int ledPin = 13;
const int sizeOfDataPacket = 8;
unsigned int counter = 0;
unsigned int dataPacket[sizeOfDataPacket];

void setup() {
  pinMode(ledPin, OUTPUT);
  
  Serial1.begin(9600);
  
  /* Signal we're alive */
  tone(ledPin, 5, 1000);
}

void loop() {
  /* Reset data */
  dataPacket[0] = dataPacket[1] = dataPacket[2] = dataPacket[3] = 
    dataPacket[4] = dataPacket[5] = dataPacket[6] = dataPacket[7] =  0;
   
  /* Assemble data */
  dataPacket[0] = 7; // Version
  dataPacket[1] = counter++; // Counter
  
  /* Read analog inputs */
  dataPacket[2] = analogRead(A0);
  dataPacket[3] = analogRead(A1);
  dataPacket[4] = analogRead(A2);
  dataPacket[5] = analogRead(A3);
  dataPacket[6] = analogRead(A4);
  
  /* Read digital input. Int is 16 bits so max 16 values. */
  bitWrite(dataPacket[7], 0, digitalRead(2) == HIGH);
  bitWrite(dataPacket[7], 1, digitalRead(3) == HIGH);
  bitWrite(dataPacket[7], 2, digitalRead(4) == HIGH);
  bitWrite(dataPacket[7], 3, digitalRead(5) == HIGH);
  bitWrite(dataPacket[7], 4, digitalRead(6) == HIGH);
  bitWrite(dataPacket[7], 5, digitalRead(7) == HIGH);
  bitWrite(dataPacket[7], 6, digitalRead(8) == HIGH);
  bitWrite(dataPacket[7], 7, digitalRead(9) == HIGH);
  bitWrite(dataPacket[7], 8, digitalRead(10) == HIGH);
  bitWrite(dataPacket[7], 9, digitalRead(11) == HIGH);
  bitWrite(dataPacket[7], 10, digitalRead(12) == HIGH);
   
  /* Write data. Unfortunately write only does ints or char *,
     the int buffer has to be written one int at a time.. */
  digitalWrite(ledPin, HIGH);
  for (int i = 0; i < sizeOfDataPacket; i++) {
    Serial1.write(dataPacket[i]);
  }
  digitalWrite(ledPin, LOW);
  
  delay(1000);
}


Seems to work ok, though the Serial monitor is a bit useless, it only really deals with ASCI data, not binary stuff.

Something that did occur to me is that there definitely are more flags, however I don’t have enough pins on the Leonardo. I’ll have to use some of the GPIO pins on the Pi for that. To make things consistent, I’d have to switch to an Arduino Due or Mega. I might do that for consistency reasons, and to use the Leonardo for something else. (Who knows what!)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: