Switch State during setup with bounce.h

Hello,
i'm trying to put further my code by introduce a if statement in the void setup() {
In my case just check the position of a specific switch during each power booting sequence in a word in the setup.
But i'm using bounce.h library and i think this is the problem.
In my exercise i use a led pin 11 (on a teensy 2.0 board)

void setup() {
  Serial.begin(9600); Serial.println("   FINAL_V4  beta "); Serial.println();
  pinMode(1, INPUT_PULLUP);
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);     // CABLE JAUNE TOGGLE
  pinMode(5, INPUT_PULLUP);     // CABLE BLANC TOGGLE


  pinMode(11, OUTPUT);
  button3.update();
  if (button3.fallingEdge()) {
    digitalWrite(11, HIGH); 
  } 
}  // Fin de Setup

You don't need to debounce a one-time check of a switch state at boot time. Just set the correct pin mode and do a simple one time read.

Debouncing is something you cannot do in a single event. It is something that is done over time. The same is true for edge detection. If you read the button once you can detect whether it is HIGH or LOW. You have to read a button multiple times to tell you whether it has changed from HIGH to LOW.

In your setup you do not need debouncing. This is only necessary when you read a button many times and you want to make sure the buttons mechanical vibrations are not mistaken for multiple user pushes.

Just use a digitalRead() in an if() statement to read the button during boot. The user is required to push the button before rebooting. So by the time the controller runs trough the setup routine the button should not bounce anymore. If the user pushes the button "too late" it is his fault if the button is still bouncing.

OK
Thank you for your advices.. I keep it in mind :slight_smile:

Also I used :
if (digitalRead(3 == LOW))
instead of :
if (digitalRead(3) == LOW)

Everything is working fine now.
Regards

Hello again,

I'm building a little midi controller for iOS to send command on a light application Luminair.
(5x buttons and 4 potentiometers)
my code is finished but I would like to optimise it a bit more.
Could anyone give me some advices to improve my skills and save memory/space in the code ?

Regards
Bib

#include <Bounce.h>             // Library BOUNCE.h (EXISTE BOUNCE_2.h..)
#include <EEPROM.h>             // Library EEPROM pour TOGGLE OU MOMENTARY
const int channel = 1;          // the MIDI channel number to send messages

Bounce button1 = Bounce(1, 5);  // 5 = 5 ms debounce time
Bounce button2 = Bounce(2, 5);  // which is appropriate for good
Bounce button3 = Bounce(3, 5);  // quality mechanical pushbuttons
Bounce button4 = Bounce(4, 100);  // TOGGLE plus long
Bounce button5 = Bounce(5, 100);  // TOGGLE plus long

unsigned int ValuePOT1 = 0;                  // value read from the potentiometer
unsigned char MIDIValuePOT1 = 0;    // value en MIDI du POT1
unsigned char PourcentagePOT1 = 0;  // value en Pourcentage du MIDI du POT1
unsigned int ValuePOT2 = 0; 
unsigned char MIDIValuePOT2 = 0;
unsigned char PourcentagePOT2 = 0;

unsigned int ValueFAD1 = 0;                  // value read from the fader
unsigned char MIDIValueFAD1 = 0;
unsigned char PourcentageFAD1 = 0;
unsigned int ValueFAD2 = 0;  
unsigned char MIDIValueFAD2 = 0;
unsigned char PourcentageFAD2 = 0;

char previousPOT1 = -1;   // store previously sent values, to detect changes
char previousPOT2 = -1;
char previousFAD1 = -1;
char previousFAD2 = -1;
elapsedMillis msec = 0;

const bool adress = 0;                 // Adresse de EEPROM

