Hi, I'm currently trying to connect both my processing and arduino sketch together using the serial. It works for a short time, then it will stop receiving any inputs, and only send null on the receiver.
The code below uses some dotstar code, just ignore it
#include <Adafruit_DotStar.h>
#include <SPI.h>
#define DATAPIN 4
#define CLOCKPIN 5
Adafruit_DotStar strip(30, DATAPIN, CLOCKPIN, DOTSTAR_BRG);
uint32_t magenta = strip.Color(255, 0, 255);
uint32_t green = strip.Color(0, 255, 0);
uint32_t red = strip.Color(255, 0, 0);
char val;
bool trigger = false;
void setup() {
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
// establishContact();
strip.begin();
strip.show();
establishContact(); // send a byte to establish contact until receiver responds
}
void loop() {
int val0 = -1;
int val1 = -1;
int val2 = -1;
int val3 = -1;
bool s = trigger;
if (Serial.available() > 0) { // If data is available to read,
val = Serial.read(); // read it and store it in val
if(val == 'p') {
trigger = true;
}
if(val == 'b') {
trigger = false;
}
}
else {
delay(50);
}
if (s) {
strip.fill(magenta, 0, 30);
strip.show();
}
else {
strip.fill(green, 0, 30);
strip.show();
}
Serial.println("" + String(val0) + " " + val1 + " " + val2 + " " + val3);
delay(200);
}
void establishContact() {
while (Serial.available() <= 0) {
Serial.println("A"); // send a capital A
delay(300);
}
}