ezButton multiple buttons

I had assumed that this uses interrupts but it seems very slow to respond to a button press.
If serial.print is running it takes several lines of print before it responds.

EDIT
I have discovered a delay statement that was causing the problem.
Is there no way to interrupt the delay ? I am looking for an emergency stop type response.

Interrupts happen during a delay() call. If you post your code, people can help.
Serial also operates using interrupts, that is how each char is loaded into the Tx register and shipped out.

The library says it is non blocking , i wonder if that is the reason ?
I need to tidy up my code first, hopefully i can dispense with the delay,it is only used for an oled update,

Always

Show us a good schematic of your proposed circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.


In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.

Use the </> icon from the ‘reply menu’ to attach the copied sketch.

Depending on the seriousness of the emergency, it may not be wise to rely on any software provision for stopping.

I'd go for the giant button that interrupts all power going to the laser.

And that's the only kind of interrupt that is appropriate here.

a7

1 Like

Yes, in a fallback situation, always add none software solutions that work alongside with the controller.

Manual mode can save a lot of headaches.

It is Just the response i want.
Not actually safety critical in any way.
I am not sure how the ezButton library sets the priority of its interrupt, if that is the right terminology.

BTW, switches need only to be scanned every ~50ms and then, look for a switch change in state to determine what action should be executed.

Recently here some person known to be young(er) claimed that 4 ms was detectable lag.

Perhaps. But 50 ms may be an eternity nevertheless for some.

You can scan every N ms, you choose N, or you can look at the buttons at your loop rate, which we hope is fast, then if a button changes you can react "instantly". Then just ignore the button for the next 20 or 50 ms. Again, you choose. I've some crappy switches that can bounce for ages, but most of the time 20 ms is plnnty generous.

a7

Those commands seem to do nothing for me even if i use the menu option.
Even using this editor to reply to posts seems to cause glitches.

50ms is worst case.

A mechanical switch doesn’t need anything faster.

A reed switch which is absolutely *hit has (I’ve measured) bounces into 15ms.

Actually, I usually use 20ms in applications, never had problems.

Where 50ms scanning was used, that rate has always work.

For bouncing, yes. For percussionists expecting something to happen an eternity.

a7]

@Boardburner2, time to post your code instead of handwaving.

I sing out of tune :face_exhaling:

1 Like

OK you asked for it GRIN

/**************************************************************************
 This is an example for our Monochrome OLEDs based on SSD1306 drivers

 Pick one up today in the adafruit shop!
 ------> http://www.adafruit.com/category/63_98

 This example is for a 128x64 pixel display using I2C to communicate
 3 pins are required to interface (two I2C and one reset).

 Adafruit invests time and resources providing this open
 source code, please support Adafruit and open-source
 hardware by purchasing products from Adafruit!

 Written by Limor Fried/Ladyada for Adafruit Industries,
 with contributions from the open source community.
 BSD license, check license.txt for more information
 All text above, and the splash screen below must be
 included in any redistribution.
 **************************************************************************/

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "Adafruit_VEML6075.h"
#include <ezButton.h>
Adafruit_VEML6075 uv = Adafruit_VEML6075();

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library. 
// On an arduino UNO:       A4(SDA), A5(SCL)
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
// On an arduino LEONARDO:   2(SDA),  3(SCL), ...
#define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#define NUMFLAKES     10 // Number of snowflakes in the animation example

#define LOGO_HEIGHT   16
#define LOGO_WIDTH    16
ezButton button1(2);  // create ezButton object that attach to pin 2;
ezButton button2(3);  // create ezButton object that attach to pin 3;
ezButton button3(4);  // create ezButton object that attach to pin 4;
ezButton button4(5);  // create ezButton object that attach to pin 5;
ezButton button5(6);  // create ezButton object that attach to pin 6;
float dose_div;

