Recitation 10: Workshops

I chose to attend the neopixel workshop, because I thought it is the most helpful for my final project.

Recitation 10- neomatrix

Arduino Code:

///setup for Neopixel
#include <FastLED.h> // declares to use the FastLEB library
#define LED_PIN  10
#define COLOR_ORDER GRB
#define CHIPSET     WS2811  // type of Neopixel
#define BRIGHTNESS 64

/*declare width and height of the matrix*/
const uint8_t kMatrixWidth = 8;
const uint8_t kMatrixHeight = 8;

/* for different pixel layouts */
const bool    kMatrixSerpentineLayout = true;

///setup for Serial Communication from Processing
#define NUM_OF_VALUES_FROM_PROCESSING 2    /** YOU MUST CHANGE THIS ACCORDING TO YOUR PROJECT **/

/** DO NOT REMOVE THESE **/
int tempValue = 0;
int valueIndex = 0;

/* This is the array of values storing the data from Processing. */
int processing_values[NUM_OF_VALUES_FROM_PROCESSING];


// This function will return the right 'led index number' for
// a given set of X and Y coordinates on your matrix.
uint16_t XY( uint8_t x, uint8_t y)
{
  uint16_t i;

  //if layed out in one direction
  if ( kMatrixSerpentineLayout == false) {
    i = (y * kMatrixWidth) + x;
  }

  //if layed out in reverse direction repeated (S shaped)
  if ( kMatrixSerpentineLayout == true) {
    if ( y & 0x01) {
      // Odd rows run backwards
      uint8_t reverseX = (kMatrixWidth - 1) - x;
      i = (y * kMatrixWidth) + reverseX;
    } else {
      // Even rows run forwards
      i = (y * kMatrixWidth) + x;
    }
  }

  return i;
}


#define NUM_LEDS (kMatrixWidth * kMatrixHeight)
CRGB leds_plus_safety_pixel[ NUM_LEDS + 1];
CRGB* const leds( leds_plus_safety_pixel + 1);


void setup() {
  Serial.begin(9600);
  FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalSMD5050);
  FastLED.setBrightness( BRIGHTNESS );
}

void loop()
{
  getSerialData();
  //start writing code here

  //use the coordinates snet from proessing
  //leds[ XY (x, y)] = CRGB::color
  leds[XY(processing_values[1], processing_values[0])] = CRGB::Blue;
  FastLED.show();//show whatever color

  //turn it off
  for (uint8_t x = 0; x < kMatrixWidth; x++) {
    for (uint8_t y = 0; y < kMatrixHeight; y++) {
      leds [XY (x, y)] = CRGB::Black;
    }
  }
  FastLED.show();
}




//receive serial data from Processing
void getSerialData() {
  while (Serial.available()) {
    char c = Serial.read();
    //switch - case checks the value of the variable in the switch function
    //in this case, the char c, then runs one of the cases that fit the value of the variable
    //for more information, visit the reference page: https://www.arduino.cc/en/Reference/SwitchCase
    switch (c) {
      //if the char c from Processing is a number between 0 and 9
      case '0'...'9':
        //save the value of char c to tempValue
        //but simultaneously rearrange the existing values saved in tempValue
        //for the digits received through char c to remain coherent
        //if this does not make sense and would like to know more, send an email to me!
        tempValue = tempValue * 10 + c - '0';
        break;
      //if the char c from Processing is a comma
      //indicating that the following values of char c is for the next element in the values array
      case ',':
        processing_values[valueIndex] = tempValue;
        //reset tempValue value
        tempValue = 0;
        //increment valuesIndex by 1
        valueIndex++;
        break;
      //if the char c from Processing is character 'n'
      //which signals that it is the end of data
      case '\n':
        //save the tempValue
        //this will b the last element in the values array
        processing_values[valueIndex] = tempValue;
        //reset tempValue and valueIndex values
        //to clear out the values array for the next round of readings from Processing
        tempValue = 0;
        valueIndex = 0;
        break;
    }
  }
}

Processing Code:

// Recitation 10-workshops

//setup for grid
int rows = 8; // number of rows in the NeoMatrix
int columns = 8;  // number of columns in the NeoMatrix
int x; // width of each cell
int y; // height of each cell
int lineWidth = 2;
int [][] cells = new int [8][8];
//define coordinates where mouse is clicked
int Xcor;
int Ycor;

//setup for serial communication
import processing.serial.*;

int NUM_OF_VALUES_FROM_PROCESSING = 2;  /** YOU MUST CHANGE THIS ACCORDING TO YOUR PROJECT **/
int processing_values[] = new int[NUM_OF_VALUES_FROM_PROCESSING]; /** this array stores values you might want to send to Arduino **/

Serial myPort;
String myString;


void setup() {
  size(600, 600);

  //setup cells
  for (int i = 0; i< rows; i++) {
    for (int j=0; j< columns; j++) {
      cells[i][j]= 1;
    }
  }
  setupSerial();
}

void draw() {
  background(0);
  stroke(255);
  strokeWeight(lineWidth);

  //draw the lines to make a grid
  x = width / rows;
  for (int i = 1; i < rows; i ++ ) {
    line (i*x, 0, i*x, height);
  }

  y = height / columns;
  for (int i = 1; i < columns; i ++ ) {
    line (0, i*y, width, i*y);
  }

  //if clicked, draw a rectangle (change color)
  if (cells[Xcor][Ycor] !=1) {
    rectMode(CORNER);
    fill(255);
    rect(x*Xcor, y*Ycor, x, y);
  }



  // send the values to Arduino.
  processing_values[0] = Xcor;
  processing_values[1] = Ycor;//arduino receiving [x,y]
  //println(processing_values[0], processing_values[1]);//check the values being sent
  sendSerialData();
}

void mousePressed() {

  ////set everything back to 1
  for (int i = 0; i<rows; i++) {
    for (int j=0; j< columns; j++) {
      cells[i][j]= 1;
    }
  }

    //check where the mouse is clicking
    if ((mouseX< x*rows) && (mouseY < y*columns)) {

      //use floor to round the number to nearest integer(whole number)
      //check coordinates
      Xcor = floor(mouseX/ x);
      Ycor = floor(mouseY/ y);

      //change 1 and -1 to check if it is clicked
      cells[Xcor][Ycor] *=-1;
    }
  }


  void setupSerial() {
    printArray(Serial.list());
    myPort = new Serial(this, Serial.list()[2], 9600);
    // WARNING!
    // You will definitely get an error here.
    // Change the PORT_INDEX to 0 and try running it again.
    // And then, check the list of the ports,
    // find the port "/dev/cu.usbmodem----" or "/dev/tty.usbmodem----"
    // and replace PORT_INDEX above with the index number of the port.

    myPort.clear();
    // Throw out the first reading,
    // in case we started reading in the middle of a string from the sender.
    myString = myPort.readStringUntil( 10 );  // 10 = '\n'  Linefeed in ASCII
    myString = null;
  }


  void sendSerialData() {
    String data = "";
    for (int i=0; i<processing_values.length; i++) {
      data += processing_values[i];
      //if i is less than the index number of the last element in the values array
      if (i < processing_values.length-1) {
        data += ","; // add splitter character "," between each values element
      }
      //if it is the last element in the values array
      else {
        data += "\n"; // add the end of data character linefeed "\n"
      }
    }
    //write to Arduino
    myPort.write(data);
    print(data); // this prints to the console the values going to arduino
  }

 

Leave a Reply

Your email address will not be published. Required fields are marked *