Olá desenvolvedores para Arduino.
Estou com uma dúvida ja tem algum tempo que não consigo resolver, vim aqui buscar alguma dica para a solucão desse meu problema.
Estou usando o Accelerometer ADXL345 para pegar a direção X_out e Y_out.
O intuito é fazer com que meu Led da esquerda entre na função de Pisca quando o valor X_out < -0.3 e fique em looping até que meu eixo Y_out seja superior a 0.3 e o mesmo quando meu eixo X_out for > 0.3, fazer com que o Led da esquerda acione.
*Usando o If, ele entra e sai da funcao, não mantendo meu Led piscando e assim saindo da funcao. *Usando o While, ele entra na funcao mas por outro lado nao consigo retornar para resgatar o valor do Y_out.
Quem tiver alguma sugestao para aprimorar minha funcao seria válido.
Comecei o projeto baseado neste link do Youtube: https://www.youtube.com/watch?v=Bys8pCuCDB8
/*
Arduino and ADXL345 Accelerometer Tutorial
by Dejan, https://howtomechatronics.com
*/
#include <Wire.h> // Wire library - used for I2C communication
int ADXL345 = 0x53; // The ADXL345 sensor I2C address
float X_out, Y_out, Z_out; // Outputs
void setup() {
Serial.begin(9600); // Initiate serial communication for printing the results on the Serial monitor
Wire.begin(); // Initiate the Wire library
// Set ADXL345 in measuring mode
Wire.beginTransmission(ADXL345); // Start communicating with the device
Wire.write(0x2D); // Access/ talk to POWER_CTL Register - 0x2D
// Enable measurement
Wire.write(8); // (8dec -> 0000 1000 binary) Bit D3 High for measuring enable
Wire.endTransmission();
delay(100);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
}
void loop() {
// === Read acceleromter data === //
Wire.beginTransmission(ADXL345);
Wire.write(0x32); // Start with register 0x32 (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(ADXL345, 6, true); // Read 6 registers total, each axis value is stored in 2 registers
X_out = ( Wire.read()| Wire.read() << 8); // X-axis value
X_out = X_out/256; //For a range of +-2g, we need to divide the raw values by 256, according to the datasheet
Y_out = ( Wire.read()| Wire.read() << 8); // Y-axis value
Y_out = Y_out/256;
Z_out = ( Wire.read()| Wire.read() << 8); // Z-axis value
Z_out = Z_out/256;
Serial.print("Xa= ");
Serial.print(X_out);
Serial.print(" Ya= ");
Serial.print(Y_out);
Serial.print(" Za= ");
Serial.println(Z_out);
digitalWrite(8,HIGH);
/*COMMENT*/
while (X_out > 0.3) {
keepRightBlink();
}
while (X_out < -0.3) {
keepLeftBlink();
if(Y_out > -0.3){
return ;
}
}
}
int keepRightBlink(){
digitalWrite(7, HIGH);
delay(100);
digitalWrite(7, LOW);
delay(100);
}
int keepLeftBlink(){
digitalWrite(9, HIGH);
delay(100);
digitalWrite(9, LOW);
delay(100);
}