trouble with a GUI program
in
Programming Questions
•
1 year ago
Hi all,
This is my first post and I'm relatively new to programming. I'm trying to write a program that will serve as a GUI for some projects that I want to do with my arduino. I started by writing a program that uses a graphical slider like on a mixing board to change a value. Right now, everything seems to set up right but when I click on my slider to change the value, it won't go any higher than 5%. Any help or advice would be much appreciated. Here is the code for my project:
slider vol; //slider that will theoretically modulate parameters sent to a
//digital potentiometer controlling volume on a guitar effects pedal
PFont f;
void setup(){
f= loadFont("AgencyFB-Reg-12.vlw");
textFont(f, 20);
size(500, 500);
vol= new slider("Volume", color(255,0,0),40, 40);
frameRate(30);
}
void draw(){
background(153);
if(mousePressed && vol.B.overRect(vol.B.buttonX, vol.B.buttonY, vol.buttonW, vol.buttonH)) {
vol.setBval(mouseY);
}
vol.display();
}
class slider{
String label;
color c;
float slotX;
float slotY;
float slotH= 100;
float slotW= 10;
float buttonW= 30;
float buttonH= 10;
float buttonVal;
button B;
slider(String name, color slideC, int slideX, int slideY){
label= name;
c= slideC;
slotX= slideX;
slotY= slideY;
B= this.new button();
buttonVal= 0;
}
void display(){
//slider label
fill(c);
textAlign(CENTER);
text(label, slotX + (slotW / 2), slotY - 10);
//slider slot position
stroke(255);
fill(100);
rect(slotX, slotY, slotW, slotH);
B.show();
textAlign(CENTER);
//displays buttonVal at the bottom of the slider
text(buttonVal + "%", slotX + slotW/2, slotY + slotH + 30);
}
//sets buttonVal to a value 0..100
void setBval(float val){
buttonVal= (slotY + slotH) - constrain(val, vol.slotY ,vol.slotY + vol.slotH);
}
//gets button value as a decimal percentage that can be used to determine button position
float conBval(){
float bPerc= buttonVal / 100;
return bPerc;
}
/**inner class button of class slider, tracks button position and value*/
class button{
float buttonX= slotX + (slotW / 2) - (buttonW / 2);
float buttonY= slotY + slotH - (buttonH / 2);
button(){
println("slot x = " + slotX + ", slotY is " + slotY);
println("buttonX = " + buttonX + ", buttonY = " + buttonY);
}
//displays button at necessary location
void show(){
float Y= (slotY + slotH) - conBval()*slotH - buttonH/2;
println("buttonVal= " + buttonVal + ", conBval() = " + conBval() + ", button Y pos = " + Y);
//setBval(Y);
stroke(c);
fill(c);
rect(buttonX, Y, buttonW, buttonH);
}
//determines whether mouse is over button
boolean overRect(float x, float y, float W, float H){
if (mouseX >= x && mouseX <= x + W &&
mouseY >= y && mouseY <= y + H){
return true;
} else {
return false;
}
}
}
}
This is my first post and I'm relatively new to programming. I'm trying to write a program that will serve as a GUI for some projects that I want to do with my arduino. I started by writing a program that uses a graphical slider like on a mixing board to change a value. Right now, everything seems to set up right but when I click on my slider to change the value, it won't go any higher than 5%. Any help or advice would be much appreciated. Here is the code for my project:
slider vol; //slider that will theoretically modulate parameters sent to a
//digital potentiometer controlling volume on a guitar effects pedal
PFont f;
void setup(){
f= loadFont("AgencyFB-Reg-12.vlw");
textFont(f, 20);
size(500, 500);
vol= new slider("Volume", color(255,0,0),40, 40);
frameRate(30);
}
void draw(){
background(153);
if(mousePressed && vol.B.overRect(vol.B.buttonX, vol.B.buttonY, vol.buttonW, vol.buttonH)) {
vol.setBval(mouseY);
}
vol.display();
}
class slider{
String label;
color c;
float slotX;
float slotY;
float slotH= 100;
float slotW= 10;
float buttonW= 30;
float buttonH= 10;
float buttonVal;
button B;
slider(String name, color slideC, int slideX, int slideY){
label= name;
c= slideC;
slotX= slideX;
slotY= slideY;
B= this.new button();
buttonVal= 0;
}
void display(){
//slider label
fill(c);
textAlign(CENTER);
text(label, slotX + (slotW / 2), slotY - 10);
//slider slot position
stroke(255);
fill(100);
rect(slotX, slotY, slotW, slotH);
B.show();
textAlign(CENTER);
//displays buttonVal at the bottom of the slider
text(buttonVal + "%", slotX + slotW/2, slotY + slotH + 30);
}
//sets buttonVal to a value 0..100
void setBval(float val){
buttonVal= (slotY + slotH) - constrain(val, vol.slotY ,vol.slotY + vol.slotH);
}
//gets button value as a decimal percentage that can be used to determine button position
float conBval(){
float bPerc= buttonVal / 100;
return bPerc;
}
/**inner class button of class slider, tracks button position and value*/
class button{
float buttonX= slotX + (slotW / 2) - (buttonW / 2);
float buttonY= slotY + slotH - (buttonH / 2);
button(){
println("slot x = " + slotX + ", slotY is " + slotY);
println("buttonX = " + buttonX + ", buttonY = " + buttonY);
}
//displays button at necessary location
void show(){
float Y= (slotY + slotH) - conBval()*slotH - buttonH/2;
println("buttonVal= " + buttonVal + ", conBval() = " + conBval() + ", button Y pos = " + Y);
//setBval(Y);
stroke(c);
fill(c);
rect(buttonX, Y, buttonW, buttonH);
}
//determines whether mouse is over button
boolean overRect(float x, float y, float W, float H){
if (mouseX >= x && mouseX <= x + W &&
mouseY >= y && mouseY <= y + H){
return true;
} else {
return false;
}
}
}
}
1