void setup() {
  Serial.begin(9600); Serial.print("   FINAL_V5   //   ");
  pinMode(1, INPUT_PULLUP);
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);     // CABLE JAUNE TOGGLE
  pinMode(5, INPUT_PULLUP);     // CABLE BLANC TOGGLE

  if ((EEPROM.read(adress) == 0) && (digitalRead(3) == LOW)) {  // Question pour TOGGLE OU MOMENTARY de 4 et 5
   EEPROM.write(adress,1); }
  else if ((EEPROM.read(adress) == 1) && (digitalRead(3) == LOW)) {
  EEPROM.write(adress,0); }
 
  if (EEPROM.read(adress) == 1) { Serial.println("TOGGLE"); }
  else { Serial.println("MOMENTARY"); }
  Serial.println();
  
}  // Fin de Setup

void loop() {
  if (msec >= 20) {
    msec = 0;
  potentiometers();
  faders();
  } // Fin de if msec
  buttons();
 
  // MIDI Controllers should discard incoming MIDI messages.
  while (usbMIDI.read()) { // ignore incoming messages
    }  // Fin de while
}  // Fin de Loop


/////////////////////////////////////////////////////
                     //FONCTIONS//
/////////////////////////////////////////////////////

void potentiometers()    //////////////////////////////
{
  ValuePOT1 = analogRead(A0);
  MIDIValuePOT1 = ValuePOT1/8;      // 1024/8 = 128 parfait pour le MIDI moins de place...
    
  ValuePOT2 = analogRead(A1);
  MIDIValuePOT2 = ValuePOT2/8;
  
  if (MIDIValuePOT1 != previousPOT1) {
      usbMIDI.sendControlChange(11, MIDIValuePOT1, channel);
      previousPOT1 = MIDIValuePOT1;
      serial();    // a commenter pour le produit final
    }
    if (MIDIValuePOT2 != previousPOT2) {
      usbMIDI.sendControlChange(12, MIDIValuePOT2, channel);
      previousPOT2 = MIDIValuePOT2;
      serial();    // a commenter pour le produit final
    }
  }  // Fin de Potentiometers


  void faders()    //////////////////////////////
 {
  ValueFAD1 = analogRead(A2);
  MIDIValueFAD1 = ValueFAD1/8;

  ValueFAD2 = analogRead(A3);
  MIDIValueFAD2 = ValueFAD2/8;
  
    if (MIDIValueFAD1 != previousFAD1) {
      usbMIDI.sendControlChange(13, MIDIValueFAD1, channel);
      previousFAD1 = MIDIValueFAD1;
      serial();    // a commenter pour le produit final
    }
    if (MIDIValueFAD2 != previousFAD2) {
      usbMIDI.sendControlChange(14, MIDIValueFAD2, channel);
      previousFAD2 = MIDIValueFAD2;
      serial();    // a commenter pour le produit final
    }
  }  // Fin de Faders


  void buttons()    /////////////////////////////
{
  button1.update();
  button2.update();
  button3.update();
  button4.update();
  button5.update();

  if (button1.fallingEdge()) {        // Attendre la pression des butons
    usbMIDI.sendControlChange(1, 127, channel);  
  }
  if (button2.fallingEdge()) {
    usbMIDI.sendControlChange(2, 127, channel); 
  }
  if (button3.fallingEdge()) {
    usbMIDI.sendControlChange(3, 127, channel); 
  }
  if (button4.fallingEdge()) {
    usbMIDI.sendControlChange(4, 127, channel);  
  }
  if (button5.fallingEdge()) {
    usbMIDI.sendControlChange(5, 127, channel);  
  }

 if (button1.risingEdge()) {          // Attendre la remonté des boutons
    usbMIDI.sendControlChange(1, 0, channel);  
  }
  if (button2.risingEdge()) {
    usbMIDI.sendControlChange(2, 0, channel);  
  }
  if (button3.risingEdge()) {
    usbMIDI.sendControlChange(3, 0, channel); 
  }

if (EEPROM.read(adress) == 1) {                  // TOGGLE pour Luminair
  if (button4.risingEdge()) {                     
    usbMIDI.sendControlChange(4, 0, channel);  
  }
  if (button5.risingEdge()) {
    usbMIDI.sendControlChange(5, 0, channel);  
  }
  }  //  Fin de if TOOGLE OU MOMENTARY 
  }  // Fin de buttons


  
  void serial()    //////////////////////////////  POUR DEBUG UNIQUEMENT
  {
  PourcentagePOT1 = map(MIDIValuePOT1, 0, 127, 0, 100);
  Serial.print("   POT1 = ");      Serial.print(ValuePOT1);
  Serial.print("   //   "); 
  Serial.print(MIDIValuePOT1);     Serial.print("   //   "); 
  Serial.print(PourcentagePOT1);   Serial.println("%");               
    
  PourcentagePOT2 = map(MIDIValuePOT2, 0, 127, 0, 100);
  Serial.print("   POT2 = ");      Serial.print(ValuePOT2);
  Serial.print("   //   ");
  Serial.print(MIDIValuePOT2);     Serial.print("   //   "); 
  Serial.print(PourcentagePOT2);   Serial.println("%");

  PourcentageFAD1 = map(MIDIValueFAD1, 0, 127, 0, 100);
  Serial.print("   FAD1 = ");      Serial.print(ValueFAD1);
  Serial.print("   //   ");   
  Serial.print(MIDIValueFAD1);     Serial.print("   //   "); 
  Serial.print(PourcentageFAD1);   Serial.println("%");     

  PourcentageFAD2 = map(MIDIValueFAD2, 0, 127, 0, 100);
  Serial.print("   FAD2 = ");      Serial.print(ValueFAD2);
  Serial.print("   //   ");   
  Serial.print(MIDIValueFAD2);     Serial.print("   //   "); 
  Serial.print(PourcentageFAD2);   Serial.println("%");
  Serial.println("");
} // Fin de Serial

