Optimisation of 2 wired Arduino's, each with a LOL Led Shield and Touch Screen

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();
	}
}
	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();

If there was a button press, get the x0, y0, z0 values. Then, regardless of whether the user did anything, or not, send the values. Please explain why.

	int radius = pow(z0,2);

Look at the documentation for the pow() function. Then, go look at the source code for the pow() function. Then, explain why

int radius = z0 * z0;

isn’t a better idea.

	for (uint8_t x = 0; x < rows; x++) {
		for (uint8_t y = 0; y < cols; y++) {
			if (radius > 100) {

Test the radius first, ONE time, not deep in nested loops.

				int dist = sqrt(pow((x*100)-x0,2)+pow((y*100)-y0,2));

More wasteful, incorrect, calls to pow().

	delay(10);

Why?

it acts a bit sluggish

Why am I not surprised?

        /*Wire.beginTransmission(5);
        Wire.write(x0);
        Wire.write(y0);
        Wire.write(z0);
        Wire.endTransmission();*/

Why is this now commented out?

Why are you doing anything if the touch screen isn't touched?

                int dist = sqrt(pow((x*100)-x0,2)+pow((y*100)-y0,2));

Some intermediate variables would be useful:

      int dx = x*100 - x0;
      int dy = y*100 - y0;
      int dist = sqrt(dx * dx + dy * dy);
                    brightness = map(dist,0,z0,7,1);

Doing this

I would need to maybe mention also that the two Arduinos communicate with each other so one should be able to receive signals from B while no pressure is being applied to A.

Nothing in the code you posted reads what the other Arduino writes to the I2C pins.