Hi!
It took me a while to get this working.
I have 2 Arduino's communicating through i2c. Each has a LOL Led Shield and a Touch Screen. As one touches the screen it is both reflected on the directly connected Arduino as well as its sibling. Same goes for the other one.
I would love some help optimising the "drawing" of the LED's in the Loop, as it acts a bit sluggish sometimes. This could be due to the processor, but maybe also my newb code.
Thank you!
#include <Charliplexing.h>
#include <TouchScreen.h>
#include <Wire.h>
#define Y1 A0 // YM
#define X2 A1 // XP
#define Y2 A2 // YP
#define X1 A3 // XM
#define MINPRESSURE 10
#define MAXPRESSURE 1000
const uint8_t rows = 14;
const uint8_t cols = 9;
const bool master = true;
int radius;
int x0,y0,z0; //incoming values
int x1,y1,z1; //incoming values
int brightness;
TouchScreen ts = TouchScreen(X2, Y2, X1, Y1, 300);
struct Point {
byte brightness;
} points[rows][cols];
void setup() {
LedSign::Init(GRAYSCALE);
LedSign::SetBrightness(100);
Serial.begin(9600);
if (master) {
Wire.begin();
} else {
Wire.begin(5);
Wire.onReceive(receiveEvent);
}
}
void loop() {
TSPoint p = ts.getPoint();
if (p.z > ts.pressureThreshhold) {
x0 = map(p.x,920,140,0,rows*100);
y0 = map(p.y,300,820,0,(cols+1)*100);
z0 = map(p.z,300,200,1,7);
}
Wire.beginTransmission(5);
Wire.write(x0);
Wire.write(y0);
Wire.write(z0);
Wire.endTransmission();
int radius = pow(z0,2);
for (uint8_t x = 0; x < rows; x++) {
for (uint8_t y = 0; y < cols; y++) {
if (radius > 100) {
//int dist = abs((x*100)-x0)+abs((y*100)-y0);
int dist = sqrt(pow((x*100)-x0,2)+pow((y*100)-y0,2));
if (dist < radius) {
brightness = map(dist,0,radius,7,1);
points[x][y].brightness = brightness;
LedSign::Set(x,y,brightness);
} else if (points[x][y].brightness > 0) {
LedSign::Set(x,y,--points[x][y].brightness);
}
}
}
}
delay(10);
}
void receiveEvent(int howMany) {
while (Wire.available()) {
x1 = Wire.read();
y1 = Wire.read();
z1 = Wire.read();
}
}