The following codes were written by Code::Blocks v.12.11, but you can use any editor of your choice. Create two separate files, one named Pixel.h, the other named Pixel.cpp. Create a new folder named “Pixel” under “libraries” directory and put both Pixel.cpp and Pixel.h in there. Learn more about Arduino libraries: http://arduino.cc/en/Guide/Libraries
Pixel.h
/* First Release April 11, 2013 by Jenna Yun Lee, Cooper Union, All rights reserved. I strongly support open source programming. Free to distribute and modify, but please keep the original copyright information. Contact lee55@cooper.edu for suggestions/errors/helps! This code is originally written for a two-player arcade game that makes uses of LED lights. Pixel.h and Pixel.cpp allows a programmer to control each pixel from an array of LEDs much easily. */ //Arduino Setting; Each arduino has different chipset per version. //Include the following if commands for program expandability. #if (ARDUINO >= 100) #include <Arduino.h> #else #include <WProgram.h> #include <pins_arduino.h> #endif #define PIXEL_H class Pixel { //Constructor public: Pixel(); Pixel(int address); void setAddress(int new_address), //Change address setSide(bool new_side), //Changes side setState(bool new_state), //Change state; 1 if the target is hit, 0 otherwise. 0 by default. setColor(int new_color_r, int new_color_g, int new_color_b); //Changes color int getAddress(void), getRed(void), getGreen(void), getBlue(void); bool getSide(void), getState(void); private: int address, // address number in decimals color_r, color_g, color_b; bool side, //0 for RED, 1 for BLUE state; //0 for off, 1 for on };
Pixel.cpp
/* First Release April 11, 2013 by Jenna Yun Lee, Cooper Union, All rights reserved. I strongly support open source programming. Free to distribute and modify, but please keep the original copyright information. Contact lee55@cooper.edu for suggestions/errors/helps! This code is originally written for a two-player arcade game that makes uses of LED lights. Pixel.h and Pixel.cpp allows a programmer to control each pixel from an array of LEDs much easily. */ #include "Pixel.h" //Constructors Pixel::Pixel() {} Pixel::Pixel(int addr) { address = addr; state = 0; //Our arcade game uses 5x5 LED grid, so 50 flippers for two sides. //Side 0 if address number is less than 24 (up to 25th LED), Side 1 otherwise if (addr>24) side = 1; else side =0; } //A series of getter-setter functions from here: //Address int Pixel::getAddress(void) { return address; } void Pixel::setAddress(int new_address) { address = new_address; } //Side bool Pixel::getSide(void) { return side; } void Pixel::setSide(bool new_side){ side = new_side; } //State bool Pixel::getState(void){ return state; } void Pixel::setState(bool new_state){ state = new_state; } //Color, RGB separately int Pixel::getRed(void) { return color_r; } int Pixel::getGreen(void) { return color_g; } int Pixel::getBlue(void) { return color_b; } void Pixel::setColor(int new_color_r, int new_color_g, int new_color_b) { color_r = new_color_r; color_g = new_color_g; color_b = new_color_b; }