Stuff I learned today

Well, that was a productive rabbit hole.

Recently I found some Arduino quantizer code out in the wild and I said, “Cool, Imma use that”. Messed around with it a bit on my Linux box before putting it on an Arduino. One thing was mysterious for a while: the same code worked on Linux and not on Arduino, and I traced it down to these two lines:

int distance = 32768;
...
if (distanceTemp > distance){

See the problem? Hint: I changed the constant to 32767 and it worked.

The other thing was that the code was using about 1500 bytes of dynamic memory out of 2048, storing information about scales. If you have constant data in your software, it’s stored in program memory (flash storage) but when you go to run it it gets copied to dynamic memory (RAM) for use, even though you’re not changing it. I did ditch some of the data it was storing since I didn’t need it and got it down to 1000 bytes. Then I went googling and learned about <avr/pgmspace.h> which lets you tell the system “keep this data in program memory” and gives you tools to access it. Took a while and more googling to figure out the details but in the end the code was using only 260 bytes of dynamic memory. I’m not sure I couldn’t have lived with 1000 bytes but at least now I’ve learned something potentially useful.

5 Likes

Something I learned today.
I could use what I learned in making a mixer/Muliple. In the CGS joystick circuit I am working on. I am adding Offset LEDs Blue and Red for -voltage/+voltage indication.
I could use a non-inverting amplifier too not only not Drag my CV signal down, but can boost the Signals Gain. So It can accurately display the voltage on the led Below The LED Forward voltage(+1.7VRed)(-3.3Blue).

1 Like

lol signed INTs. 2’s complement’s a bitch when you forget yeh?

2 Likes

The only thing worse than signed ints is unsigned ints.

unsigned int i;
...
for (i = 1000; i >= 0; --i)
1 Like

I can think of something worse than signed / unsigned INT’s…not knowing what an INT is. :upside_down_face:

2 Likes

Well I know what 2’s Complement and Signed is… But apart from discussin briefly with my 15 year old last year… last time I realy thought about them was in 1992…

But it came flooding back…

2 Likes

INT = Integer.

Computers store data in a lot of common ways, in this case, a number is stored as an INT. This usually just means that its a number with no decimal place.

What analogoutput experienced was a result of computers only being able to store a value of a given size. Without being to technical here, the INT is capable of storing a value from -32,768 to 32,767. This is all assuming you are talking about the C programming language, and things based on it’s standard data types (really most everything is). The “signed/unsigned” part is just saying it either supports negative numbers or not. “unsigned” is when its only zero and positive, and “signed” is when it has both, but you effectively half your overall positive numbers. Modern computery deals have larger capacity to store bigger numbers, which is why there was an issue.

@analogoutput yeah, i dont typically have to worry about that shit either. Its only when we go to more low level shit like this right?

3 Likes

Great, something I learned today! :slight_smile: Thanks @Caustic!

Clearly programming is not my field, even though I’m MERN stack certified. My degree is in 3D Computer Animation but I’ve spent my entire life working in the music industry for major labels or promoters in the artist development / marketing and Artist Support roles. All that means is I have had to babysit a lot of royal assholes. I’ve also worked with some amazing people. They do exist, but they are few and far between. So I may not know or understand synth DIY very well or computer programming, but ask me about anything music industry related and I’m your specialist :slight_smile:

4 Likes

I came across a different but similar problem.

I was writing some code for a DMX interface ( sadly all my code was lost when my HDD died on boxing day ). I was writing the part that allowed the DMX address to be set with buttons and then be stored on the DUINO Non Volatile Memory, so when it rebooted it would get the saved address… All Sweet :slight_smile:

Except sometimes, randomly (It Seemed) the address would not be correct after a reboot.

It realy should have twigged much sooner, much much much sooner… DMX to give it it’s fuller name is DMX512 ( as it can send/recive upt to 512 channels of data…)

512 in binary is a 9 bit word… 111111111
the duino memory bytes are standard 8 bit words, so could only store saved addresses up to 256…

So every time I saved the DMX address it lost one bit…

Resolved by storeying to 8 bit Bytes and all was good… ( until the HDD failure :frowning: )

I have been programming for 38~ years, but not more than a little bit hear and there (apart from some hefty Linux shell scripts)… But doing more and more with the Arduino

Rob

In particular, on my Linux computer, an int is 32 bits long, which is enough to store numbers from -2147483648 to 2147483647, so it has no problem storing the number 32768 in an int. Then when it went to compare 5 to 32768 it found 5 was smaller. But on the Arduino an int is 16 bits, enough to store -32768 to 32767 and when you set an int variable to 32768 it happily tries to do so but the result is it gets set to -32768 instead. Now when you compare 5 to that it says it’s larger and the software doesn’t do what you wanted it to.

Hard drive failures are nature’s way of reminding you to get a github or bitbucket account, commit your changes often, and push after every commit.

And a second drive, and backup software.

I thought you were just being case sensitive, like most modern programming languages :stuck_out_tongue_winking_eye: (your average C/C++ compiler isn’t likely to know what an INT is, the fundamental type is spelled int).

2 Likes

PROGMEM is a real good thing to preserve memory. I use the F-macro quite a bit for string message my sketches need to print.

I’m still puzzled by fixed point arithmetic. Has anybody ever worked with that? I need to use it for an FPGA project.

oh you. I was just doing that for emphasis!

Ah right, fixed point arithmetic is basically a way to have fractional values with integer instructions.

Just like how you need to convert things with a 2’s complement result to get negative numbers you do something similar for the fraction result.

Nutshell: fixed-point ints are 32-bits, with the upper 16 bits as the int part, and the lower 16 bits the fractional part.

Do you already know about 2’s complement?

Whenever I try to be emphatic with a compiler it only makes things worse.

1 Like

It cares not for your human mushy feelings.

3 Likes

Yep, ik know about 2’s complement ( 20 character response ).