All of the code needed to run this project is located at
https://bitbucket.org/alterationx10/8bitbox-adafruit

The Arduino sketch is located (from the source root directory) at

/Arduino/_8BitBox_Adafruit/_8BitBox_Adafruit.ino

The code is fairly commented, and is best explained while looking right at it.

Some concepts are highlighted/discussed below, but you should check out the source for all the details.

Defining some Variables:

Here are the variables defined at the top of the sketch, and used throughout the code.
// Define our pins
int buzzerPin = 3;  // Piezo buzzer is hooked to pwm 3
int redPin = 9;     // Red LED hooked to pwm 9
int greenPin = 10;  // Green LED hooked to pwm 10
int bluePin = 11;   // Blue LED hooked to pwm 11

int commandByte;   // A variable for command parsing

// We will store our favorite color in the EEPROM
// NOTE:
// From http://playground.arduino.cc/Code/EEPROM-Flash :
// "The ATmega328 has 32 KiB of Flash program memory. 
//  This memory supports at least 10,000 writes or upload cycles."
int FAV_RED = 0;
int FAV_GREEN = 1;
int FAV_BLUE = 2;

Defining some Functions:

Setting an LED:


Here is a snippet of a function used to set the value of the Red LED (similar functions are made for the G and B). Note that we use a common anode RGB LED, so we use analogWrite(pin, 255 - colorValue)!
// Set the PWM of the Red LED
void setRed(int val) {
  // Sanitize the value
  if (val > 255) {
    val = 255;
  }
  if (val < 0) {
    val = 0;
  }  
  analogWrite(redPin, 255 - val);
}

Making a blocking read:

Serial.read() will perform faster than we can send data to the Arduino, se we will need to know "how to speak" to the device using our own protocol. When we receive a known command of our protocol, we know there could be more data coming, so we need to do a blocking read to make sure we get it.
// Serial.read() appears to go much faster than we can send text.
// Therefore, we add a method to do a blocking read.
int getNextByte() {
  while (Serial.available() == 0) {
    // BLOCK
  }
  return Serial.read();
}

The essential behavior of the sketch:

The loop() function of our sketch just checks to see if serial data is available, and if so, parse the command and perform an action.

Below is a snippet to show how it would parse for a command to set the Red LED to a certain value, using the two functions shown above.

In this example, we will send an "R" from the Android device as our protocol command to set the Red LED to a value, along with the value (0-255) to set it to.

If we receive an "R", then we know that there is one more byte on it's way as a value to set the LED to (0-255), so we then perform a blocking read to get it, and then use our function to set the value to the Red LED.
void loop() {
  
   
  // All our loop currently does is check for serial data
  // and parse serial commands
  if (Serial.available() > 0) {
    
    // Read our command byte
    commandByte = Serial.read();
    // See if it matches any of our commands
   
    // Set the Red LED
    if (commandByte == 'R') {
      int redVal =  getNextByte();
      setRed(redVal);
    }
    
    // Parse for other commands in full sketch...
  }
}

This guide was first published on Jun 13, 2014. It was last updated on Jun 13, 2014.

This page (Arduino Code) was last updated on Jan 21, 2014.

Text editor powered by tinymce.