Back to Parent

//Date - 02/16/2019  - Final code for the concept Pixel Away. Contributors: Ranveer Katyal, Menghan Zhang, and Sijia Li
//Idea: The follwoing code lets a user light up a specific Pixel on a 8x8 LED Matrix and transmit it to another device 

// This #include statement was automatically added by the Particle IDE.
#include <LedControl-MAX7219-MAX7221.h>
// IMPORTANT: Set pixel COUNT, PIN and TYPE
LedControl *led;
int phase = 0;
char message[64];
int messageLength = 0;
int myUptime = 0;
uint8_t data = A5;
uint8_t load = A4;
uint8_t myclock = A3;
int rowButtonPin = D1;
int colButtonPin  = D2;
int oldRowButtonState = HIGH;
int oldColButtonState = HIGH;
long lastPublishedAt = 0;
int publishAfter = 10000;
int pushSendButton = D6;
int rowIndex = -1;
int colIndex = -1;
String indices;

void setup() {
    Particle.subscribe(  "diot2019/PixelAway/" , handleSharedEvent );
    pinMode(pushSendButton, INPUT_PULLUP);
    
    led = new LedControl(data,myclock,load,1); //DIN,CLK,CS,HowManyDisplays
    led-> shutdown(0,false); //Turn it on
    led-> setIntensity(0,1);
    
    pinMode(rowButtonPin, INPUT_PULLUP);
    pinMode(colButtonPin, INPUT_PULLUP);
}

void loop() {
   
    String RowCol = pixelCounter();
    if (digitalRead(pushSendButton) == 0){
    publishMyEvent(RowCol);
    }

    // delay for a bit
    delay(100);
   

}


void publishMyEvent(String Loc)
{
  if( lastPublishedAt + publishAfter < millis() )
  {

      String eventName = "diot2019/PixelAway/" + System.deviceID();
      Particle.publish(eventName, Loc);
      lastPublishedAt = millis();
  }

}
void handleSharedEvent(const char *event, const char *data)
{
    String eventName = String( event ); // convert to a string object
    String deviceID = System.deviceID();
    String Loc = String(data);
    char colLoc = Loc[1];
    char rowLoc = Loc[0];
   // int colInd = colLoc.toInt(); //convert the string data  into Int
    // int rowInd = rowLoc.toInt(); //convert the string data  into Int
    
    Particle.publish("col", String(Loc[1]));
    Particle.publish("row", String(Loc[0]));

    if( eventName.indexOf( deviceID ) != -1 ){
      return;
    } else {
        ledLit(Loc[0],Loc[1]); // Lights up the Pixel sent byt the other device
        
    }

}

//The function PixelCounter lets the user select the specific pixel they want to light up by the press of push button
//The function returns the pixel locetion to be transmiited to other devices.

String pixelCounter(){
    int newRowButtonState = digitalRead(rowButtonPin);
    int newColButtonState = digitalRead(colButtonPin);
    
    if (newRowButtonState == LOW && oldRowButtonState == HIGH) {
        if (rowIndex == 7) {
            rowIndex = 0;
            led-> setLed(0, 7, colIndex, false);
            
        } else {
            rowIndex++;
        }
        
        if (colIndex == -1) {
            colIndex = 0;
        }
        led-> setLed(0, rowIndex, colIndex, true);
        led-> setLed(0, rowIndex-1, colIndex, false);
    }
    oldRowButtonState = newRowButtonState;
    
    if (newColButtonState == LOW && oldColButtonState == HIGH) {
        if (colIndex == 7) {
            colIndex = 0;
            led-> setLed(0, rowIndex, 7, false);
        } else {
            colIndex++;
        }
        
        if (rowIndex == -1) {
            rowIndex = 0;
        }
        led-> setLed(0, rowIndex, colIndex, true);
        led-> setLed(0, rowIndex, colIndex-1, false);

    }
    oldColButtonState = newColButtonState;
    
    delay(100);
    String indices = String(rowIndex)+String(colIndex);
    return indices;
}

void ledLit(char rowLoc, char colLoc){
    int intRow = rowLoc - '0';
    int intCol = colLoc - '0';
    led-> setLed(0, intRow, intCol, true);
    delay( 100 );
    
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0