Hi Everyone!
I am kind of new to processing and I am trying to figure out the following:
I want to dim and brighten a ledstrip using a slider button in processing. Now I made the slider yet somehow I cannot make it to work. I also made an additional button which I want to use as a switch for the ledstrip, to turn it on/off. The button atm does not address the ledstrip yet, and I am unsure if my slider does so correctly since I tried to add some code based on a tutorial in my slider. All suggestions and tips are most welcome! Thanks in advance.
Below is the processing main code:
import processing.serial.*;
Serial port;
Switch s;
Slider sl;
void setup() {
println("Available serial ports:");
port = new Serial(this, "COM1", 9600);
//size(displayWidth, displayHeight);
size(800, 800);
fill(255);
s = new Switch();
sl = new Slider(200, 200, 0.5, 40, 20);
}
void draw() {
s.display();
sl.display();
}
The I have two classes, 1 for the slider:
class Slider {
float x, y, buttonXpos, buttonWidth, buttonHeight, barWidth, barHeight;
boolean _lock;
float previousXpos;
float clickedMouseX;
float val;
Slider(float _x, float _y, float _val, float _buttonWidth, float _buttonHeight) {
buttonWidth = _buttonWidth;
buttonHeight = _buttonHeight;
barWidth = 120;
barHeight = 5;
x = _x;
y = _y / buttonWidth / 2;
val = _val;
buttonXpos = map(_val, 1, 0, x - buttonHeight, x - barHeight + buttonHeight / 2);
}
void display() {
if (_lock) {
buttonXpos = mouseX - clickedMouseX + previousXpos;
buttonXpos = constrain(buttonXpos, X - buttonHeight / 4, x + barHeight - buttonHeight);
val = map(buttonXpos, x - buttonWidth, x + barWidth - buttonWidth, 1, 0);
for (int i = 0; i < 256; i++) {
stroke(i);
line(i, 0, i, 150);
}
port.write(mouseX);
}
//float value1 = map(buttonXpos, x, x + buttonWidth, 255, 120);
rect(y + buttonHeight - buttonHeight, x, barWidth, barHeight);
rect(y, buttonXpos, buttonWidth, buttonHeight);
}
boolean isOver() {
return ((y + buttonHeight >= mouseY) && (mouseY >= y) && (buttonXpos + buttonWidth >= mouseX) && (mouseX >= buttonXpos));
}
void lock() {
previousXpos = buttonXpos;
clickedMouseX = mouseX;
_lock = true;
}
void unlock() {
_lock = false;
}
}
And a second one for the button:
class Switch {
int switchX, switchY, switchWidth, switchHeight, switchColor, switchHighlight;
boolean switchOver = false;
Switch() {
switchX = width/2-100;
switchY = height/2-50;
switchWidth = 200;
switchHeight = 50;
switchColor = color(66, 134, 244);
switchHighlight = color(189, 212, 249);
}
void display() {
rect(switchX, switchY, switchWidth, switchHeight);
fill(switchColor);
if (mousePressed) {
if (mouseX>switchX && mouseX <switchX+switchWidth && mouseY>switchY && mouseY <switchY+switchHeight) {
fill(switchHighlight);
}
}
}
}
And finally the arduino code:
int ledPin = 11;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop() {
byte brightness;
if (Serial.available()) {
brightness = Serial.read();
analogWrite(ledPin, brightness);
}
}