PWM and Touch Switch by using Bela Trill Bar

Hi, I`m a newbee. I would like create an light adjustment by using the Bela Trill Bar. Basically it should have following function:

  1. If I touch the bar with one finger under 0,5 seconds the LED should turn ON and OFF
  2. If I touch more than 0,5 seconds with one finger the LED should dimm form 0 to 255
  3. If I touch again more than 0,5 seconds with one finger the LED should dimm form 255 to 0
  4. If I touch and move one finger LEDPin"blue" should dimming up and LEDPin"yellow" should dimmind down
  5. Later I will add a second light which I want to control with two fingers - you can see in my code

Point 1 - works without the other parts
Point 2 and 3 - I have no idea
Point 4 - It works with only one LED but not with two LEDs in the opposite way. I tried a "!" infront of touchValue but it doesn`t work.

How can I merge these different tasks? Many thanks.

#include <Trill.h>

int blue = 6;
int yellow = 5;

Trill trillSensor;
boolean touchActive = false;

unsigned long touchTimestamp = 0;
bool ledState = false;

void setup() {
  // Initialise serial and touch sensor
  Serial.begin(115200);
  int ret = trillSensor.setup(Trill::TRILL_BAR);

 
  pinMode(blue,OUTPUT);
  pinMode(yellow,OUTPUT);

}

void loop() {
  // Read 20 times per second
  delay(50);
  trillSensor.read();


/*if (trillSensor.getNumTouches() == 1) {
      if (millis() - touchTimestamp >= 500) {
        // Toggle LED state
        ledState = !ledState;
        digitalWrite(blue, ledState);

        // Update touch timestamp
        touchTimestamp = millis();
     
      }
      delay(50);
    }

 if (trillSensor.getNumTouches() == 2) {
      if (millis() - touchTimestamp >= 500) {
        // Toggle LED state
        ledState = !ledState;
        digitalWrite(yellow, ledState);

        // Update touch timestamp
        touchTimestamp = millis();
      }
      delay(50);
    }

*/

if(trillSensor.getNumTouches() == 1){
    int touchValue = trillSensor.touchLocation(0)/255;
    int brightness1 = map(touchValue, 0, 255, 0, 255);
    int brightness2 = map(! touchValue, 0, 255, 0, 255);
      
    analogWrite(blue, brightness1);
    analogWrite(yellow, brightness2);  

    delay(100);        
  }

     
 /*
 if(trillSensor.getNumTouches() == 2){
    int touchValue = trillSensor.touchLocation(0)/255;
    int brightness = map(touchValue, 0, 255, 0, 255);
        
    analogWrite(yellow, brightness);
    delay(100);
  }
*/

}

You are mapping touchValue from 0-255 into 0-255 - the same range so this does nothing.
if you want the opposite brightness for #2, you would use 255 - brightness1

Many thanks for your fast reply. I'm afraid I don't get it. I added this, but the result is still the same.

int brightness1 = map(touchValue, 0, 255, 0, 255);
int brightness2 = map(touchValue, 255, brightness1, 255, brightness1);

OK. Later I tried this. Basically it works, but the brightness of the yellow LED is double as the blue one and only dimming down to 50%. Is there a way to get the blue LED also brighter and fix the dimming issue with the yellow LED?

int brightness1 = map(touchValue, 0, 255, 0, 255);
int brightness2 = 255 / brightness1;
               
analogWrite(blue, brightness1);
analogWrite(yellow, brightness2);  

First off, I told you this was a useless statement

It takes the value of touchValue in the range of 0-255 and maps it to the same range. Same as

int brightness1 = touchValue;

What is the range of touchValue? You should print it out so you know what sort of range you are getting (probably not 0-255, but something less) and then map that.

Why are you doing division? If you want the opposite effect, use subtractions

brightness2 = 255 - brightness1;

Also, you should know that map() does not constrain the input values to be in range. If, for example, touchValue = 1000, brightness1 would also be 1000 since it it outside the mapp()ed range.

If I were debugging this, I would print out the value of touchValue every time though loop() to see what range I was dealing with. AFTER that, start to figure out how to map it to 0-255 or 255-0.

int touchValue = trillSensor.touchLocation(0);
Serial.println(touchValue);
...

Wow, that was very helpful. Thanks a lot. Now it works very well :partying_face:

int brightness1 = map(touchValue, 0, 3200, 0, 255);
int brightness2 = 255 - brightness1;

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