After uploading my code, my serial monitor does not display anything.
Is there any way to fix this issue?
After uploading my code, my serial monitor does not display anything.
Is there any way to fix this issue?
@nwf, welcome.
Your board is no longer properly detected; serial monitor is missing the 'box' for e.g. the baud rate selection.
Does the problem persist if you close and open the serial monitor again?
What does Windows device manager think of your board? Which version of Windows do you use?
Please post your complete sketch; don't forget the code tags as described in How to get the best out of this forum.
yes, the problem persists even if the serial monitor is closed and opened again.
I use Windows 11
volatile unsigned int temp, counter = 0; //This variable will increase or decrease depending on the rotation of encoder
void setup() {
Serial.begin (9600);
pinMode(2, INPUT_PULLUP); // internal pullup input pin 2
pinMode(3, INPUT_PULLUP); // internal pullup input pin 3
//Setting up interrupt
//A rising pulse from the encoder activated ai0(). AttachInterrupt 0 is DigitalPin nr 2 on Arduino.
attachInterrupt(0, ai0, RISING);
//B rising pulse from encoder activated ai1(). AttachInterrupt 1 is DigitalPin nr 3 on Arduino.
attachInterrupt(1, ai1, RISING);
}
void loop() {
// Send the value of counter
if( counter != temp ){
Serial.println (counter);
temp = counter;
}
}
void ai0() {
// ai0 is activated if DigitalPin nr 2 is going from LOW to HIGH
// Check pin 3 to determine the direction
if(digitalRead(3)==LOW) {
counter++;
}else{
counter--;
}
}
void ai1() {
// ai0 is activated if DigitalPin nr 3 is going from LOW to HIGH
// Check with pin 2 to determine the direction
if(digitalRead(2)==LOW) {
counter--;
}else{
counter++;
}
}
OK, at least you have your baud rate dropdown back.
I'm not sure if I can help you with your program. One thing is that, because your variables are two bytes in size, you should use noInterrupts () and interrupts() around the code that uses these variables
void loop()
{
noInterrupts();
// Send the value of counter
if (counter != temp)
{
Serial.println(counter);
temp = counter;
}
interrupts();
}
I would advise to start by printing the variables so you can see if they change. If they never change you will never get output.
What is the source for the interrupts?
Note:
Don't use magic numbers; your pins serve a function so use names for them.
I use a rotary encoder, pin 2 for encoder A and pin 3 for encoder B. When the encoder shaft is rotated, the counter value will change
#define encoderA 2
#define encoderB 3
volatile unsigned int temp, counter = 0; //This variable will increase or decrease depending on the rotation of encoder
void setup() {
Serial.begin (9600);
pinMode(encoderA, INPUT_PULLUP); // internal pullup input pin 2
pinMode(encoderB, INPUT_PULLUP); // internal pullup input pin 3
//Setting up interrupt
//A rising pulse from encoder activated ai0(). AttachInterrupt 0 is DigitalPin nr 2 on Arduino.
attachInterrupt(0, ai0, RISING);
//B rising pulse from encoder activated ai1(). AttachInterrupt 1 is DigitalPin nr 3 on Arduino.
attachInterrupt(1, ai1, RISING);
}
void loop() {
noInterrupts();
// Send the value of counter
if( counter != temp ){
Serial.println (counter);
temp = counter;
}
interrupts();
}
void ai0() {
// ai0 is activated if DigitalPin nr 2 is going from LOW to HIGH
// Check pin 3 to determine the direction
if(digitalRead(encoderB)==LOW) {
counter++;
}else{
counter--;
}
}
void ai1() {
// ai0 is activated if DigitalPin nr 3 is going from LOW to HIGH
// Check with pin 2 to determine the direction
if(digitalRead(encoderA)==LOW) {
counter--;
}else{
counter++;
}
}
still no changes to the serial monitor
I was more thinking something like
void loop()
{
noInterrupts();
Serial.print(F("counter = "));
Serial.println(counter);
Serial.print(F("temp = "));
Serial.println(temp);
// Send the value of counter
//if (counter != temp)
//{
// Serial.println(counter);
// temp = counter;
//}
interrupts();
}
Do you see the variables changing?
Does this map to pins 2 and 3 (have you tried digitalPinToInterrupt)?
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.