Bounce2 query

Current project is a starter for a 3 cylinder 4 stroke engine we are building using a brushless motor.

I found some detail on how to code for a brushless controller using a potentiometer and I modified it to use a push button instead.

I have a query regarding the library Bounce2.

When I write the code and I add #include <Bounce2.h> the Bounce2 section does not change colour as does the Servo in #include <Servo.h>

It compiles ok and running the program on just the serial monitor appears all ok.

I have yet to connect the speed control etc. hardware to do a final check but just thought I’d ask.
Included the code and the circuit diagram below.

#include <Servo.h>//Using servo library to control ESC
    #include <Bounce2.h> // using bounce2 library for push button switch
    Servo esc; //Creating a servo class with name as esc
    int startButton = 2;
    int readyLed = 3; //LED on when ESC is armed ...perhaps make it a flashing type LED
    int buttonVal = 0;  //variable to store button value
    int startTransistor = 10; //transistor which feeds 5v supply to top of A0 resistor when start button is pressed (for maximum speed)

    Bounce debouncer = Bounce();
    
    void setup()
    {
      pinMode(startButton,INPUT_PULLUP);
      debouncer.attach(startButton);
      debouncer.interval(5);
      pinMode(readyLed,OUTPUT);
      pinMode(startTransistor,OUTPUT);
      esc.attach(9); //Specify the esc signal pin,Here as D8
      esc.writeMicroseconds(1000); //initialize the signal to 1000
      Serial.begin(9600);
    }
    void loop()
    {
      buttonVal = digitalRead(startButton);
      if (buttonVal == HIGH){
        digitalWrite(startTransistor,HIGH);
        digitalWrite(readyLed,HIGH);
        }
        else {
          digitalWrite(startTransistor,LOW);
          digitalWrite(readyLed,LOW);
          }
      int val; //Creating a variable val
      val= analogRead(A0); //Read input from analog pin a0 and store in val
      val= map(val, 0, 1023,1000,2000); //mapping val to minimum and maximum(Change if needed) 
      esc.writeMicroseconds(val); //using val as the signal to esc
      Serial.println (val);  //comment out later
      delay (1000); //comment out later
    }

The changing of colour depends on what keywords the library author put in the keywords file and is of no importance afaik.

In fact I've seen a few complaints about the way it works. If "foo" is a keyword in the library "bar", if you use "foo" in your sketch it changes colour even if you didn't include "bar" and use "foo" in a totally unrelated context.

So ignore the colours afaics.

Is there a keywords.txt file in your library folder? That is the file which tells the Arduino text editor to colour certain words a certain way.

Also, did you know your debouncer is actually doing nothing in the code you posted? When using that library, you don't do digitalRead on the pins directly.

saximus:
When using that library, you don't do digitalRead on the pins directly.

Not the friendliest way to do things. It would be nice if it simply had a debounced equivalent of digitalRead(), say debouncedRead()...

It basically does. Maybe try reading the documentation and examples that come with it.

saximus:
It basically does.

No, it doesn't. I don't call the whole mydb.attach, mydb.update and mydb.read an equivalent of debouncedRead(mypin). It's quite cumbersome.

saximus:
Maybe try reading the documentation and examples that come with it.

The date comment in my oldest Bounce2 sketch is July 2014: I've been using it since then, which is when I read the docs, so I know exactly how it works.

That said it works perfectly and I use it quite often; it's just a bit of a pita, and is probably why OP disn't use it properly.

Hmm apologies, I just had a closer look and realised I modified the constructor a long time ago so there would be no need for attach() or interval(). So yeah I guess at some point I decided that was a pain to remember as well.

Although I still think that, apart from the addition of update() , it's about as close to a debounced equivalent of digitalRead() as I have ever seen.

I wonder if it would be possible to incorporate timer interrupts into the library to remove the need for update() as well...

I looked at an example on Github and modified the first line in the loop to what was shown there.

Could someone take a peek and advise if correct?

Also I looked in the keywords.txt file in the library for the bounce2.
It had some text however mostly it was hash tags.

All still working ok as far as the serial monitor is concerned but as I said, still need to connect it to the speed control etc.

