Hi!
I hope this is the right place to post this. I am working on what I would like to be a big LED project using an interface between arduino and processing, with Processing generating patterns and sending them to the Arduino to display. To test the feasibility of this, I wrote a test code for Processing to send a string of data to the Arduino via Serial, and for the Arduino to read that string and display the corresponding pattern on a 3 by 3 LED matrix. However, the code produces absolutely no result, and I can't for the life of me figure out why.
Here is my Arduino code
#include "FastLED.h"
// How many leds in your strip?
#define NUM_LEDS 9
// define the Data Pin
#define DATA_PIN 2
// Define the array of leds
CRGB leds[NUM_LEDS];
// Use a reading boolean
boolean reading = false;
String string = ""; // where we ll store the pattern data
void setup() {
Serial.begin(9600);
FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);
}
void loop() {
if (Serial.available() ) {
if (reading == false) {
char ch = Serial.read();
if (ch == '9') {
reading = true;
}
}else{
char ch = Serial.read();
if (ch != 8) {
string = string + ch;
} else {
readLights(string);
reading = false;
string = "";
}
}
}
FastLED.show();
}
void readLights(String string) {
for(int i=1; i<10; i++){ // read only the characters that aren't the 9 which means start and the 8 which means end
char c = string.charAt(i);
if(c == 1) {
leds[i-1] = CRGB(0, 0, 225);
}else{
leds[i-1] = CRGB(255, 0, 0);
}
}
}
And here is my processing code:
import processing.serial.*;
Serial myPort;
//the string I want to send
String stringOut = "91211211218"; //9 means start, 8 means end, the rest is the pattern
char[] strChar;
void setup() {
// punt my string from a string to a char array
strChar = stringOut.toCharArray();
//set up serial port for your arduino
String portName = Serial.list()[1];
myPort = new Serial(this, portName, 9600);
}
//global index into the char array
int i=0;
void draw() {
// nothing to draw all don upstairs in serial event
for (int i = 0; i<stringOut.length(); i++) {
myPort.write(strChar[i]);
}
}
If anyone had any idea of what I am doing wrong it would be amazing!! Also, is there any other way to communicate strings to the Arduino than via Serial? I have heard good things about Firmata but it doesn't look compatible with the FastLED library.
Anyway thanks a lot if anyone have an idea on what I am doing wrong! It's probably very obvious but I am completely new to Arduino