Salve a tutti,
Ho realizzato. anche con l'aiuto di lesto, un un codice per arduino + GUI Processing che mi permette di visualizzare la velocità di un motore, il numero delle volte in cui si e chiuso un pulsante e mi permette anche l'attivazione di alcuni relè.Tutto ovviamente gestito da grafica.Il tutto funziona anche se noto una lentezza.
Quando provo ad attivare un relè, il comando viene eseguito da arduino dopo alcuni secondi e non subito.Come potrei fare per velocizzare?
I codici sono i seguenti:
Processing
// Example by Tom Igoe
import processing.serial.*;
//IMPOSTAZIONI GRAFICHE//
int width = 800;
int height = 600;
int statoluci= 0;
int statomot= 0;
int button1X;
int button1Y;
int button1Width = width/10;
int button1Height = height/20;
int button2X;
int button2Y;
int button3X;
int button3Y;
int button4X;
int button4Y;
int button2Width = button1Width;
int button2Height = button1Height;
int button3Width = button1Width;
int button3Height = button1Height;
int button4Width = button1Width;
int button4Height = button1Height;
float buttonFont = button1Height/1.5;
float wordFont = button1Height;
color buttonColor, buttonHighlight, buttonPressed;
PFont font;
boolean button1Over = false;
boolean button2Over = false;
boolean button3Over = false;
boolean button4Over = false;
int velocita;
int pass;
//END//
Serial myPort; // The serial port
// The display font
String inString; // Input string from serial port
int lf = 10; // ASCII linefeed
void setup() {
size(width,height);
smooth();
// Make your own font. It's fun!
font = loadFont("CourierNew36.vlw");
buttonColor = color(255);
buttonHighlight = color(200);
buttonPressed = color(50);
button1X = width/10 - button1Width/2;
button1Y = height/5 - button1Height/2;
button2X = 2*width/10 - button1Width/2;
button2Y = height/5 - button1Height/2;
button3X = 3*width/10 - button1Width/2;
button3Y = height/5 - button1Height/2;
button4X = 3*width/10 - button1Width/2;
button4Y = height/5 - button1Height/2;
textFont(font, 18);
// List all the available serial ports:
//println(Serial.list());
// Open the port you are using at the rate you want:
myPort = new Serial(this, Serial.list()[0], 9600);
myPort.bufferUntil(lf);
}
void draw() {
background(200);
noFill();
stroke (255);
strokeWeight(2);
rect(width/25, height/13,280,100);
strokeWeight(2);
rect(width/2.3, height/13,405,300);
strokeWeight(2);
rect(width/25, height/3.5,280,175);
strokeWeight(2);
rect(width/25, height/1.6,720,175);
update(mouseX,mouseY);
if(button1Over) {
fill(buttonHighlight);
} else {
fill(buttonColor);
}
stroke(0);
rect(width/8,height/5.8, 132, button1Height);
if(button2Over) {
fill(buttonHighlight);
} else {
fill(buttonColor);
}
stroke(0);
rect(width/1.72,height/5.8, 132, button2Height);
if(button3Over) {
fill(buttonHighlight);
} else {
fill(buttonColor);
}
stroke(0);
rect(377,250, 134, button3Height);
if(button4Over) {
fill(buttonHighlight);
} else {
fill(buttonColor);
}
stroke(0);
rect(530,250, 185, button3Height);
textFont(font, buttonFont);
textFont(font, wordFont);
fill(0);
text("Controllo Luci",width/20,height/8);
fill(0);
text("Controllo Motore",width/1.9,height/8);
fill(0);
text("Produzione",width/20,height/3);
fill(0);
if(statoluci == 0)
{
text("Accendi",width/7.6,height/4.7);
}else if(statoluci == 1)
{
text("Spegni",width/7.6,height/4.7);
}
if(statomot == 0)
{
text("Accendi",width/1.7,button1Y + button1Height/1.3);
}else if(statomot == 1)
{
text("Spegni",width/1.7,button1Y + button1Height/1.3);
}
text("Velocità",width/1.7,height/2.9);
text("Aumenta",width/2.1,height/2.2);
text("Diminuisci",width/1.5,height/2.2);
textFont(font, wordFont);
fill(0);
// text("Accendi/Spegni Led ",button3X + button1Width + width/20,button1Y + button1Height/1.2);
fill(0);
text("Velocità: " + velocita, 40,248);
text("Carrelli: " + pass, 40,310);
}
void update(int x, int y) {
if(overButton(width/8,height/6,width/6,button1Height)){
button1Over = true;
} else {
button1Over = false;
}
if(overButton(462,height/6,135,button2Height)){
button2Over = true;
} else {
button2Over = false;
}
//
if(overButton(381,250,130,button3Height)){
button3Over = true;
} else {
button3Over = false;
}
if(overButton(530,250,180,button4Height)){
button4Over = true;
} else {
button4Over = false;
}
}
void mousePressed() {
if(button1Over) {
println("Button1 pressed");
if(statoluci == 0)
{
myPort.write('A');
statoluci=1;
}else if(statoluci == 1)
{
myPort.write('B');
statoluci=0;
}
fill(buttonPressed);
rect(width/8,height/5.9, width/6, button1Height);
}
if(button2Over) {
println("Button2 pressed");
if(statomot == 0)
{
myPort.write('C');
statomot=1;
}else if(statomot == 1)
{
myPort.write('D');
statomot=0;
}
fill(buttonPressed);
rect(465,height/5.8, 130, button2Height);
}
if(button3Over) {
println("Button3 pressed");
myPort.write('E');
fill(buttonPressed);
rect(381,250, 130, button3Height);
}
if(button4Over) {
println("Button4 pressed");
myPort.write('F');
fill(buttonPressed);
rect(530,250, 180, button4Height);
}
}
boolean overButton(int x, int y, int width, int height) {
if(mouseX >= x && mouseX <= x+width && mouseY >= y && mouseY <= y+height) {
return true;
} else {
return false;
}
}
void serialEvent(Serial p) {
inString = (myPort.readString());
int sensors[] = int(split(inString, ','));
velocita=sensors[0];
pass=sensors[1];
}