Serial I/O on serial monitor

I was assuuming that getchar(), printf(), putchar() work on the serial monitor but somehow I can't get any output appearing in the serial monitor window.

This is the code snippet I have in loop():

 printf(">\n");fflush(stdout);
   i=getchar();
   putchar(i);

Arduino Nano (FWIW)

Serial.println(">");
if (Serial.available()) {
  Serial.write(Serial.read());
}

You need a different logic for the prompt.

An Arduino is not a PC. It does not have a console like a PC so you should forget about the PC way of interacting with a user.

Just thought stdio would default to the built-in UART (serial monitor). But , ok. No problem, to use the suggested Serial.println/Serial.read.

Can the Serial monitor being open, influence programming? It looks like programming always fails when I have the serial monitor openend.

I want the following program to send the array to the PWM port, but it should hold as long there is no key pressed. I would like to trigger the pulse train with a keystroke to have time to monitor the pin using pulseview, e.g.

byte A[]={255,254,253,252,251,250,249,249,248,247,246,245,245,244,243,242,241,240,
240,239,238,237,236,236,235,234,233,233,232,231,230,229,229,228,227,226,
226,225,224,223,223,222,221,220,220,219,218,218,217,216,215,215,214,213,
212,212,211,210,210,209,208,208,207,206,206,205,204,203,203,202,201,201,
200,199,199,198,197,197,196,195,195,194,194,193,192,192,191,190,190,189,
188,188,187,187,186,185,185,184,183,183,182,182,181,180,180,179,179,178,
177,177,176,176,175,174,174,173,173,172,172,171,170,170,169,169,168,168,
167,166,166,165,165,164,164,163,163,162,162,161,160,160,159,159,158,158,
157,157,156,156,155,155,154,154,153,153,152,152,151,151,150,150,149,149,
148,148,147,147,146,146,145,145,144,144,143,143,142,142,141,141,140,140,
139,139,139,138,138,137,137,136,136,135,135,134,134,134,133,133,132,132,
131,131,130,130,130,129,129,128,128,127,127,127,126,126,125,125,124,124,
124,123,123,122,122,122,121,121,120,120,120,119,119,118,118,118,117,117,
116,116,116,115,115,114,114,114,113,113,113,112,112,111,111,111,110,110,
110,109,109,108,108,108,107,107,107,106,106,106,105,105,105,104,104,104,
103,103,102,102,102,101,101,101,100,100,100,99,99,99,98,98,98,97,
97,97,96,96,96,96,95,95,95,94,94,94,93,93,93,92,92,92,
91,91,91,91,90,90,90,89,89,89,88,88,88,88,87,87,87,86,
86,86,86,85,85,85,84,84,84,84,83,83,83,82,82,82,82,81,
81,81,81,80,80,80,79,79,79,79,78,78,78,78,77,77,77,77,
76,76,76,76,75,75,75,75,74,74,74,74,73,73,73,73,72,72,
72,72,71,71,71,71,70,70,70,70,69,69,69,69,69,68,68,68,
68,67,67,67,67,66,66,66,66,66,65,65,65,65,65,64,64,64,
64,63,63,63,63,63,62,62,62,62,62,61,61,61,61,61,60,60,
60,60,60,59,59,59,59,59,58,58,58,58,58,57,57,57,57,57,
56,56,56,56,56,55,55,55,55,55,55,54,54,54,54,54,53,53,
53,53,53,53,52,52,52,52,52,52,51,51,51,51,51,50,50,50,
50,50,50,49,49,49,49,49,49,48,48,48,48,48,48,48,47,47,
47,47,47,47,46,46,46,46,46,46,45,45,45,45,45,45,45,44,
44,44,44,44,44,44,43,43,43,43,43,43,43,42,42,42,42,42,
42,42,41,41,41,41,41,41,41,40,40,40,40,40,40,40,39,39,
39,39,39,39,39,39,38,38,38,38,38,38,38,38,37,37,37,37,
37,37,37,37,36,36,36,36,36,36,36,36,35,35,35,35,35,35,
35,35,34,34,34,34,34,34,34,34,34,33,33,33,33,33,33,33,
33,33,32,32,32,32,32,32,32,32,32,31,31,31,31,31,31,31,
31,31,31,30,30,30,30,30,30,30,30,30,30,29,29,29,29,29,
29,29,29,29,29,28,28,28,28,28,28,28,28,28,28,27,27,27,
27,27,27,27,27,27,27,27,26,26,26,26,26,26,26,26,26,26,
26,25,25,25,25,25,25,25,25,25,25,25,25,24,24,24,24,24,
24,24,24,24,24,24,24,23,23,23,23,23,23,23,23,23,23,23,
23,23,22,22,22,22,22,22,22,22,22,22,22,22,22,22,21,21,
21,21,21,21,21,21,21,21,21,21,21,21,20,20,20,20,20,20,
20,20,20,20,20,20,20,20,19,19,19,19,19,19,19,19,19,19,
19,19,19,19,19,19,18,18,18,18,18,18,18,18,18,18,18,18,
18,18,18,18,17,17,17,17,17,17,17,17,17,17,17,17,17,17,
17,17,17,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,
16,16,16,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
15,15,15,15,14,14,14,14,14,14,14,14,14,14,14,14,14,14,
14,14,14,14,14,14,14,13,13,13,13,13,13,13,13,13,13,13,
13,13,13,13,13,13,13,13,13,13,13,12,12,12,12,12,12,12,
12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,11,
11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
11,11,11,11,11,11,11,11,10,10,10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
9,9,9,9,9,9,9,9,9,9};
int loopcount=0;
// Program B for Nano2 – Changed frequency on Pin 3 :
int i=0;
void setup() {
Serial.begin(115200);
TCCR2B = TCCR2B & B11111000 | B00000001; // for PWM frequency of 3921.16 Hz
    pinMode(3,OUTPUT);
    delay(1000);

}