Here are a few things I would do to make your code more readable.

  • remove numbers in your code and replace them with CONSTANT_NAMES e.g.

digitalRead(3) -> digitalRead( START_BUTTON_PIN );
analogRead(A0); -> replace A0 its a number too, make it application specific, then you can change pins easier

  • use naming schemes and better names

EEPROM.read(adress)

address is a const so should be all CAPTIAL_WITH_UNDERSCORE and a better name (what does this value represent) e.g.

EEPROM.read(adress) == 0
EEPROM.read( EE_MODE ) == MODE_TYPE_FAST (use enums to create these, all members should have the same name structure)

enum MODE_TYPE {MODE_TYPE_FAST, MODE_TYPE_SLOW};

serial() - what does this function do? You only know when looking at the function source. Could be printPotentiometerValues() and printFaderValues().

  • use English for comments and everything else (I am not a native English speaker)

  • use Tools -> "Auto Format" to format your code.

This configuration file contains a selection of the available options provided by the formatting tool "Artistic Style"

Artistic Style

If you wish to change them, don't edit this file.

Instead, copy it in the same folder of file "preferences.txt" and modify the copy. This way, you won't lose your custom formatter settings when upgrading the IDE

If you don't know where file preferences.txt is stored, open the IDE, File -> Preferences and you'll find a link

  • if you want to use variables in only one function but need them to for the next time, declare them inside the function and use the static keyword. Makes it easier to copy the code somewhere else and reduces the number of global variables at the beginning of the code.

void potentiometers()
{
static char previousPOT1 = -1;

}

  • Avoid global variables when possible. Local variables use the stack or just the processor registers. This is faster and uses less globally allocated RAM.

ValuePOT1 = analogRead(A0);
MIDIValuePOT1 = ValuePOT1/8;

You only use this value one more time to print it in serial. Create a function that prints it for debug.

unsigned int ValuePOT1 = analogRead(A0);
MIDIValuePOT1 = ValuePOT1/8;

printPotentiometerValue( ValuePOT1 );

Hope this is useful.

Thank you very much Klaus_K.
I have made some changes with your advice.
And keep in mind for the next projects..
Regards
Bib