Serial monitor=proof…but the µC (Arduino R3) acts differently!

it's one thing to know the syntax, it's another thing to know how to use the language

i think most people would have done what you posted as several functions

  • display results of a measurment on the LCD using a generic routine
  • make distance measurement
  • evaluate the distance
  • use timer to invoke this processing periodically

also, don't underestimate the necessity to include diagnostics and test code in your programs

Hi, @mutmong

Why, don't you have enough panel space for a selector switch?
It would have possibly been simpler and easier to code.

Why didn't you use a Nano plugged into your PCB on headers.
You basically have cut off your error system, also you can use the serial port to communicate with another system, like a bigger PC based display.

What ultrasonic sensor are you using and what is its sensing angle?

Tom.... :smiley: :+1: :coffee: :australia:

You could have added test points or a header to temporarily attach an external UART <--> USB converter when needed for serial debugging:

Simpler hardware is the aim, not simpler coding. Less hardware for mass production. I look at like a pro (but i”m not :wink:).

The code i’ve posted in this topic will compile (without warnings) if you can get the libs.

Indeed inside out. Different aproach. “But if i leave this function with passing the (bool =flag) and the next loop cycle i return to this function thd int fails() is getting reset to zero. Right?….but i thought about this kind of solution to pass a flag”=> Sorry my bad. Indeed it keeps ‘looping’ in the While loop.

I’ll keep that in mind…and i’ll try to ignore all the comments that say i can do it in much less code. thx

I asked for a link to the lib, not a puzzle. You're asking for help, and I presume you found it somewhere.
As for "compiling without complaints", even you should know by now that's not the whole story.

  • compiling, but with marginal space left in RAM, will soon lead to problems with even the hint of "String" usage, let alone your extensive use.
  • compiling is only the first step; even if ample RAM appears to be available, no one but you knows how much dynamic allocation is happening; without access to the libs, it's wasted time.
  • we have no idea if you have warnings set to none, or all, or somewhere in between; if you're suppressing warnings, either deliberately or through lack of IDE setting comprehension, it could markedly affect how much information is available to you for debugging. Compiler warnings are often less than clear, so (you profess to be a noob) expecting you to have understood the implications of such warnings is a stretch.

But, there's also this:

The thing that is most likely to get you a cold shoulder, by the way, is cursing, demonizing, and general peeing on everything in sight. It's a technical forum, not Facebook, so it's best to seek technical resolutions, not shoulders to cry on, or furniture to kick.

1 Like

The function I wrote is not the one I talked about in my earlier post. I've since examined your function more closely and your response to my question, then scribbled the demo.

try() is designed to be called once, and will return true or false.

true means it was successful.

false means it took four tries, or took too long.

It will block for five seconds, or longer if the called function takes longer to succeed or fail.

fails is a local non-static variable. It is initialised to zero on every call. On purpose. Every time you try to <whatever> you get five seconds or four tries.

If you started coding recently, you've got quite the sketch propped up there. I encourage you to find better learning sources that can get at some of what frustrates you.

Also, read read read these fora, and I do mean read the code also, closely.

And when it comes to things like doing something four times or for five seconds that you build the tiniest little sketches you can to explore and investigate different ideas about how to do them.

So you don't wonder why simple things aren't working… everything in that function "works". But that does not always add up to doing what you want.

The function I posted is a skeleton, you can decorate it however you want with real stuff. I could see returning zero for success and other values for what the caused the failure.

If you strip down your function to its essence, I believe you will see that it does not do what you want, and would be hard to fix. I did not do that.

I haven't read your entire sketch, but it looks like there are repeated patterns that could be exploited to make the code shorter, easier to understand, fix, modify and enhance.

There are sources for basic program design; as @gcjr says you can speak the language perfectly and it won't help you write a coherent letter to the editor, or whatever.

No good writer is not also widely read. No matter what she's writing.

a7

do you mean the PD0 and PD1 pins of the µC? So actualy the RX and TX pins that are used by the Arduino board for programming and serial monitoring (Arduino pin 0 and 1)? If that is the case…damn…why didn’t i think of that. I was in doubt to add some test points to my pcb but didn’t came up with that. Still…great tip! thx

Show us a screen grab of the IDE preferences/settings

Context is key. People will point out repeated code that should be generalized and made into a function, and other things that make code shorter.

Good.

People will dazzle you with for loops that have no body code, that do everything in one line just because it can all be shoe-horned into the syntax a for loop provides.

Bad.

Writing something in three lines that could be two if you did X or one if you did Y is not bad programming. In fact, it can be easier to write and read. Writing a few lines and then studying it to pack all of what they do into one line is time spent that will only mean more wasted time later as we (or you, the day after tomorrow) must spend re-figuring it out.

Comments are fine, but think twice when you have to comment what shoukd be obvious from carefully written code.

a7

I found this out the hard way. Many, many,….many hours of figuring out why it works as a stand-allone testsketch and not in my project code. That is why i’m so very scared of altering or adding to the code that works and f*cking it up forever (i tend to do that). It’s sometimes almost impossible to revert to the situation before the alteration…and if i try…i’m making it even worse!

Exactly. If have an Atmega328p mounted on a custom PCB, it's essentially an Uno without the UART <--> USB converter chip. You can connect that chip (using a cable as linked in my previous post) externally when needed for debugging.

But, maybe I'm misunderstanding something. Without access to the UART, how are you programming the Atmega328p on your custom board?

1 Like

I concur but my problem is that i don’t know enough about the ‘inner workings’ of what i’m programming. This means i could be stealing more ‘processing power’ or ‘memory space’ with this aproach (and getting comments about it). To be honest, if i know for shure i can cut resources in the code i’ll try it (i’m trying to balace reading-friendly code with it). But if the total code works within the possibilities of the device (µC) it’s also fine by me.

yes, using the ICSP pins (that are actualy a direct SPI connection to the µC). I use my Arduino uno R3 as a programming device and when i upload i use ‘sketch-upload using programmer’

https://docs.arduino.cc/built-in-examples/arduino-isp/ArduinoISP/

This sensor (dutch) and no angle (straith to the water surface).

The UART version is a A02YYUW.

You got me on this! For some reason, a few months ago (when i left coding aside while building a pcb with KiCad) the compiler warnings where set to ‘none’ by or for some unknown reason. So time to turn it on i would say.

Set to default. Also do a screen grab so I can see if any other settings are suboptimal, also your additional boards, many folks get that wrong and it is tricky to resolve.

There are issues in IDE 2.x that can cause it; root cause not determined yet so not solved. I have encountered this specific one as well. I got a "warning" because the serial monitor moved from the right to the bottom and inspecting showed that "compiler warnings" did also change.