[FIXED] Serial transmits in .INO but not working in .CPP

Hi, I'm not new to Arduino but haven't used it for many years so pretty rusty. Trying to port a project to the IDE environment and have a fundamental problem.

Serial.write works just fine inside loop() but not in a function external to the main INO file.

Code follows.

extern void quub_setup();
extern void quub_loop();

void setup() {
  quub_setup();
}

void loop() {
  quub_loop();
  Serial.write ("Y");
}
#include "Arduino.h"

void quub_setup() {
    pinMode(LED_BUILTIN, OUTPUT);
    Serial.begin(9600);
}

void quub_loop() {
  digitalWrite(LED_BUILTIN, HIGH); 
  delay(500);                     
  digitalWrite(LED_BUILTIN, LOW);  
  delay(500);                    
  Serial.write ("X");
}

Everything compiles just fine and the LED flashes as expected. However while I get plenty of Ys on the serial monitor there are no Xs.

Sorry, rooky timing mistake, not enough time between printing the X then dropping out of quub_loop() and straight away printing Y.

This fixed it

  digitalWrite(LED_BUILTIN, HIGH); 
  delay(500);      
  Serial.write ("X");               
  digitalWrite(LED_BUILTIN, LOW);  
  delay(500);  

Strange. With the 1 second delay between each character written to the console you are not exceeding the modest capabilities of 9600 baud. I'd have expected a slow delivery of this sequence "XYXYXYXY...." to the console based on your original code and assuming a conventional hardware setup. What is the file type of the external code ?

That's what I though until 2 minutes after posting :grinning:

But the last thing quub_loop() does before returning is Serial.write("X"), and the FIRST thing loop() does on getting control back is Serial.write("Y").

These two calls would be just microseconds apart.

Now that said I would have thought that there was a buffer to handle such things, but it seems not.

OK. When I get a chance later on today I'll attempt to duplicate your findings ins Wokwi.com. Would a Uno be a realistic representation of your hardware and are you using a reasonably fresh version of the IDE ?

Did. The code in #1 prints "XY" about once a second.

Simulation
XYXYXYXYXY

wokwi simulator, UNO board and whatever version of whatever too chain they use.

a7

I am pleased you got there first. My suspicion is that @graynomad has an Arduino infrastructure from a previous decade.

Hmmm, interesting.

As for my setup, the latest IDE (well 2.3.2, 6 months old I think) and a Pi Pico.

@graynomad! Welcome back!

1 Like

Thanks, it's been a while since I posted here eh? I did recognize your handle on another post the other day and thought, wow he's still here.

I've been out of the game for a few years but getting back into it now with a new design...well a rehash of a 10-year-old design :grinning: So I might get active again, most of the board models are new to me now but I'm working with the Pi Pico which is a nice piece of kit.

I'm setting up a new lab but am also building a house so time is limited probably until next year.

2 Likes

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