Explicação do código


#1

oi preciso de ajuda para entender esse código:`#include “elapsedMillis.h”
#include “RunningMedian.h”
#include “Wire.h”
#include “SFE_MMA8452Q.h”

int potPin = 2;
int ledPin = 13;
int enabledPin = 12;
int tolerance = 100;
int val = 0;
bool blocked = false;
bool pressed = false;
MMA8452Q acelerometro(0x1C);
RunningMedian samplesx = RunningMedian(15);
RunningMedian samplesy = RunningMedian(15);
RunningMedian samplesz = RunningMedian(15);
elapsedMillis timeElapsed;
uint8_t buf[8] = { 0 }; /* Keyboard report buffer */

void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(enabledPin, INPUT_PULLUP);
digitalWrite(ledPin, LOW);
Serial.begin(9600);
acelerometro.init();
}

void loop()
{
bool enabled = digitalRead(enabledPin);
if (enabled) {
val = analogRead(potPin); // read the value from the sensor
tolerance = val * 2;
if (acelerometro.available()) {
acelerometro.read();
samplesx.add(acelerometro.x);
samplesy.add(acelerometro.y);
samplesz.add(acelerometro.z);
if (!blocked) {
bool tmpx = (acelerometro.x > (samplesx.getAverage() + tolerance)) || (acelerometro.x < (samplesx.getAverage() - tolerance));
bool tmpy = (acelerometro.y > (samplesy.getAverage() + tolerance)) || (acelerometro.y < (samplesy.getAverage() - tolerance));
bool tmpz = (acelerometro.z > (samplesz.getAverage() + tolerance)) || (acelerometro.z < (samplesz.getAverage() - tolerance));
if (tmpx || tmpy || tmpz) {
buf[2] = 0x45; // F12
Serial.write(buf, 8); // Send keypress
digitalWrite(ledPin, HIGH);
blocked = true;
pressed = true;
timeElapsed = 0;
}
}
}
}
if (pressed && (timeElapsed > 100) ) {
buf[0] = 0;
buf[2] = 0;
Serial.write(buf, 8); // Release key
pressed = false;
}
if (blocked && (timeElapsed > 500) ) {
digitalWrite(ledPin, LOW);
blocked = false;
}
}`
esse programa pode ser visto nesse site
https://www.embarcados.com.br/comunicacao-para-ela-utilizando-arduino/