#include <Servo.h>//Using servo library to control ESC
    #include <Bounce2.h> // using bounce2 library for push button switch
    Servo esc; //Creating a servo class with name as esc
    int startButton = 2;
    int readyLed = 3; //LED on when ESC is armed ...perhaps make it a flashing type LED
    int buttonVal = 0;  //variable to store button value
    int startTransistor = 10; //transistor which feeds 5v supply to top of A0 resistor when start button is pressed (for maximum speed)

    Bounce debouncer = Bounce();
    
    void setup()
    {
      pinMode(startButton,INPUT_PULLUP);
      debouncer.attach(startButton);
      debouncer.interval(5);
      pinMode(readyLed,OUTPUT);
      pinMode(startTransistor,OUTPUT);
      esc.attach(9); //Specify the esc signal pin,Here as D8
      esc.writeMicroseconds(1000); //initialize the signal to 1000
      Serial.begin(9600);
    }
    void loop()
    {
      debouncer.update();
      buttonVal = debouncer.read();
      if (buttonVal == HIGH){
        digitalWrite(startTransistor,HIGH);
        digitalWrite(readyLed,HIGH);
        }
        else {
          digitalWrite(startTransistor,LOW);
          digitalWrite(readyLed,LOW);
          }
      int val; //Creating a variable val
      val= analogRead(A0); //Read input from analog pin a0 and store in val
      val= map(val, 0, 1023,1000,2000); //mapping val to minimum and maximum(Change if needed) 
      esc.writeMicroseconds(val); //using val as the signal to esc
      Serial.println (val);  //comment out later
      //delay (1000); //comment out later
    }

Recently made some query regarding the use of Bounce2 .

Someone gave a reason why the Bounce2 in … #include<Bounce2.h> did not highlight the keyword as it does in say #include<Servo.h>

The idea of the highlight I thought was to ensure correct syntax.

As shown in the attached pics, I found the library listed in my IDE and if I go to Library management it is also listed there as “Installed”.

However, if i go to C:\Program Files\Arduino\libraries it is not included there as far as I can see.

So would the library actually be installed correctly?

There were doubts as to my program initially working properly as I had made errors in the beginning of the “loop” . These I corrected as far as I was able to ascertain as the program is working ok in the serial monitor at least.

As I cannot seem to look into the installed folder for Bounce2 I am unsure of why the keyword.txt does not highlight the colour and therefore if it is correctly installed.

Edit:- I just found these libraries in a different location. I am guessing these would have been added libraries. So should they be moved into the main arduino library folder?

Edit:- I just found these libraries in a different location. I am guessing these would have been added libraries. So should they be moved into the main arduino library folder?

No.

There are two locations for library files. On is for the libraries packaged with the ide. The other one if for libraries you have added. The reason for the second folder, is that if you update the ide, the libraries you added will not be lost when the ide libraries folder is updated with the currently packaged libraries.

Ok. So any idea why the keyword does not highlight and from the code supplied, does it appear to be correct?

Which keywords in your sketch are not highlighting when viewed in the serial monitor of the ide.?

bluejets:
Ok. So any idea why the keyword does not highlight

Are you saying that you have seen certain words, foo and bar say, in the Bounce2 keywords file and they’re in your sketch but not highlighting?

My Bounce2 library doesn’t even have a keywords file.

It’s entirely up to the library author if there’s a keyword file, and if there is, what’s in it.

And as I said earlier, if you happen to use a word in your sketch that’s in a keyword file from a library that’s installed but not #include-d in the current sketch, it will highlight. Keywords in Arduino IDE basically mean nothing.

cattledog:
Which keywords in your sketch are not highlighting when viewed in the serial monitor of the ide.?

I’m not viewing keywords in the serial monitor, just the change in analog input value.
The part I was referring to is as I said in my opening query

" When I write the code and I add #include <Bounce2.h> the Bounce2 section does not change colour as does the Servo in #include <Servo.h>"

If it is regarded as unimportant as the revised code picks up the switch closure, then I guess I’ll just live with it the way it is.

I just needed to be certain it was not some kind of error.

I’ve compared it to the keyword txt in the servo library and , other than the bounce2 library being an add on in a different folder and location, it is the same layout.