1
click if you enjoy
import processing.serial.*;
Serial myPort; // Serial port object
int value1, value2; // Variables to store the values
void setup() {
// Adjust the following line to match your serial port
myPort = new Serial(this, "COM6", 9600);
// Open the serial port
myPort.bufferUntil('\n'); // Read until newline character
size(400,400);
}
void draw() {
// Empty draw function
background(255);
pushMatrix();
fill(222,2,2);
translate( 200,200 );
rotate( radians(value1) );
rect(0,0,100,10 );
popMatrix();
pushMatrix();
fill(222,222,2);
translate( 200,200 );
rotate( radians(value2) );
rect(0,0,100,10 );
popMatrix();
}
void serialEvent(Serial myPort) {
// Read the incoming data string
String dataString = myPort.readStringUntil('\n');
if (dataString != null) {
// Remove any leading/trailing whitespaces
dataString = dataString.trim();
// Split the string into an array of values
String[] values = dataString.split(",");
if (values.length == 2) {
// Parse and save the values into variables
value1 = Integer.parseInt(values[0]);
value2 = Integer.parseInt(values[1]);
// Print the values
//println("Value 1: " + value1);
//println("Value 2: " + value2);
}
}
}
#include <Arduino.h>
#define outputA 6
#define outputB 7
int counter = 0;
int aState;
int aLastState;
int poti1_raw = 0;
int tick = 0;
void setup() {
pinMode (outputA,INPUT);
pinMode (outputB,INPUT);
Serial.begin (9600);
// Reads the initial state of the outputA
aLastState = digitalRead(outputA);
}
void loop() {
poti1_raw = analogRead(0);
aState = digitalRead(outputA); // Reads the "current" state of the outputA
// If the previous and the current state of the outputA are different, that means a Pulse has occured
if (aState != aLastState){
// If the outputB state is different to the outputA state, that means the encoder is rotating clockwise
if (digitalRead(outputB) != aState) {
counter ++;
} else {
counter --;
}
}
aLastState = aState; // Updates the previous state of the outputA with the current state
// delay(100);
if(tick>300){
tick =0;
// Serial.print("Position: ");
Serial.print (counter);
Serial.print(",");
Serial.println(poti1_raw);
}else{
tick++;
}
}