#include #include "FastLED.h" #define NUM_LEDS 8 #define DATA_PIN 3 //Pin connected to ST_CP of 74HC595 int latchPin = 9; //Pin connected to SH_CP of 74HC595 int clockPin = 10; ////Pin connected to DS of 74HC595 int dataPin = 11; SoftwareSerial recepcion(5, 6); // RX, TX CRGB leds[NUM_LEDS]; char arrayRecibido[4]; byte data; byte dataArray[8]; int arrayR[8][8]={ {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0} }; int arrayG[8][8]={ {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0} }; int arrayB[8][8]={ {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0} }; String recibido; int lenRecibido; int color; int xPos=0; int yPos=0; int colorR=255; int colorG=255; int colorB=255; void setup() { // Open serial communications and wait for port to open: Serial.begin(9600); while (!Serial) { ; // wait for serial port to connect. Needed for native USB port only } recepcion.begin(9600); FastLED.addLeds(leds, NUM_LEDS); pinMode(latchPin, OUTPUT); //Binary notation as comment dataArray[7] = 0x01; //0b00000001 dataArray[6] = 0x02; //0b00000010 dataArray[5] = 0x04; //0b00000100 dataArray[4] = 0x08; //0b00001000 dataArray[3] = 0x10; //0b00010000 dataArray[2] = 0x20; //0b00100000 dataArray[1] = 0x40; //0b01000000 dataArray[0] = 0x80; //0b10000000 //resetAllArray(); } void loop() { // run over and over if (Serial.available()>0) { recibido = Serial.readStringUntil(';'); lenRecibido = recibido.length(); if(lenRecibido>=3) { recibido.toCharArray(arrayRecibido, lenRecibido+1); xPos = arrayRecibido[1]-48; yPos = arrayRecibido[2]-48; color = arrayRecibido[3]-48; Serial.println(recibido); Serial.println(xPos); Serial.println(yPos); Serial.println(color); updateArray(xPos,yPos,color); } } /// for (int i = 0; i < 8; i++){ data=dataArray[i]; Serial.println(data,BIN); updateRow(i); digitalWrite(latchPin, 0); //move 'em out shiftOut(dataPin, clockPin, data); //return the latch pin high to signal chip that it //no longer needs to listen for information digitalWrite(latchPin, 1); delay(1); } } void updateArray(int pX, int pY, int col) { updateColor(col); arrayR[pX][pY]=colorR; arrayG[pX][pY]=colorG; arrayB[pX][pY]=colorB; } void updateRow(int fila) { for(int i = 0; i<8; i++){ Serial.print("R"); Serial.print(arrayR[fila][i]); Serial.print("G"); Serial.print(arrayG[fila][i]); Serial.print("B"); Serial.print(arrayB[fila][i]); } Serial.println(""); FastLED.show(); } void updateColor(int dato){ switch(dato){ case 1: colorR=214; colorG=0; colorB=115; break; case 2: colorR=26; colorG=161; colorB=224; break; case 3: colorR=227; colorG=199; colorB=0; break; case 4: colorR=92; colorG=168; colorB=229; break; case 5: colorR=250; colorG=102; colorB=77; break; case 6: colorR=168; colorG=0; colorB=255; break; case 7: colorR=0; colorG=0; colorB=0; break; default: break; } } // the heart of the program void shiftOut(int myDataPin, int myClockPin, byte myDataOut) { // This shifts 8 bits out MSB first, //on the rising edge of the clock, //clock idles low //internal function setup int i=0; int pinState; pinMode(myClockPin, OUTPUT); pinMode(myDataPin, OUTPUT); //clear everything out just in case to //prepare shift register for bit shifting digitalWrite(myDataPin, 0); digitalWrite(myClockPin, 0); //for each bit in the byte myDataOut� //NOTICE THAT WE ARE COUNTING DOWN in our for loop //This means that %00000001 or '1" will go through such //that it will be pin Q0 that lights. for (i=7; i>=0; i--) { digitalWrite(myClockPin, 0); //if the value passed to myDataOut and a bitmask result // true then... so if we are at i=6 and our value is // %11010100 it would the code compares it to %01000000 // and proceeds to set pinState to 1. if ( myDataOut & (1<