Salve a tutti,
Sto realizzando un sistema con arduino che mi faccia accendere dei led e mi controlli la velocità di un motore e me la stampi.Tutto ovviamente controllato da una GUI.
Per quanto riguarda arduino tutto funziona perfettamente credo.Seguendo un tutorial esterno son riuscito a far visualizzare la velocità del motore in mph da terminale.La lettura viene effettuata con un magnete che chiude il segnale ogni qualvolta il magnete applicato sul motore passa sul sensore.Ora il mio problema e farla visualizzare da gui.Vi posto i miei due sorgenti:Il primo di arduino il secondo della GUI in Processing.
Arduino Code
const int pinled=13;
int val;
#define reed A0//pin connected to read switch
//storage variables
int reedVal;
long timer;// time between one full rotation (in ms)
float mph;
float radius = 13.5;// tire radius (in inches)
float circumference;
int maxReedCounter = 100;//min time (in ms) of one rotation (for debouncing)
int reedCounter;
void setup(){
Serial.begin(9600);
pinMode(pinled, OUTPUT);
pinMode(0, INPUT);
reedCounter = maxReedCounter;
circumference = 2*3.14*radius;
pinMode(reed, INPUT);
// TIMER SETUP- the timer interrupt allows precise timed measurements of the reed switch
//for more info about configuration of arduino timers see http://arduino.cc/playground/Code/Timer1
cli();//stop interrupts
//set timer1 interrupt at 1kHz
TCCR1A = 0;// set entire TCCR1A register to 0
TCCR1B = 0;// same for TCCR1B
TCNT1 = 0;
// set timer count for 1khz increments
OCR1A = 1999;// = (1/1000) / ((1/(16*10^6))*8) - 1
// turn on CTC mode
TCCR1B |= (1 << WGM12);
// Set CS11 bit for 8 prescaler
TCCR1B |= (1 << CS11);
// enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);
sei();//allow interrupts
//END TIMER SETUP
}
ISR(TIMER1_COMPA_vect) {//Interrupt at freq of 1kHz to measure reed switch
reedVal = digitalRead(reed);//get val of A0
if (reedVal){//if reed switch is closed
if (reedCounter == 0){//min time between pulses has passed
mph = (56.8*float(circumference))/float(timer);//calculate miles per hour
timer = 0;//reset timer
reedCounter = maxReedCounter;//reset reedCounter
}
else{
if (reedCounter > 0){//don't let reedCounter go negative
reedCounter -= 1;//decrement reedCounter
}
}
}
else{//if reed switch is open
if (reedCounter > 0){//don't let reedCounter go negative
reedCounter -= 1;//decrement reedCounter
}
}
if (timer > 2000){
mph = 0;//if no new pulses from reed switch- tire is still, set mph to 0
}
else{
timer += 1;//increment timer
}
}
void displayMPH(){
Serial.println(mph);
}
void loop()
{
if(Serial.available() >0)
{
val= Serial.read();
if(val == 'S')
{
digitalWrite(pinled, HIGH);
delay(1000);
digitalWrite(pinled, LOW);
}else if(val == 'N')
{
digitalWrite(pinled, HIGH);
}else if(val=='Z')
{
digitalWrite(pinled, LOW);
}
}
//print mph once a second
displayMPH();
delay(2000);
}
GUI Code
import processing.serial.*;
int width = 800;
int height = 600;
int button1X;
int button1Y;
int button1Width = width/10;
int button1Height = height/20;
int button2X;
int button2Y;
int button3X;
int button3Y;
int button2Width = button1Width;
int button2Height = button1Height;
int button3Width = button1Width;
int button3Height = button1Height;
float buttonFont = button1Height/1.5;
float wordFont = button1Height;
Serial arduino;
color buttonColor, buttonHighlight, buttonPressed;
PFont font;
boolean button1Over = false;
boolean button2Over = false;
boolean button3Over = false;
void setup() {
size(width,height);
smooth();
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;
arduino = new Serial(this,"COM3", 9600);
}
void draw() {
update(mouseX,mouseY);
background(0);
if(button1Over) {
fill(buttonHighlight);
} else {
fill(buttonColor);
}
stroke(0);
rect(button1X,button1Y, button1Width, button1Height);
if(button2Over) {
fill(buttonHighlight);
} else {
fill(buttonColor);
}
stroke(0);
rect(button2X,button2Y, button2Width, button2Height);
if(button3Over) {
fill(buttonHighlight);
} else {
fill(buttonColor);
}
stroke(0);
rect(button3X,button3Y, button3Width, button3Height);
textFont(font, buttonFont);
fill(0);
text("On t",button1X + button1Width/3.9,button1Y + button1Height/1.3);
text("On nt",button2X + button1Width/10.9,button1Y + button1Height/1.3);
text("Off nt",button3X + button1Width/10.9,button1Y + button1Height/1.3);
textFont(font, wordFont);
fill(255);
text("Accendi/Spegni Led ",button3X + button1Width + width/20,button1Y + button1Height/1.2);
}
///////////////////////////////////
void update(int x, int y) {
if(overButton(button1X,button1Y,button1Width,button1Height)){
button1Over = true;
} else {
button1Over = false;
}
if(overButton(button2X,button2Y,button2Width,button2Height)){
button2Over = true;
} else {
button2Over = false;
}
if(overButton(button3X,button3Y,button3Width,button3Height)){
button3Over = true;
} else {
button3Over = false;
}
}
void mousePressed() {
if(button1Over) {
println("Button1 pressed");
arduino.write('S');
fill(buttonPressed);
rect(button1X,button1Y, button1Width, button1Height);
}
if(button2Over) {
println("Button2 pressed");
arduino.write('N');
fill(buttonPressed);
rect(button2X,button2Y, button2Width, button2Height);
}
if(button2Over) {
println("Button3 pressed");
arduino.write('Z');
fill(buttonPressed);
rect(button3X,button3Y, button3Width, button3Height);
}
}
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;
}
}
Sarei grati se riuscireste a farmi visualizzare la velocità nella gui in modo live.Ringrazio tutti in anticipo