我不知道!!

This commit is contained in:
unlockable
2023-07-06 14:23:24 +08:00
parent 3c52a9219f
commit 3af54a2a52

View File

@@ -1,17 +1,19 @@
#include <TimerOne.h> #include <TimerOne.h>
const byte IN1 = 6; const byte IN1 = 10;
const byte IN2 = 7; const byte IN2 = 11;
const byte ENABLE = 5; const byte ENABLE = 6;
const byte ENCO_B = 3; const byte ENCO_B = 3;
const byte ENCO_A = 2; 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 unsigned long RESET_COUNT_PERIOD_IN_US = 50000;
const int EDGES_PER_CYCLE = 13; const int EDGES_PER_CYCLE = 13;
const int REDUCTION_RATIO = 20; const int REDUCTION_RATIO = 20;
int count = 0;
int forward_count = 0;
int backward_count = 0;
unsigned long aFallingTime = 0;
unsigned long bFallingTime = 0;
void setup() { void setup() {
pinMode(IN1, OUTPUT); pinMode(IN1, OUTPUT);
@@ -19,29 +21,37 @@ void setup() {
pinMode(ENABLE, OUTPUT); pinMode(ENABLE, OUTPUT);
pinMode(A0, INPUT); pinMode(A0, INPUT);
pinMode(A1, INPUT); pinMode(A1, INPUT);
digitalWrite(IN1, HIGH); digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW); digitalWrite(IN2, HIGH);
pinMode(ENCO_B, INPUT); pinMode(ENCO_B, INPUT);
pinMode(ENCO_A, INPUT); pinMode(ENCO_A, INPUT);
Serial.begin(115200); Serial.begin(115200);
Timer1.initialize(); Timer1.initialize();
Timer1.setPeriod(RESET_COUNT_PERIOD_IN_US); Timer1.setPeriod(RESET_COUNT_PERIOD_IN_US);
Timer1.attachInterrupt(resetCountAndPrint); Timer1.attachInterrupt(resetCountAndPrint);
attachInterrupt(digitalPinToInterrupt(ENCO_A), onEncoAChange, CHANGE); // attachInterrupt(digitalPinToInterrupt(ENCO_A), onEncoAChange, CHANGE);
attachInterrupt(digitalPinToInterrupt(ENCO_B), onEncoBChange, CHANGE); // attachInterrupt(digitalPinToInterrupt(ENCO_B), onEncoBChange, CHANGE);
// attachInterrupt(digitalPinToInterrupt(ENCO_A), onEncoAFalling, FALLING); attachInterrupt(digitalPinToInterrupt(ENCO_A), onEncoAFalling, FALLING);
// attachInterrupt(digitalPinToInterrupt(ENCO_B), onEncoBFalling, FALLING); // attachInterrupt(digitalPinToInterrupt(ENCO_B), onEncoBFalling, FALLING);
analogWrite(ENABLE, 50); analogWrite(ENABLE, 50);
} }
void loop() { void loop() {
// int statusA = analogRead(0); // int statusA = digitalRead(ENCO_A);
// int statusB = analogRead(1); // int statusB = digitalRead(ENCO_B);
// // Serial.print("A: "); // // Serial.print("A: ");
// Serial.print('$');
// Serial.print(statusA); // Serial.print(statusA);
// // Serial.print("B: "); // // Serial.print("B: ");
// Serial.print(", "); // Serial.print(' ');
// Serial.println(statusB); // Serial.print(statusB);
// Serial.println(";");
// bFallingTime = micros();
// aFallingTime = micros();
// Serial.println(bFallingTime - aFallingTime);
} }
void resetCountAndPrint() { void resetCountAndPrint() {
@@ -51,10 +61,15 @@ void resetCountAndPrint() {
// Serial.print(' '); // Serial.print(' ');
// Serial.print(backward_count); // Serial.print(backward_count);
// Serial.print(' '); // Serial.print(' ');
count = forward_count - backward_count;
Serial.print(forward_count);
Serial.print(' ');
Serial.print(backward_count);
Serial.print(' ');
Serial.println(count); Serial.println(count);
count = 0; count = 0;
// forward_count = 0; forward_count = 0;
// backward_count = 0; backward_count = 0;
} }
void onEncoAFalling() { void onEncoAFalling() {
@@ -64,63 +79,71 @@ void onEncoAFalling() {
else { else {
forward_count++; forward_count++;
} }
// aFallingTime = micros();
} }
void onEncoBFalling() { void onEncoBFalling() {
if (digitalRead(ENCO_A) == LOW) { // if (digitalRead(ENCO_A) == LOW) {
forward_count++; // forward_count++;
} // }
else { // else {
backward_count++; // backward_count++;
} // }
bFallingTime = micros();
} }
void onEncoAChange() { void onEncoAChange() {
int b_stat = digitalRead(ENCO_B); if (LOW == digitalRead(ENCO_A)) {
int a_stat = digitalRead(ENCO_A); if (LOW == digitalRead(ENCO_B)) {
if (a_stat == LOW) { // Serial.println("A 1");
// if (b_stat == LOW) { // count--;
// Serial.println("A 1"); backward_count++;
// count--; }
// } if (HIGH == digitalRead(ENCO_B)) {
if (b_stat == HIGH) { // Serial.println("A 2");
Serial.println("A 2"); // count ++;
count ++; forward_count++;
} }
} }
else { else {
// if (b_stat == HIGH) { if (HIGH == digitalRead(ENCO_B)) {
// Serial.println("A 3"); // Serial.println("A 3");
// count--; // count--;
// } backward_count++;
if (b_stat == LOW) { }
Serial.println("A 4"); if (LOW == digitalRead(ENCO_B)) {
count++; // Serial.println("A 4");
// count++;
forward_count++;
} }
} }
} }
void onEncoBChange() { void onEncoBChange() {
int a_stat = digitalRead(ENCO_A); if (LOW == digitalRead(ENCO_B)) {
int b_stat = digitalRead(ENCO_B); if (HIGH == digitalRead(ENCO_A)) {
if (b_stat == LOW) { // Seria/l.println("B 1");
if (a_stat == HIGH) { // count--;
Serial.println("B 1"); backward_count++;
count--; }
if (LOW == digitalRead(ENCO_A)) {
// Serial.println("B 2");
// count++;
forward_count++;
} }
// if (a_stat == LOW) {
// Serial.println("B 2");
// count++;
// }
} }
else { else {
if (a_stat == LOW) { if (LOW == digitalRead(ENCO_A)) {
Serial.println("B 3"); // Serial.println("B 3");
count--; // count--;
backward_count++;
}
if (HIGH == digitalRead(ENCO_A)) {
// Serial.println("B 4");
// count++;
forward_count++;
} }
// if (a_stat == HIGH) {
// Serial.println("B 4");
// count++;
// }
} }
} }