先试一下。
This commit is contained in:
@@ -3,10 +3,12 @@
|
||||
const byte IN1 = 6;
|
||||
const byte IN2 = 7;
|
||||
const byte ENABLE = 5;
|
||||
const byte ENCO_B = 4;
|
||||
const byte ENCO_B = 3;
|
||||
const byte ENCO_A = 2;
|
||||
|
||||
int count = 0;
|
||||
int forward_count = 0;
|
||||
int backward_count = 0;
|
||||
const unsigned long RESET_COUNT_PERIOD_IN_US = 50000;
|
||||
const int EDGES_PER_CYCLE = 13;
|
||||
const int REDUCTION_RATIO = 20;
|
||||
@@ -25,29 +27,100 @@ void setup() {
|
||||
Timer1.initialize();
|
||||
Timer1.setPeriod(RESET_COUNT_PERIOD_IN_US);
|
||||
Timer1.attachInterrupt(resetCountAndPrint);
|
||||
attachInterrupt(digitalPinToInterrupt(ENCO_A), onEncoAFalling, FALLING);
|
||||
analogWrite(ENABLE, 100);
|
||||
attachInterrupt(digitalPinToInterrupt(ENCO_A), onEncoAChange, CHANGE);
|
||||
attachInterrupt(digitalPinToInterrupt(ENCO_B), onEncoBChange, CHANGE);
|
||||
// attachInterrupt(digitalPinToInterrupt(ENCO_A), onEncoAFalling, FALLING);
|
||||
// attachInterrupt(digitalPinToInterrupt(ENCO_B), onEncoBFalling, FALLING);
|
||||
analogWrite(ENABLE, 50);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Serial.print(digitalRead(2));
|
||||
// Serial.print(" ");
|
||||
// Serial.println(digitalRead(4));
|
||||
// delay(100);
|
||||
// int statusA = analogRead(0);
|
||||
// int statusB = analogRead(1);
|
||||
// // Serial.print("A: ");
|
||||
// Serial.print(statusA);
|
||||
// // Serial.print("B: ");
|
||||
// Serial.print(", ");
|
||||
// Serial.println(statusB);
|
||||
}
|
||||
|
||||
void resetCountAndPrint() {
|
||||
Serial.print("Rpm: ");
|
||||
Serial.println((double)count * 60 * 1000 * 1000 / EDGES_PER_CYCLE / REDUCTION_RATIO / RESET_COUNT_PERIOD_IN_US);
|
||||
// Serial.println((double)count * 60 * 1000 * 1000 / RESET_COUNT_PERIOD_IN_US / REDUCTION_RATIO / EDGES_PER_CYCLE);
|
||||
// Serial.print(forward_count);
|
||||
// Serial.print(' ');
|
||||
// Serial.print(backward_count);
|
||||
// Serial.print(' ');
|
||||
Serial.println(count);
|
||||
count = 0;
|
||||
// forward_count = 0;
|
||||
// backward_count = 0;
|
||||
}
|
||||
|
||||
void onEncoAFalling() {
|
||||
// Serial.println("In!");
|
||||
if (digitalRead(ENCO_B) == LOW) {
|
||||
count++;
|
||||
backward_count++;
|
||||
}
|
||||
else {
|
||||
forward_count++;
|
||||
}
|
||||
}
|
||||
|
||||
void onEncoBFalling() {
|
||||
if (digitalRead(ENCO_A) == LOW) {
|
||||
forward_count++;
|
||||
}
|
||||
else {
|
||||
backward_count++;
|
||||
}
|
||||
}
|
||||
|
||||
void onEncoAChange() {
|
||||
int b_stat = digitalRead(ENCO_B);
|
||||
int a_stat = digitalRead(ENCO_A);
|
||||
if (a_stat == LOW) {
|
||||
// if (b_stat == LOW) {
|
||||
// Serial.println("A 1");
|
||||
// count--;
|
||||
// }
|
||||
if (b_stat == HIGH) {
|
||||
Serial.println("A 2");
|
||||
count ++;
|
||||
}
|
||||
}
|
||||
else {
|
||||
// if (b_stat == HIGH) {
|
||||
// Serial.println("A 3");
|
||||
// count--;
|
||||
// }
|
||||
if (b_stat == LOW) {
|
||||
Serial.println("A 4");
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void onEncoBChange() {
|
||||
int a_stat = digitalRead(ENCO_A);
|
||||
int b_stat = digitalRead(ENCO_B);
|
||||
if (b_stat == LOW) {
|
||||
if (a_stat == HIGH) {
|
||||
Serial.println("B 1");
|
||||
count--;
|
||||
}
|
||||
// if (a_stat == LOW) {
|
||||
// Serial.println("B 2");
|
||||
// count++;
|
||||
// }
|
||||
}
|
||||
else {
|
||||
if (a_stat == LOW) {
|
||||
Serial.println("B 3");
|
||||
count--;
|
||||
}
|
||||
// if (a_stat == HIGH) {
|
||||
// Serial.println("B 4");
|
||||
// count++;
|
||||
// }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user