I've been trying to this for a while now, I want esp32 to get some info that is running in the i2c communication after I press and release a button, since if I just use:
//exemple
if(button == HIGH){
GetInfo();
}
the code will get the info for as long as i am pressing the button,it doesnt work very well cause it might get more data than I need since a wrote it to get 1024 bytes of data every press.
I also tried like this, but it didnt work too:
bool PulseSignal = false;
void loop(){
if(digitalRead(button) == HIGH){
PulseSignal == !PulseSignal;
}
if(PulseSignal == true){
Serial.println("catching RAW msg...");
myGNSS.checkUblox(1024); // Check for the arrival of new data and process it.
PulseSignal == !PulseSignal; //change back to false, so I have to press again.
}
while (myGNSS.fileBufferAvailable() >= sdWriteSize) // Check to see if we have at least sdWriteSize waiting in the buffer
{
digitalWrite(LED, HIGH); // Flash LED_BUILTIN each time we write to the SD card
myGNSS.extractFileBufferData(myBuffer, sdWriteSize); // Extract exactly sdWriteSize bytes from the UBX file buffer and put them into myBuffer
myFile.write(myBuffer, sdWriteSize);
bytesWritten += sdWriteSize; // Update bytesWritten
// In case the SD writing is slow or there is a lot of data to write, keep checking for the arrival of new data
myGNSS.checkUblox(); // Check for the arrival of new data and process it.
digitalWrite(LED, LOW); // Turn LED_BUILTIN off again
}
Need to store previous button status, something like this (untested) ...
bool lastPulseSignal = false;
bool PulseSignal = false;
void loop() {
if (digitalRead(button) == HIGH) {
PulseSignal == true;
} else {
PulseSignal == false;
lastPulseSignal == false; // ready to check for next press
}
if (PulseSignal && !lastPulseSignal) {
Serial.println("catching RAW msg...");
myGNSS.checkUblox(1024); // Check for the arrival of new data and process it (only once).
lastPulseSignal == true; // Stop checking for new data until button is released then pressed again.
}
while (myGNSS.fileBufferAvailable() >= sdWriteSize) // Check to see if we have at least sdWriteSize waiting in the buffer
{
digitalWrite(LED, HIGH); // Flash LED_BUILTIN each time we write to the SD card
myGNSS.extractFileBufferData(myBuffer, sdWriteSize); // Extract exactly sdWriteSize bytes from the UBX file buffer and put them into myBuffer
myFile.write(myBuffer, sdWriteSize);
bytesWritten += sdWriteSize; // Update bytesWritten
// In case the SD writing is slow or there is a lot of data to write, keep checking for the arrival of new data
myGNSS.checkUblox(); // Check for the arrival of new data and process it.
digitalWrite(LED, LOW); // Turn LED_BUILTIN off again
}
You need button debouncing. The easiest way (if you don`t want to write code for debouncing) is to use ezButton library. Also google for hardware debouncing, sometimes one small capacitor could fix things..
It depends if you want the button to have just one function I.è a short push. You can also code a long push and have a different function. I like to separate my button functions and then you can set the state of the button and use that in the main loop to trigger your event
I actually decided to do as you mentioned, since I need a funtion to close the sd card anyway.
So im doing short press to catch data, and long press to stop logging data and close sd card.
When I make all this actually work nice i'll do some leds indicating long presses, just like dji does with their drones powering on
Adding a debouncer library would be an easy solution. Also, it can greatly simplify your code. This example provides a robust debouncer by default, and the button part of your code would look like this ...
#include <Toggle.h>
const byte buttonPin = 2;
Toggle button(buttonPin);
void setup() {
button.begin(buttonPin);
}
void loop() {
button.poll();
if (button.onPress()) {
Serial.println("catching RAW msg...");
//myGNSS.checkUblox(1024); // Check for the arrival of new data and process it (only once).
}
}