void loop() {
   Serial.print(loopcount++);
   Serial.println("=>");
   while(!Serial.available())
    ;
   Serial.read();
   Serial.println("!\n");
   
    for (i=0;i<1000;i++)
        analogWrite(3,A[i]);
    
}

Ah, I already see: while(Serial.available == 0); it should be.

If "programming" means "uploading": which version of the IDE?

I recall a bug in one of the the release candidates of IDE2.0 where the open serial monitor interfered with the upload; I thought it was solved. There was no bug like that in 1.8.x.

1.8.19

Yes. I mean "uploading" the -> button, which starts AVRDude.

At the moment uploading is totally unreliable. Also I'm not sure about what I programmer I should choose.

quitting and restarting Arduino IDE (1.8.19) and uploading the new empty sketch seems to work. Programming the app above then once (the first time) it works.
Maybe, since my loop previously didn't terminate or stop, it overflowed the buffer with serial i/o and impeded the programming.

I've replied in your topic about that: What programmer with Nano connected via USB? - #2 by sterretje

Thanks, this got me free of at least one problem :slight_smile:

I got the program logic in the loop working now:

void loop() {
   Serial.print(loopcount);
   loopcount++;
   Serial.println("=>");
   while(Serial.available() == 0)
    ;
   Serial.read();
   Serial.println("!\n");
   
    for (i=0;i<1000;i++)
        analogWrite(3,A[i]);
    
}

I don't think this would work, there is no time to have any PWM cycle between reprogramming.

Ah, I see. The analogwrite() are too fast after another? Since the PWM-frequency is 3.9KHz, a 1ms delay() would be appropriate? What is the highest PWM-frequency possible on an Arduino Nano?

I think that would be too short.

https://docs.arduino.cc/learn/microcontrollers/analog-output

You should not use delay, get used to timing control via millis.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.