6 years, 9 months ago.

Problem with Capacitive Sensor

Hello! I'm trying to control the RGB led color with the keyboard. I'm having problems with this line:

red = 1.0 - touch;

Anyone knows why? Thank you!

Complete code:

  1. include "mbed.h"
  2. include "TSISensor.h"
  3. include "Serial.h"

Serial pc(USBTX, USBRX); tx, rx PwmOut red(PTB18); Led Red PwmOut green(PTB19); Led Green PwmOut blue(PTD1);Led Blue

int main(void) { TSISensor tsi; pc.baud(9600); pc.format(8,Serial::None,1);

char cor;

pc.printf("\nDigite a cor desejada (R,G,B) -> \r\n"); cor = pc.getc(); pc.printf("O Valor lido foi: \r\n"); pc.printf("%c \r\n",cor);

while (true) {

float touch =tsi.readPercentage(); pc.printf("Touch lido -> %f \r\n", touch); if(cor == 0x52) wait(0.5); { pc.printf("Entrei no R\r\n"); red = 1.0 - touch; green = 0.0; blue = 0.0; wait(0.5); } } }

1 Answer

6 years, 9 months ago.

Use <<code>> and <</code>> around your code to make it readable.

You mean why 1 minus the value is used? The RGB led on the KL25 (I assume you got that one) is connected as common anode. Or cathode. One of them, don't blame me for not being able to keep them apart. Anyway what is the case is that they are on with a logical '0' on the output, and off with a logical '1'. So if you output a 100% '1' time, it will actually be off. Thats why 1 minus the value is used, so when value is close to '1', the output is most of the time '0', which means the LED is mostly on.