Hi, I`m a newbee. I would like create an light adjustment by using the Bela Trill Bar. Basically it should have following function:
- If I touch the bar with one finger under 0,5 seconds the LED should turn ON and OFF
- If I touch more than 0,5 seconds with one finger the LED should dimm form 0 to 255
- If I touch again more than 0,5 seconds with one finger the LED should dimm form 255 to 0
- If I touch and move one finger LEDPin"blue" should dimming up and LEDPin"yellow" should dimmind down
- 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);
}
*/
}