With the FeatherWing OLED (ID 4650) on a Feather M0 Express, I would like to get data from one of three different sensors and use a button to select which sensor to process and display. It seems like what I need is a way to know the "last button pushed" so that only those data will be processed and displayed each time through the loop function, so I don't have to keep pushing that button to update date and display.
The example code doesn't appear to do what I need. How do I do it??
"last button pushed" on FeatherWing OLED?
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- adafruit_support_bill
- Posts: 88096
- Joined: Sat Feb 07, 2009 10:11 am
Re: "last button pushed" on FeatherWing OLED?
Not sure what example code you are using. But all you need to do is declare a global variable for "LastButton" and assign a value to it whenever a button is pressed.
Since it is a global variable, it will retain that value on successive iterations of the loop - until you press a different button and assign it a different value.
Since it is a global variable, it will retain that value on successive iterations of the loop - until you press a different button and assign it a different value.
- dastels
- Posts: 15666
- Joined: Tue Oct 20, 2015 3:22 pm
Re: "last button pushed" on FeatherWing OLED?
For starters... are you using C++ or CircuitPython?
In either case you'll probably want to use the Debouncer library/class to detect the presses (i.e when the buttons go from not pressed to pressed).Then each time through the loop check them and save in a variable which one was just pressed (if none, leave the variable as it was) then go about reading & displaying the appropriate sensor as indicated by the variable.
Dave
In either case you'll probably want to use the Debouncer library/class to detect the presses (i.e when the buttons go from not pressed to pressed).Then each time through the loop check them and save in a variable which one was just pressed (if none, leave the variable as it was) then go about reading & displaying the appropriate sensor as indicated by the variable.
Dave
- earthres
- Posts: 221
- Joined: Fri May 28, 2021 10:48 am
Re: "last button pushed" on FeatherWing OLED?
Thanks for your help with this little project. I'm using a Feather M0 Express with a FeatherWing 128x64 OLED. Each button is intended to display data from one of three sensors. Here's some "template" code for using the three buttons to choose which sensor to display. It's not very fancy code and I haven't tried to debounce the buttons, but it works OK for my purpose.
Code: Select all
// ThreeButtonOLEDTemplate.ino, D. Brooks, September 2022
#include <SPI.h>
//#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>
Adafruit_SH1107 OLED = Adafruit_SH1107(64, 128, &Wire);
// Button definitions for Feather M0 Express:
#define BUTTON_A 9
#define BUTTON_B 6
#define BUTTON_C 5
byte LastButton;
void setup() {
Serial.begin(115200);
//Serial.println("Three-button OLED test");
delay(250); // wait for the OLED to power up
OLED.begin(0x3C, true); // Address 0x3C default
// Initially shows Adafruit spashscreen for 1 s.
OLED.display();
delay(1000);
OLED.clearDisplay(); OLED.display(); // Clear the buffer.
OLED.setRotation(1); //Display text horizontally.
pinMode(BUTTON_A, INPUT_PULLUP);
pinMode(BUTTON_B, INPUT_PULLUP);
pinMode(BUTTON_C, INPUT_PULLUP);
OLED.setTextSize(1); // size 2 will be bigger!
OLED.setTextColor(SH110X_WHITE); //Optional, as only white available.
}
void loop() {
if (LastButton==0) {
OLED.clearDisplay(); OLED.setCursor(0,0); OLED.display();
OLED.println("OLED data display");
OLED.println("Press button for 1 s to choose...");
OLED.println("A: ??");
OLED.println("B: ??");
OLED.println("C: ??");
OLED.display();
}
if (!digitalRead(BUTTON_A)) LastButton=1;
if (!digitalRead(BUTTON_B)) LastButton=2;
if (!digitalRead(BUTTON_C)) LastButton=3;
if(LastButton==1) {
// data processing code...
OLED.clearDisplay(); OLED.setCursor(0,0); OLED.display();
// Display Button A data here.
OLED.display();
}
if(LastButton==2) {
// data processing code...
OLED.clearDisplay(); OLED.setCursor(0,0); OLED.display();
// Display Button B data here.
OLED.display();
}
if (LastButton==3) {
// data processing code...
OLED.clearDisplay(); OLED.setCursor(0,0); OLED.display();
// Display Button C data here.
OLED.display();
}
delay(1000);
}
- adafruit_support_bill
- Posts: 88096
- Joined: Sat Feb 07, 2009 10:11 am
Re: "last button pushed" on FeatherWing OLED?
In this case, debouncing would serve no purpose. The display operations will likely take longer than any contact bounces are likely to last. And even if they didn't the bounce would just be read as another press of the same button.
Please be positive and constructive with your questions and comments.