I am having an issue with Serial Monitor not showing the correct readings in serial monitor.
I have 2 MH sensor which is detecting the rpm/revolutions for my DC motor using encoder disks.
When i tested the sensors individually, it shows correct analog and digital reading as picture shown.
const int IN_A0 = A1; // analog input
const int IN_D0 = 5; // digital input
void setup() {
Serial.begin(9600);
pinMode (IN_A0, INPUT);
pinMode (IN_D0, INPUT);
}
int value_A0;
bool value_D0;
void loop() {
value_A0 = analogRead(IN_A0); // reads the analog input from the IR distance sensor
value_D0 = digitalRead(IN_D0);// reads the digital input from the IR distance sensor
Serial.print(" Analogue = ");
Serial.print(value_A0);
Serial.print("\t Digital =");
Serial.println(value_D0);
delay(100);
}
However, when i use both sensors simultaneously while i rotate the motor and using Serial Monitor, somehow either my
Right analog value is not changing(remain in 900s) while Right Digital value is changing,
or
Right analog value is changing (from 100s to 900s) while Right Digital value is the same (1)
const int Left_IN_A0 = A0; // analog input
const int Left_IN_D2 = 2; // digital input
const int Right_IN_A1 = A1; // analog input
const int Right_IN_D5 = 5; // digital input
void setup() {
Serial.begin(9600);
pinMode (Left_IN_A0, INPUT);
pinMode (Left_IN_D2, INPUT);
pinMode (Right_IN_A1, INPUT);
pinMode (Right_IN_D5, INPUT);
}
int value_A0;
int value_A1;
bool value_D2;
bool value_D5;
void loop() {
value_A0 = analogRead(Left_IN_A0); // reads the analog input from the IR distance sensor
value_D2 = digitalRead(Left_IN_D2);// reads the digital input from the IR distance sensor
value_A1 = analogRead(Right_IN_A1);
value_D5 = analogRead(Right_IN_D5);
Serial.print(" Left Analogue = ");
Serial.print(value_A0);
Serial.print(" Left Digital = ");
Serial.print(value_D2);
Serial.print(" Right Analogue = ");
Serial.print(value_A1);
Serial.print(" Right Digital =");
Serial.print(value_D5);
Serial.println();
delay(100);
}
Hi I have another issue. I originally planned to use both encoders in Arduino pin 2 and 3 for ISR.
When i soldered the encoder to pin 3, while testing individually, analogue value is changing while digital value is not (not sure if pin issue as pin 5 is working as shown above individual testing.
const int IN_A0 = A1; // analog input
const int IN_D0 = 3; // digital input
void setup() {
Serial.begin(9600);
pinMode (IN_A0, INPUT);
pinMode (IN_D0, INPUT);
}
int value_A0;
bool value_D0;
void loop() {
value_A0 = analogRead(IN_A0); // reads the analog input from the IR distance sensor
value_D0 = digitalRead(IN_D0);// reads the digital input from the IR distance sensor
Serial.print(" Analogue = ");
Serial.print(value_A0);
Serial.print("\t Digital =");
Serial.println(value_D0);
delay(100);
}
I am currently building a vehicle to avoid obstacles + go back to home (starting point) once a period has passed. lets say 1min.
Currently i am using 2 motor encoders to detect motor revolution to determine where the robot is, so that it can go back to home.
I have heard that i will need to use ISR so that it can calculate the motor rev/distance while letting the robot move.
I am using Arduino Uno and know that interrupt pins are 2 and 3.
However, my pin 3 is having issue for some reasons when i was testing out my encoder as per below post. Am i able to calculate motor rev/distance without using interrupt?
Am i able to calculate motor rev/distance without using interrupt?
If you can read the encoder fast enough in loop() then you don't need to use an interrupt but even using an interrupt don't expect your calculation of current position and angle to be very accurate due to wheel slip and the slight difference in diameter of the wheels
anyang:
However, my pin 3 is having issue for some reasons when i was testing out my encoder as per below post. Am i able to calculate motor rev/distance without using interrupt?
My first question is why have you started a second Thread about the same project? Please click Report to Moderator and ask to have the two Threads merged so we have all the data in one place.
Next, either figure out why Pin 3 is not working, and fix it, or get a new Arduino if it is damaged.
Finally, you can use PinChange interrupts on any I/O pin, but they are a bit less convenient to use.
gcjr:
what is an MH sensor? is it an encoder with 2 optical outputs?
i don't understand why you read one input as analog and the other as digital
Please see pic below. should i just ignore the analog input and just focus on digital? since i only want to see the rising edge to determine if the encoder is turned and then do counter++?
But i am still having issues since digital value is not changing..
i finally manage to found the datasheet after so long.
It seems to be a simple slotted optical switch. I can't think of any reason to use the analog output from it.
I suggest you write a short program that does nothing except count the pulses from the two devices and print the value to the Serial Monitor maybe once per second. There would be little point trying anything more complex if that does not work reliably.