void setup() {
  Serial.begin(9600);
  button1.setDebounceTime(50); // set debounce time to 50 milliseconds
  button2.setDebounceTime(50); // set debounce time to 50 milliseconds
  button3.setDebounceTime(50); // set debounce time to 50 milliseconds
  button4.setDebounceTime(50); // set debounce time to 50 milliseconds
  button5.setDebounceTime(50); // set debounce time to 50 milliseconds

  Serial.println("VEML6075 Full Test");
  if (! uv.begin()) {
    Serial.println("Failed to communicate with VEML6075 sensor, check wiring?");
  }
  Serial.println("Found VEML6075 sensor");

  // Set the integration constant
  uv.setIntegrationTime(VEML6075_100MS);dose_div=10;
  // Get the integration constant and print it!
  Serial.print("Integration time set to ");
  switch (uv.getIntegrationTime()) {
    case VEML6075_50MS: Serial.print("50"); break;
    case VEML6075_100MS: Serial.print("100"); break;
    case VEML6075_200MS: Serial.print("200"); break;
    case VEML6075_400MS: Serial.print("400"); break;
    case VEML6075_800MS: Serial.print("800"); break;
  }
  Serial.println("ms");

  // Set the high dynamic mode
  uv.setHighDynamic(true);
  // Get the mode
  if (uv.getHighDynamic()) {
    Serial.println("High dynamic reading mode");
  } else {
    Serial.println("Normal dynamic reading mode");
  }

  // Set the mode
  uv.setForcedMode(false);
  // Get the mode
  if (uv.getForcedMode()) {
    Serial.println("Forced reading mode");
  } else {
    Serial.println("Continuous reading mode");
  }

  // Set the calibration coefficients
  uv.setCoefficients(0, 0,  // UVA_A and UVA_B coefficients
                     0, 0,  // UVB_C and UVB_D coefficients
                     0, 0); // UVA and UVB responses

  //SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
   Serial.println(F("SSD1306 allocation failed"));
   for(;;); // Don't proceed, loop forever
  }

  // Show initial display buffer contents on the screen --
  // the library initializes this with an Adafruit splash screen.
  display.display();
  delay(2000); // Pause for 2 seconds

  // Clear the buffer
  display.clearDisplay();

  // display.display() is NOT necessary after every single drawing command,
  // unless that's what you want...rather, you can batch up a bunch of
  // drawing operations and then update the screen all at once by calling
  // display.display(). These examples demonstrate both approaches...

 //testdrawline();      // Draw many lines
 // testdrawrect();      // Draw rectangles (outlines)
 // testdrawchar();      // Draw characters of the default font

  display.setTextSize(3);      // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE); // Draw white text
  display.setCursor(0, 0);     // Start at top-left corner
  display.cp437(true);         // Use full 256 char 'Code Page 437' font

  // Not all the characters will fit on the display. This is normal.
  // Library will draw what it can and the rest will be clipped.


}


float UVA_Dose;
float UVB_Dose;
float INDEX;
float UVA_mW;
float UVB_mW;


//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void loop() {
  
  button1.loop(); // MUST call the loop() function first
  button2.loop(); // MUST call the loop() function first
  button3.loop(); // MUST call the loop() function first
  button4.loop(); // MUST call the loop() function first
  button5.loop(); // MUST call the loop() function first
  int btn1State = button1.getState();
  int btn2State = button2.getState();
  int btn3State = button3.getState();
  int btn4State = button4.getState();
  int btn5State = button5.getState();

    if(button1.isPressed())
    Serial.println("The button 1 is pressed");

  if(button1.isReleased()){
    Serial.println("The button 1 is released");
    // Code to initialise display for reading uv goes here

  }


  if(button2.isPressed())
    Serial.println("The button 2 is pressed");

  if(button2.isReleased()){
    Serial.println("The button 2 is released");
// Code to initialise display for dose reading goes here

  }
  if(button3.isPressed())
    Serial.println("The button 3 is pressed");

  if(button3.isReleased())
    Serial.println("The button 3 is released");

  if(button4.isPressed())
    Serial.println("The button 4 is pressed");

  if(button4.isReleased())
    Serial.println("The button 4 is released");

  if(button5.isPressed())
    Serial.println("The button 5 is pressed");

  if(button5.isReleased())
    Serial.println("The button 5 is released");






 Serial.print("Raw UVA reading:  "); Serial.println(String(uv.readUVA()));
 Serial.print("Raw UVB reading:  "); Serial.println(String(uv.readUVB()));
 Serial.print("UV Index reading: "); Serial.println(String(uv.readUVI()));


UVA_mW =((uv.readUVA())/1000);
UVB_mW =((uv.readUVB())/1000);
INDEX =((uv.readUVI()));



 if (UVA_mW>0.1) {
    UVA_Dose = UVA_Dose + UVA_mW/dose_div;//convert to millijoules
  } else {
    }

