#include<math.h>
#define PIN_SETA_ESQUERDA 5
#define PIN_SETA_BAIXO 19
#define PIN_SETA_DIREITA 18
#define PIN_SETA_CIMA 4
#define PIN_START 21
#define PIN_A 26
#define PIN_B 25
#define PIN_X 27
#define PIN_Y 33
#define XPIN 32
#define YPIN 35
#define ZPIN 34
const int buffer = (2 << 6);
int medidas[buffer][2];
int bufferIdx = 0;
const double ADC = 3.3;
const double ADC_AMPLITUDE = 4096.0;
const double ZERO_G_X = 3.3/2.0;
const double SENSIBILIDADE = 360.0/1000.0;
bool acelerometroEnable = true;
bool A_B_press = false;
void calc(int xv, int yv, int zv){
medidas[bufferIdx][0] = xv;
medidas[bufferIdx][1] = yv;
medidas[bufferIdx][2] = zv;
bufferIdx = (bufferIdx + 1) % buffer;
Serial.print(bufferIdx);
Serial.print(" ");
Serial.println(buffer);
//pega o output em volts
double xvoltage = (double)xv * ADC / ADC_AMPLITUDE;
Serial.print(xvoltage);
Serial.println(" volts.");
//calc o mov de roll (testar se eh esse no controle)
//baseado em: https://www.analog.com/media/en/technical-documentation/data-sheets/ADXL335.pdf
double xacc = (xvoltage - ZERO_G_X) / SENSIBILIDADE;
Serial.print(xacc);
Serial.println(" aceleracao em x.");
}
void setup() {
Serial.begin(115200);
memset(medidas, 0, sizeof(medidas));
pinMode(PIN_SETA_ESQUERDA, INPUT_PULLUP);
pinMode(PIN_SETA_BAIXO, INPUT_PULLUP);
pinMode(PIN_SETA_DIREITA, INPUT_PULLUP);
pinMode(PIN_SETA_CIMA, INPUT_PULLUP);
pinMode(PIN_START, INPUT_PULLUP);
pinMode(PIN_A, INPUT_PULLUP);
pinMode(PIN_B, INPUT_PULLUP);
pinMode(PIN_X, INPUT_PULLUP);
pinMode(PIN_Y, INPUT_PULLUP);
pinMode(XPIN, INPUT);
pinMode(YPIN, INPUT);
pinMode(ZPIN, INPUT);
}
void loop() {
if(digitalRead(PIN_A) == LOW)
{
Serial.println("--------------");
if(!A_B_press){
acelerometroEnable = !acelerometroEnable;
A_B_press = true;
}
}
else
{
A_B_press = false;
}
delay(100);
if(acelerometroEnable == true)
{
int xv = analogRead(XPIN);
int yv = analogRead(YPIN);
int zv = analogRead(ZPIN);
Serial.print(acelerometroEnable);
Serial.print(A_B_press);
Serial.print("\neixo x mexendo: ");
Serial.println(xv);
Serial.print("eixo y mexendo: ");
Serial.println(yv);
Serial.print("eixo z mexendo: ");
Serial.println(zv);
calc(xv, yv, zv);
}
if (digitalRead(PIN_SETA_ESQUERDA) == LOW) {
Serial.println("Botão Seta para Esquerda foi pressionado");
}
if (digitalRead(PIN_SETA_BAIXO) == LOW) {
Serial.println("Botão Seta para Baixo foi pressionado");
}
if (digitalRead(PIN_SETA_DIREITA) == LOW) {
Serial.println("Botão Seta para Direita foi pressionado");
}
if (digitalRead(PIN_SETA_CIMA) == LOW) {
Serial.println("Botão Seta para Cima foi pressionado");
}
if (digitalRead(PIN_START) == LOW) {
Serial.println("Botão Start foi pressionado");
}
if (digitalRead(PIN_A) == LOW) {
Serial.println("Botão A foi pressionado");
}
if (digitalRead(PIN_B) == LOW) {
Serial.println("Botão B foi pressionado");
}
if (digitalRead(PIN_X) == LOW) {
Serial.println("Botão X foi pressionado");
}
if (digitalRead(PIN_Y) == LOW) {
Serial.println("Botão Y foi pressionado");
}
delay(100);
}