F. writing sketches for the wixel shield, Example serial communication sketch – Pololu Wixel Shield for Arduino User Manual

Page 11

Advertising
background image

2.f. Writing Sketches for the Wixel Shield

Writing sketches for the Wixel shield is as easy as using the standard serial library to transmit and receive serial data.
Note that when first enabling the serial port, you might receive an invalid serial byte as a result of noise on the serial
lines. You can keep the Wixel from receiving this noise byte by delaying for ten milliseconds before and after calling

Serial.begin()

, like this:

delay(10);

Serial.begin(115200); // change this baud rate to the baud rate you set on your Wixel

delay(10);

Example Serial Communication Sketch

Here is a sketch (based off of the example

physical pixel sketch

[http://www.arduino.cc/en/Tutorial/PhysicalPixel]

) that

demonstrates wireless serial communication between an Arduino and a computer:

const int ledPin = 13; // the pin that the LED is attached to

int incomingByte; // a variable to read incoming serial data into

void setup() {

// initialize serial communication:

delay(10);

Serial.begin(115200); // *** NOTE: change this to the baud rate you set on your Wixel

delay(10);

Serial.println("Wireless Physical Pixel");

Serial.println("Send 'H' to turn on the LED and 'L' to turn off the LED.");

// initialize the LED pin as an output:

pinMode(ledPin, OUTPUT);

}

void loop() {

// see if there's incoming serial data:

if (Serial.available() > 0) {

// read the oldest byte in the serial buffer:

incomingByte = Serial.read();

// if it's a capital H (ASCII 72), turn on the LED:

if (incomingByte == 'H') {

digitalWrite(ledPin, HIGH);

Serial.println("LED is on");

}

// if it's an L (ASCII 76) turn off the LED:

if (incomingByte == 'L') {

digitalWrite(ledPin, LOW);

Serial.println("LED is off");

}

}

}

If you paste this code into an empty sketch, you should be able to wirelessly upload it to your Arduino and use the
serial monitor to communicate with your Arduino. In the serial monitor, you should see instructions for how to use
the sketch.

Pololu Wixel Shield for Arduino User's Guide

© 2001–2014 Pololu Corporation

2. Getting Started

Page 11 of 12

Advertising