salve a tutti sono lucio e partecipare a questo forum già da alcuni anni, vi ringrazio in anticipo per gli spunti e il sostegno di tutti.
sicuramente di encoder ed Arduino si è già scritto come ho visto, ma chiedo una mano per l'utilizzo di processing
come interfaccia.
il progetto si sviluppa con un encoder rotativo Ltd3806 600bm.
arduino uno r3
e poi dovrei avere un interfaccia grafica per la lettura dei dai.
con questo codice riesco credo a leggere la velocità di rotazione come serve a me.
pero per utilizzare processing andrebbe cambiato perché quando uso la funzione
di importazione seriale non ho nessun risultato .
vorrei realizzare un semplice grafico per ora.
ps:
togliendo RPM in serial print riesco ad avere una specie di grafico in processing, ma non fedele a quello che sto
cercando.
const int encoder_a = 2; // Pin 3
const int encoder_b = 3; // Pin 5
long encoder_pulse_counter = 0;
long direction = 1;
void encoderPinChangeA()
{
encoder_pulse_counter += 1;
direction = digitalRead(encoder_a) == digitalRead(encoder_b) ? -1 : 1;
}
void encoderPinChangeB()
{
encoder_pulse_counter += 1;
direction = digitalRead(encoder_a) != digitalRead(encoder_b) ? -1 : 1;
}
void setup()
{
Serial.begin(9600);
pinMode(encoder_a, INPUT_PULLUP);
pinMode(encoder_b, INPUT_PULLUP);
attachInterrupt(0, encoderPinChangeA, CHANGE);
attachInterrupt(1, encoderPinChangeB, CHANGE);
}
void loop()
{
long speed = encoder_pulse_counter/600.00*60; // For encoder plate with 1024 Pulses per Revolution
Serial.print(" ");
Serial.println(direction*speed);
encoder_pulse_counter = 0; // Clear variable just before counting again
delay(100);
}
in processing:
// Graphing sketch
import processing.serial.*;
Serial myPort; // The serial port
int xPos = 1; // horizontal position of the graph
float inByte = 0;
void setup () {
// set the window size:
size(900, 600);
println(Serial.list());
// Open whatever port is the one you're using.
myPort = new Serial(this, "/dev/tty.usbmodem14101", 9600);
// don't generate a serialEvent() unless you get a newline character:
myPort.bufferUntil('\n');
// set inital background:
background(0);
}
void draw () {
// draw the line:
stroke(0, 100, 255);
line(xPos, height, xPos, height - inByte);
// at the edge of the screen, go back to the beginning:
if (xPos >= width) {
xPos = 0;
background(0);
} else {
// increment the horizontal position:
xPos++;
}
}
void serialEvent (Serial myPort) {
// get the ASCII string:
String inString = myPort.readStringUntil('\n');
if (inString != null) {
// trim off any whitespace:
inString = trim(inString);
// convert to an int and map to the screen height:
inByte = float(inString);
println(inByte);
inByte = map(inByte, 0, 1023, 0, height);
}
}