yes just like that I have an encoder coupled to the axis
Hehe. My friend, the information is coming in slowly. How could I know?
I need to know how this is wired to proceed. The android code to command the angle over Bluetooth I already, have, but I need to know the details to write the Arduino code. (if possible). Maybe you could manually design it.
Letās recapitulate this topic because I think I misunderstood the whole thing.
-
1 You: Remember the code of the circle with degrees and steps, which is moved by an encoder?
in java mode it works well while in android mode the same code of the circle does the flickeringI: Now I believe that you want an āencoder knobā being displayed on the screen that rotates equally to the motor, while I instead was thinking that you wanted the motor to rotate by moving the knop on the device. I do not understand how you have the code working on PC Java, because I donāt see any serial commands.
-
2 You: DC Motor yes i have 2 encoder
I: Here I totally missed that you mentioned a dc motor, while I already was writing code for a stepper motor.
-
3 You: this my code:===
I: Now that I know that a rotary optical encoder is coupled to the axis, the code makes totally sense. The external interrupt request pins 2 and 3 receive the encoder signals and the attachInterrupt() function will call the ai1() and ai0() functions in turn to keep track of the counter variable, which gives the amount of steps made, thus, in fact, the angle. To display a rotating knob this indeed is sufficient.
Of course, this is only a part of the code, because I see a slider, etc, but how do you actually control, or set, the angle?
Now that I think I have a working Arduino code to control the stepper motor, I will post it here anyway. I havenāt tested it yet, because at the moment I have no stepper at my disposal. So if anyone wants to try it, will do so at his own risk. This code expects to receive the angle (0 to 360) via serial.
Arduino stepper control code.
const int stepperPin = 7;
const int directionPin = 6;
String state = "";
int current_state = 0;
int current_angle = 0;
int last_angle = 0;
int angle = 0;
int rotatation = 0;
boolean dirRotation = HIGH;
int revolution = 200;
void setup() {
pinMode(stepperPin, OUTPUT);
pinMode(directionPin, OUTPUT);
Serial.begin(38400);
}
void loop() {
delayMicroseconds(1);
if (Serial.available() > 0) {
state = Serial.readString();
}
current_state = state.toInt();
//Serial.println(state);
current_angle = map(current_state, 0, 359, 0, revolution);
digitalWrite(directionPin, HIGH);
if (current_angle != last_angle) {
if (current_angle > last_angle) {
rotatation = current_angle-last_angle;
for (int x = 0; x < rotatation; x++) {
digitalWrite(stepperPin, HIGH);
delayMicroseconds(500);
digitalWrite(stepperPin, LOW);
delayMicroseconds(500);
}
}
if (current_angle < last_angle) {
rotatation = last_angle-current_angle;
digitalWrite(directionPin, LOW);
for (int x = 0; x < rotatation; x++) {
digitalWrite(stepperPin, HIGH);
delayMicroseconds(500);
digitalWrite(stepperPin, LOW);
delayMicroseconds(500);
}
}
}
last_angle = current_angle;
}
}
Hi Noel
yes in fact, I rotate the motor then the encoder on the same axis and consequently the needle of the circle must move on android,
on java; yes it does and transmits via serial (maybe I will not have posted the part of the serial)
however I want to clarify that everything works with dc motor and not step by step
so the code you posted above i believe it is not for my engine but for step by step
let me know thanks
OK. So if you post the serial code that you use to send data to the pc processing sketch, I can write the Android receiver code. I need to know how you are sending your data.
import processing.serial.*;
Serial myPort;
String myString = null;
int incoming_position = 0;
float position_in_degree = 0;
PFont font18,font63;
void setup(){
size(800, 600);
font18 = loadFont("Tahoma-18.vlw");
font63 = loadFont("Tahoma-63.vlw");
println(Serial.list());
myPort = new Serial(this, Serial.list()[0], 9600);
}
void draw(){
background(100);
textFont(font63, 36);
text("RAW Position:", 30, 510);
text(incoming_position, 270, 510);
text("Position in Degree:", 30, 550);
text(position_in_degree, 330, 550);
draw_angle(400,250,200,position_in_degree); // draw circle and encoder position
// Reading Serial Port when buffer not zero
while (myPort.available() > 0) {
myString = myPort.readStringUntil(10); // Read Data from Serial until line feed (10)
if (myString != null) {
myString = trim(myString); // Remove Line Feed
incoming_position = int(myString); // String to int
position_in_degree = map(incoming_position,0, 1023, 0,359.999); // translate raw position in to 360 degree position format
println(position_in_degree);
}
}
}
void draw_angle(int x, int y,int size, float angle_dec){
noFill();
stroke(255,255,255,255);
// X,Y plane Translation
float end_x,end_y;
float angle_rad = radians(angle_dec);
end_x = x + cos(angle_rad)*size;
end_y = y + sin(angle_rad)*size;
// mark angle guage in circle
line(x,y,end_x,end_y);
int space = 20;
int space2 = size - space;
ellipse(x, y, size*2+space, size*2+space);
// mark small tick on cicle every 15 degree
for(int i = 0; i < 360; i = i +15){
float rad1 = radians(i);
// mark large scale on 0 90 180 270 degree
if( i == 0 || i == 90 || i == 180 || i == 270){
line(x + cos(rad1)*space2,y + sin(rad1)*space2,x + cos(rad1)*(size+space),y + sin(rad1)*(size+space));
}else{
line(x + cos(rad1)*space2,y + sin(rad1)*space2,x + cos(rad1)*size,y + sin(rad1)*size);
}
}
}
. . . . .I mean the Arduino serial code that you use to send data to the pc processing sketch
Edit. Looking at this code in this line:
position_in_degree = map(incoming_position,0, 1023, 0,359.999);
Your Arduino code is sending strings from number 0 to 1023 (to give the angle), so thatās enough as well. Tonight I will post the code.
the data from 0 to 1023 are sent by the encoder through the sketch I posted to you some post above
I understood this already, therefore I edited my post soon after posting.I have it already working nicely. After lunch ( and a nap ), I will post it.
226 / 5000
Risultati della traduzione
noel I would need to be able to set the position of the motor example say go to position example 300 (then the program reads the encoder steps and the mortor moves to position 300 Could you give me info?
The Ketai lib canāt handle fast incoming data, so I wrote a Bluetooth code that does.
I posted that code here and added also the code you requested as an example for others as well.
I kept your design, because I think itās nice, but re-wrote the code as a class to easily construct multiple knobs if needed. Donāt forget to give the needed permissions.
But that was my first understanding of your request because it seemed more obvious to me that you want to control your motor, and not only visualize the axis position. That is where the Arduino code I wrote above is for (stepper) Of course you can do this as well with the dc motor since you have an encoder coupled to the axis. The picture shows me that you have a slider, I assume itās for the speed control, but without seeing the whole Arduino code I canāt say much. It also depends on what the motor is connected to, because you are using a more potent motor, and together with the load they can generate high kinetic energy so that you need to decrease the velocity before the actual stopping.
Did you have this already in your code?
no, I have a good code but it is for python and runs under windows with arduino and honestly I donāt like this os so much but how it works is good and malleable in everything only that should be modified for android
[image]
Noel where you ?
i need help
I will not answer any questions anymore because Iām leaving this forum unless my request being accepted. My ego just canāt accept an unjustifiable accusation. You were pinged at did not react. I am very disappointed.
(You received a ping and did not react. I am very disappointed.) what does it mean
But what do you say I donāt know what a ping is
also I always have to use google to translate so everything is new to me
so explain to me what happened as I no longer receive posts regarding my requests
Anyway I wanted to tell you that I tried your code for the circle and it works great with my 200 step encoder for rotation
Please donāt take things that happen here too seriously. Life is short. Personally, I think you do a great job and are the main reason I hang around over here.
With regard to the initial reason for this thread (which was a flicker of the encoder gauge) I believe the problem was on the Android end when the serial bluetooth data was reconstructed. The data was not being perfectly parsed and sometimes gave spurious results, which gave the flicker when plotted. The demo that you posted (using Android bluetooth code) appears to have solved the problem. It needs further testing but it appears to work as far as stopping the flicker (due to better parsing code). Thanks for the post.
Hello Svan
?
I donāt understand what I have done something wrong or did something wrong? let me know ā¦ then the ping issue that I donāt understand
Thanks