/* 8x8 <-> processing code vers 0.1 by jacob remin for more information and documentation please refer to the development blog: www.campingsex.org/8x8/ max7219 control and serial communication based on examples provided by arduino sketchbook examples */ int serialVal = 0; int value = 0; int i = 0; boolean reading = false; boolean writing = false; int times = 0; int serialBuff[7]; int ledPin = 13; int serInString[8]; int y=0; int potPin = 0; // define wiring pins byte pin_max7219_clock = 4; byte pin_max7219_load = 2; byte pin_max7219_dataIn = 3; // specify wiring pin i/o directions void setPinModes() { pinMode(pin_max7219_dataIn, OUTPUT); pinMode(pin_max7219_clock, OUTPUT); pinMode(pin_max7219_load, OUTPUT); pinMode(ledPin, OUTPUT); pinMode(potPin, INPUT); } // define max7219 registers byte max7219_reg_noop = 0x00; byte max7219_reg_digit0 = 0x01; byte max7219_reg_digit1 = 0x02; byte max7219_reg_digit2 = 0x03; byte max7219_reg_digit3 = 0x04; byte max7219_reg_digit4 = 0x05; byte max7219_reg_digit5 = 0x06; byte max7219_reg_digit6 = 0x07; byte max7219_reg_digit7 = 0x10; byte max7219_reg_decodeMode = 0x09; byte max7219_reg_intensity = 0x0a; byte max7219_reg_scanLimit = 0x0b; byte max7219_reg_shutdown = 0x0c; byte max7219_reg_displayTest = 0x0f; // define max7219 as rows and cols (for nexus 8x8 displays) byte max7219_row0 = 0x01; byte max7219_row1 = 0x02; byte max7219_row2 = 0x03; byte max7219_row3 = 0x04; byte max7219_row4 = 0x05; byte max7219_row5 = 0x06; byte max7219_row6 = 0x07; byte max7219_row7 = 0x08; byte max7219_col0 = 0x01; byte max7219_col1 = 0x02; byte max7219_col2 = 0x04; byte max7219_col3 = 0x08; byte max7219_col4 = 0x10; byte max7219_col5 = 0x20; byte max7219_col6 = 0x40; byte max7219_col7 = 0x80; // program initialization routine void setup() { Serial.begin(9600); setPinModes(); max7219_init(); } // program loop void loop() { // read the serial port int val = Serial.read(); // if the input is '-1' then there is no data // at the input, otherwise check out if it is 'X' if (val != -1) { //Serial.print("something is there"); if (val == 'X') { updateDisplay (); } } int potVal = analogRead (potPin); Serial.print ("P"); Serial.println (potVal); delay(10); } void updateDisplay() { digitalWrite(ledPin, HIGH); for (y=0; y<8; y++) { int rowval=0; int a = readAndCompare(); int b = readAndCompare(); int c = readAndCompare(); int d = readAndCompare(); int e = readAndCompare(); int f = readAndCompare(); int g = readAndCompare(); int h = readAndCompare(); rowval=a+b*2+c*4+d*8+e*16+f*32+g*64+h*128; /* Serial.print(" y: "); Serial.print(y); Serial.print(" rowval: "); Serial.println(rowval); */ if (y==0) { max7219_put(max7219_row0, rowval); } else if (y==1) { max7219_put(max7219_row1, rowval); } else if (y==2) { max7219_put(max7219_row2, rowval); } else if (y==3) { max7219_put(max7219_row3, rowval); } else if (y==4) { max7219_put(max7219_row4, rowval); } else if (y==5) { max7219_put(max7219_row5, rowval); } else if (y==6) { max7219_put(max7219_row6, rowval); } else if (y==7) { max7219_put(max7219_row7, rowval); } } digitalWrite(ledPin, LOW); //Serial.flush(); } char secureRead() { while (Serial.available() == 0) { // do nothing } return Serial.read(); } int SerialReadHexDigit() { // Serial.print("SerialReadHexDigit "); byte c = secureRead(); if (c >= '0' && c <= '9') { return c - '0'; } else if (c >= 'a' && c <= 'f') { return c - 'a' + 10; } else if (c >= 'A' && c <= 'F') { return c - 'A' + 10; } else { return -1; // getting -1 here is bad: it means the character was invalid } } int readAndCompare() { byte readHex = SerialReadHexDigit(); // Serial.print(" RH:"); // Serial.print(int(readHex)); if (readHex >= 8 && readHex < 16) { return 1; } else if (readHex >= 0 && readHex < 8) { return 0; } else { return -1; // read error! } } void stripeLED () { max7219_put(max7219_row0, 1); max7219_put(max7219_row1, 2); max7219_put(max7219_row2, 4); max7219_put(max7219_row3, 8); max7219_put(max7219_row4, 16); max7219_put(max7219_row5, 32); max7219_put(max7219_row6, 64); max7219_put(max7219_row7, 128); } // below are all the max7219 library functions // function to control max7219 data line void max7219_setData(boolean value) { digitalWrite(pin_max7219_dataIn, value); } // function to control max7219 clock line void max7219_setClock(boolean value) { digitalWrite(pin_max7219_clock, value); } // function to control max7219 load line void max7219_setLoad(boolean value) { digitalWrite(pin_max7219_load, value); } // function that puts a byte of data to the max7219 void max7219_putByte(byte data) { byte i = 8; byte mask; while(i > 0) { mask = 0x01 << (i - 1); // get bitmask max7219_setClock(LOW); // tick if (data & mask){ // choose bit max7219_setData(HIGH); // send 1 }else{ max7219_setData(LOW); // send 0 } max7219_setClock(HIGH); // tock --i; // move to lesser bit } } // function that puts a byte of data into a max7219 register void max7219_put(byte reg, byte data) { max7219_setLoad(HIGH); // begin max7219_putByte(reg); // specify register max7219_putByte(data); // put data max7219_setLoad(LOW); // latch in data max7219_setLoad(HIGH); // end } // function that sets brightness of the max7219 void max7219_setIntensity(byte intensity) { // range: 0x00 to 0x0f max7219_put(max7219_reg_intensity, intensity & 0x0f); } //////////////////////////////////////////// // function that sets the same value for all registers of the max7219 void max7219_all(byte value) { max7219_put(0x01, value); max7219_put(0x02, value); max7219_put(0x03, value); max7219_put(0x04, value); max7219_put(0x05, value); max7219_put(0x06, value); max7219_put(0x07, value); max7219_put(0x08, value); } /////////////////////////////////////////// void printBuff(){ for (i=1;i <= 8;i++){ max7219_put(0x0+i, serialBuff[i-1]); } } //////////////////////////////////////////// void readBuff(){ Serial.print("into readbuff method"); serialVal = Serial.read(); if (serialVal != -1){ Serial.print("information there"); if ((serialVal == 43) && !reading){ reading = true; Serial.print("plus"); } if ((serialVal == 45) && (times == 0 && reading)){ writing = true; Serial.print("minus"); } if (reading && writing){ serialBuff[times] = serialVal; times++; } if (times >= 7){ Serial.print("Print to buff"); times = 0; reading = false; writing = false; printBuff(); } } } ////////////////////////////////////////////////////////////// // function that initializes the max7219 to use a matrix of leds void max7219_init() { max7219_put(max7219_reg_scanLimit, 0x07); // use all 8 columns max7219_put(max7219_reg_decodeMode, 0x00); // using an led matrix (not digits) max7219_put(max7219_reg_shutdown, 0x01); // not in shutdown mode max7219_put(max7219_reg_displayTest, 0x00); // no display test max7219_all(0x00); // empty registers max7219_setIntensity(0x0f); // set initial brightness to dim }