Button update and SoftPWMSet incompatibility

Status
Not open for further replies.

Jibeji

Member
Hi,
I am just writing a small piece of code for 5 buttons and 5 LEDs. The idea is to have the LED lightned except when a button is pushed.

This works fine :

Code:
#include <Bounce.h>

const int NB_PADS = 5;
const int DEBOUNCE_TIME = 50;

Bounce buttons[NB_PADS] = { Bounce (0, DEBOUNCE_TIME), Bounce (1, DEBOUNCE_TIME), Bounce (2, DEBOUNCE_TIME), Bounce (3, DEBOUNCE_TIME), Bounce (4, DEBOUNCE_TIME) };

void setup() {
  for (int i = 0; i < NB_PADS; i++) {
    pinMode (i, INPUT_PULLUP);
  }
  for (int i = NB_PADS; i < NB_PADS+NB_PADS; i++) {
    pinMode (i, OUTPUT);
    digitalWrite(i, HIGH);    
  }
}

void loop() {
  for (int i = 0; i < NB_PADS + 1; i++) {
    buttons[i].update();
  }

  for (int i = 0; i < NB_PADS; i++) {
    if (buttons[i].fallingEdge()) {
      digitalWrite(i+NB_PADS, LOW);
    }
    else if (buttons[i].risingEdge()) {
      if (i != 3 && i != 4) {
      }
      digitalWrite(i+NB_PADS, HIGH);
    }
  }
}

Now, I would like to lighten the LEDs with SoftPWMSet so that I can change their intensity later.

I have tried that:

Code:
#include <Bounce.h>
#include <SoftPWM.h>

const int NB_PADS = 5;
const int DEBOUNCE_TIME = 50;
const int PWM = 255;

Bounce buttons[NB_PADS] = { Bounce (0, DEBOUNCE_TIME), Bounce (1, DEBOUNCE_TIME), Bounce (2, DEBOUNCE_TIME), Bounce (3, DEBOUNCE_TIME), Bounce (4, DEBOUNCE_TIME) };

void setup() {
  SoftPWMBegin();
  
  for (int i = 0; i < NB_PADS; i++) {
    pinMode (i, INPUT_PULLUP);
  }
  for (int i = NB_PADS; i < NB_PADS+NB_PADS; i++) {
    pinMode (i, OUTPUT);
    SoftPWMSet(i, PWM);    
  }
}

void loop() {
  for (int i = 0; i < NB_PADS + 1; i++) {
    buttons[i].update();
  }

  for (int i = 0; i < NB_PADS; i++) {
    if (buttons[i].fallingEdge()) {
      SoftPWMSet(i, 0);
    }
    else if (buttons[i].risingEdge()) {
      if (i != 3 && i != 4) {
      }
      SoftPWMSet(i, PWM);
    }
  }
}

This does not work, the LEDs are off.
The part of code which shuts the LEDs off is:
Code:
for (int i = 0; i < NB_PADS + 1; i++) {
    buttons[i].update();
  }
As soon as it is commented, the LEDs remain at PWM value declared in setup()
 
The part of code which shuts the LEDs off is:
Code:
for (int i = 0; i < NB_PADS + 1; i++) {
    buttons[i].update();
  }
As soon as it is commented, the LEDs remain at PWM value declared in setup()

You did not show your wiring, but from your code, Im going to assume that you have the 5 buttons wired to pins 0-4 & the 5 LEDs wired to pins 5-9.

I did not load your sketch into a Teensy, nor did I attempt to reproduce your hardware setup, but I noticed the following: the first "for" loop in your loop() function is incorrect. Your terminating conditional should only go to "i < NB_PADS" (just like all of the other loops). Try making it look like the following & report back whether or not it behaves any better:

Code:
  for (int i = 0; [COLOR="#FF0000"]i < NB_PADS[/COLOR]; i++) {
    buttons[i].update();
  }

Good luck & have fun !!

Mark J Culross
KD5RXT
 
Hi Mark,
Your assumption is correct :)
Here is the schematic. For information this will be a MIDI control surface, I have removed all the MIDI stuff for more clarity.

Sans titre.png


I did not load your sketch into a Teensy, nor did I attempt to reproduce your hardware setup, but I noticed the following: the first "for" loop in your loop() function is incorrect. Your terminating conditional should only go to "i < NB_PADS" (just like all of the other loops). Try making it look like the following & report back whether or not it behaves any better:

You are right! +1 has nothing to do here. Removing it makes the LEDs to remain illuminated.
However, it still doesn't work with the following code. Pushing a button does nothing (and MIDI control messages are not sent) as soon as SoftPWM is used, but works fine with digitalWrite.

Code:
#include <Bounce.h>
#include <SoftPWM.h>

const int NB_PADS = 5;
const int DEBOUNCE_TIME = 50;
const int PWM = 55;

Bounce buttons[NB_PADS] = { Bounce (0, DEBOUNCE_TIME), Bounce (1, DEBOUNCE_TIME), Bounce (2, DEBOUNCE_TIME), Bounce (3, DEBOUNCE_TIME), Bounce (4, DEBOUNCE_TIME) };

void setup() {
  SoftPWMBegin();

  for (int i = 0; i < NB_PADS; i++) {
    pinMode (i, INPUT_PULLUP);
  }
  for (int i = NB_PADS; i < NB_PADS+NB_PADS; i++) {
    pinMode (i, OUTPUT);
    //digitalWrite(i, HIGH);
    SoftPWMSet(i, PWM);
  }
}

void loop() {
  for (int i = 0; i < NB_PADS; i++) {
    buttons[i].update();
  }

  for (int i = 0; i < NB_PADS; i++) {
    if (buttons[i].fallingEdge()) {
      //digitalWrite(i, LOW);
      SoftPWMSet(i, 0);
    }
    else if (buttons[i].risingEdge()) {
      //digitalWrite(i, HIGH);
      SoftPWMSet(i, PWM);
    }
  }
}

73 ;)
 
@Jibeji - you made a new thread I see ...

T_3.2 has PWM driven Analog pins. I just wrote this on a T_4.1 for A9 or pin 23 the T_3.2 has as well.
Code:
void setup() {
	Serial.begin(115200);
	while (!Serial && millis() < 4000 );
	Serial.println("\n" __FILE__ " " __DATE__ " " __TIME__);
	analogWriteResolution(12);
}

elapsedMillis tLED;
int aVal=96;
void loop() {
	if (tLED > 100) {
		tLED = 0;
		analogWrite(23, aVal);
		aVal += 100;
		if (aVal>4096) aVal = 96;
	}
}

Will need to alter the way pins are accessed from #0 through #9 to #0 to #4 to perhaps #19 to #23 for PWM analog usage.
Indexed from an array :: int tenPins[10] = { 0,1,2,3,4,19,20,21,22,23 }
Then recode loops like : pinMode ( tenPins, INPUT_PULLUP);
 
In the loop you set the SoftPWM to the input pins and not the output pins. Will not give any light effects and most likely confuse the inputs also.
 
In the loop you set the SoftPWM to the input pins and not the output pins. Will not give any light effects and most likely confuse the inputs also.

Yes, like before in the other thread for the outputs:

for (int i = NB_PADS; i < NB_PADS+NB_PADS; i++) {

But - using analogWrite ... with those correct offsets ... should be more direct with hardware support instead of added software.
 
Status
Not open for further replies.
Back
Top