Hi everyone!
I wanted to use two pins of my esp 32 and I faced that problem, it was detected both rising and falling edge of my signal. I wanted to read a tachometer's signal but it didn'tt want to work properly. I have tried it with esp8266 and then it worked fine.
Is there any solution for this problem?
Here is my code:
#include <Arduino.h>
#include <analogWrite.h>
const int pinA = 23;//14
const int pinB = 22;//4
int cA = 0;
int cB = 0;
int enA = 32;
int in1 = 33;
int in2 = 25;
int enB = 13;
int in3 = 26;
int in4 = 27;
void IRAM_ATTR cntA() {
cA++;
Serial.print("cA:");
Serial.println(cA);
}
void IRAM_ATTR cntB() {
cB++;
Serial.print("cB:");
Serial.println(cB);
}
void Forward( int steps,int mspeed){
cA = 0; // reset counter A to zero
cB = 0; // reset counter B to zero
// Set Motor A forward
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
// Set Motor B forward
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
// Go forward until step value is reached
while (steps > cA && steps > cB) {
if (steps > cA) {
analogWrite(enA, 1*mspeed);
} else {
analogWrite(enA, 0);
}
if (steps > cB) {
analogWrite(enB, 1*mspeed);
} else {
analogWrite(enB, 0);
}
delay(0);
}
// Stop when done
analogWrite(enA, 0);
analogWrite(enB, 0);
cA = 0; // reset counter A to zero
cB = 0; // reset counter B to zero
}
void Backward (int steps, int mspeed) {
cA = 0; // reset counter A to zero
cB = 0; // reset counter B to zero
// Set Motor A forward
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
// Set Motor B forward
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
while (steps > cA && steps > cB){
if (steps > cA) {
analogWrite(enA, 1*mspeed);
} else {
analogWrite(enA, 0);
}
if (steps > cB) {
analogWrite(enB, 1*mspeed);
} else {
analogWrite(enB, 0);
}
delay(0);
}
// Stop when done
analogWrite(enA, 0);
analogWrite(enB, 0);
cA = 0; // reset counter A to zero
cB = 0; // reset counter B to zero
}
void setup()
{
pinMode(in1,OUTPUT);
pinMode(in2,OUTPUT);
pinMode(enA,OUTPUT);
pinMode(in3,OUTPUT);
pinMode(in4,OUTPUT);
pinMode(enB,OUTPUT);
pinMode(pinA,INPUT_PULLDOWN);
pinMode(pinB,INPUT_PULLDOWN);
Serial.begin(115200);
attachInterrupt(digitalPinToInterrupt (pinA), cntA, RISING);
attachInterrupt(digitalPinToInterrupt (pinB), cntB, RISING); // Increase counter B when speed sensor pin goes High
Forward(20,130);
delay(2000);
Backward(20,130);
delay(2000);
}
void loop()
{
}
I also tried with and without pull down the interrupt pins, but it didn't work.