displayreading(UVA_mW,UVB_mW,INDEX);
//delay(1000);
//displaydose(UVA_Dose,UVB_Dose,'B');


 //Serial.print("UVA Dose:  ");Serial.println(UVA_Dose);
 //Serial.print("UVB Dose:  ");Serial.println(UVB_Dose);

}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

void displayreading(float UVA_mW,float UVB_mW,float INDEX) // Value is expected to be in mW per cm squared
{

  char  x;
  char  y;
  char  z;
  char  w;
  char charBuf[8];

  String stringOne = (String((UVA_mW)));
  stringOne.toCharArray(charBuf,8);
  w=stringOne[0];
  x=stringOne[1];
  y=stringOne[2];
  z=stringOne[3];
 
  display.setTextSize(2) ;
  display.setCursor(78,6);   
  display.write(w);
  display.setCursor(90,6);   
  display.write(x);
  display.write(y);
  display.setCursor(114,6);   
  display.write(z);
   


  stringOne = (String((UVB_mW)));
  stringOne.toCharArray(charBuf,8);
  w=stringOne[0];
  x=stringOne[1];
  y=stringOne[2];
  z=stringOne[3];
 
  display.setTextSize(2) ;
  display.setCursor(78,32);   
  display.write(w);
  display.setCursor(90,32);   
  display.write(x);
  display.write(y);
  display.setCursor(114,32);   
  display.write(z);







  display.setTextSize(1);  
  display.setCursor(80, 50);   
  display.write('m');
  display.setCursor(88,50);   
  display.write('W');
  display.setCursor(96, 50);   
  display.write('/');
 display.setCursor(104, 50);   
  display.write('c');
  display.setCursor(112,50);   
  display.write('m');
  display.setCursor(120, 50);   
  display.write('2');

 display.setTextSize(1);  
  display.setCursor(80, 23);   
  display.write('m');
  display.setCursor(88 ,23);   
  display.write('W');
  display.setCursor(96, 23);   
  display.write('/');
   display.setCursor(104,23);   
  display.write('c');
  display.setCursor(112,23);   
  display.write('m');
  display.setCursor(120, 23);   
  display.write('2');
 

   
  display.display();
  

}

void displaydose(float UVA_Dose,float UVB_Dose,char uv) //Dose is expected to be in millijoules
{

  char  v;
  char  w;
  char  x;
  char  y;
  char  z;
  char charBuf[8];

  String stringOne = (String(UVA_Dose));
  stringOne.toCharArray(charBuf,8);
  v=stringOne[0];
  w=stringOne[1];
  x=stringOne[2];
  y=stringOne[3];
  z=stringOne[4];


display.setTextSize(3) ;
  display.setCursor(6,32);   
  display.write(v);
  display.setCursor(26,32);   
  display.write(w);
  display.setCursor(46,32);  
  display.write(x);
  display.setCursor(66,32);   
  display.write(y);
  display.setCursor(86,32);   
  display.write(z);
  display.display();

void init_displaydose();{display.clearDisplay();
display.drawRect(0, 0, display.width(), display.height(), SSD1306_WHITE);


  display.setTextSize(2) ;

 display.setCursor(5, 6);   
  display.write('U');
  display.setCursor(20, 6);   
  display.write('V');

 
  display.setCursor(35, 6); 
  display.write('A');
 

  display.setCursor(50,6);   
  display.write(' ');
  display.setCursor(65, 6);   
  display.write('D');
  display.setCursor(80, 6);   
  display.write('O');
  display.display();
   display.setCursor(95, 6); 
  display.write('S');
  display.display();
 display.setCursor(110, 6); 
  display.write('E');
}





void init_displayreading(void);{

 display.clearDisplay();
 display.drawRect(0, 0, display.width(), display.height(), SSD1306_WHITE);
 //display.fillRect(64, 1,64,25, SSD1306_BLACK);

display.setTextSize(3) ;
  display.setCursor(5, 6);   
  display.write('U');
  display.setCursor(25, 6);   
  display.write('V');
  display.setCursor(46, 6);   
  display.write('A');
  
  display.setCursor(5, 34);   
  display.write('U');
  display.setCursor(25, 34);   
  display.write('V');
  display.setCursor(46, 34);   
  display.write('B');
  display.display();
}
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.