Problems compiling

Post here first, or if you can't find a relevant section!
Post Reply
Rodrigo_2022
Posts: 5
Joined: Thu Jun 30, 2022 6:55 am

Problems compiling

Post by Rodrigo_2022 »

Hello, everyone.

I'm fairly new at this but I'm a very quick study.
Quick background info, I make furniture so I made a shop vac auto switch with a Nano, a current sensor and a relay. Everything wors perfect.

I removed the Nano because I wanted to do something else with it, so while purchasing some LEDs and resistors and such, I found the STM32F401 board on the seller website and it was fairly cheap compared to a nano so I got it.
I've been playing with it a bit and while it takes a few extra steps to upload a sketch to it, it does work as intended.

My problem however is that now want to use the STM for the shop vac (because I prefer to have the Nano free to tinker, rather than the STM, just becuse it's easier to use) but the code fails to compile. It compiles fine if I select the Nano board from the Tools Menu, but wheb I change itto the STM then it doesn't and throws a lot of error.

Since I'm not that knowledgeable, I can't tell what the problem is... Maybe I need to include some library?
I'm uploading the original code and two log files, with and withous verbose output.
As fas as I understand it, I just needed to change the pins in the code from the Nano to the STM according to howI intended to use them.

So there, any help appreciated, thanks!
Attachments
shopvac.rar
(5.8 KiB) Downloaded 81 times
by GonzoG » Thu Jun 30, 2022 8:12 am
Looks like there's an issue with max() function. I think it needs 2 values of the same type.
If you define analogValue as uint32_t, it will compile:
Go to full post
GonzoG
Posts: 403
Joined: Wed Jan 15, 2020 11:30 am
Answers: 26
Location: Prudnik, Poland

Re: Problems compiling

Post by GonzoG »

Looks like there's an issue with max() function. I think it needs 2 values of the same type.
If you define analogValue as uint32_t, it will compile:
Rodrigo_2022
Posts: 5
Joined: Thu Jun 30, 2022 6:55 am

Re: Problems compiling

Post by Rodrigo_2022 »

changing "int analogValue = 0" to "uint32_t analogValue = 0" on line 10 did make the code compile and upload but now it doesn't work, the relay it's closed (activated) upon start, I switched pins and even got back to the Nano to see if any of the components had gone bad and no. Something else must be wrong with code and telling the relay to activate. I disconnected the sensor to check if its readings were tripping it and nothing happens.

I tried everything my unexperienced brain can think of, changing values on the code, changing pins, etc. nothing works. I thought the code was supposed to be the same for both boards?
GonzoG
Posts: 403
Joined: Wed Jan 15, 2020 11:30 am
Answers: 26
Location: Prudnik, Poland

Re: Problems compiling

Post by GonzoG »

Check serial output for values you get.
STM32 are 3.3V MCUs, not 5V like ATMega on Arduino boards, so you may get different readings.
Rodrigo_2022
Posts: 5
Joined: Thu Jun 30, 2022 6:55 am

Re: Problems compiling

Post by Rodrigo_2022 »

forgot to mention that I can't check the serial monitor, unless I'm missing some config, it doesn't work through USB like the Nano, I'm werial for my serial to USB to arrive but that will takes weeks.
I could try using the multimeter I suppose.
GonzoG
Posts: 403
Joined: Wed Jan 15, 2020 11:30 am
Answers: 26
Location: Prudnik, Poland

Re: Problems compiling

Post by GonzoG »

It should work exactly the same as with Nano board. You have everything set up right.
You should see a COM port after you connect USB cable to board and computer (check device manager).
Rodrigo_2022
Posts: 5
Joined: Thu Jun 30, 2022 6:55 am

Re: Problems compiling

Post by Rodrigo_2022 »

Yes, I couldn't see it running because in order to upload I have to get the board into DFU mode, which for some reason doesn't work 80% of the time, I have to try a few times before getting it ready to upload, don't know why, then set it to COM1 and then try to upload, and after it's done, to see the monitor I have to go back to COM4 and I wasn't doing that.

Anyway, on the STM it reads around 710 every time. If I disconnect the sensor it goes down to 300 and if I start the heat gun (instead of what would usually be my table saw) the reading goes up to about 870.
o the Nano, however, the sensor reading is about 515, if I disconnect the sensor it goes down to about 220 and if I start the tool it goes up to about 628.

So, I already knew that the current threshold on the code was 520 so any reading below that wouldn't trigger the relay, I had already tried increasing this value and it didn't change anything, but for the sake of the sake I tried it again now that I knew the actual reading and still didn't work. I even increased it to 2000 just to see what happened and still the relay is active.

I just don't get it, the sensor is reading under 720, so if I set the threshold between 750 and 850, given that the heat gun takes the reading up to 870, it should work... yet it doesn't.
I've tried almost every pin on the board and it's always the same.


OK, now it works, instead of connecting the relay to the 5v pin I connected it to the 3.3v pin. Why this works? I'd love to know so I can learn. The relay is a standard 5v relay and on the Nano I do connect it to the 5v pin and works fine...
GonzoG
Posts: 403
Joined: Wed Jan 15, 2020 11:30 am
Answers: 26
Location: Prudnik, Poland

Re: Problems compiling

Post by GonzoG »

There is no COM port when MCU is in DFU mode. You can select any port and it will upload as MCU is identified by it's hardware ID not COM port.
You will see COM port only if you run a program that has CDC enabled.

Resetting those F4x1 black pill boards into DFU is tricky. It's a design flaw. For me it resets almost always if I'm touching USB port with finger while resetting board. But others found that they have to use something to press buttons so they won't touch anything on the board with theirs fingers.


As to why it works when connected to 3.3V and not 5V - I suspect you're using relays driven with PNP transistors or Adruino relay boards that are triggered with low (GND) signal. With this setup you need 5V signal to turn relay off. With 3.3V signal, there was always at least 1.7V on transistor base.
But this is just guessing, as I have no idea what exactly your circuit looks like.
Rodrigo_2022
Posts: 5
Joined: Thu Jun 30, 2022 6:55 am

Re: Problems compiling

Post by Rodrigo_2022 »

Yeah I noticed that there's no need for port when programming, but I have to select something, there's no no port option when there's at least one available, which in my case is COM1 (always shows), when the Nano is connected, COM3 becomes available.

Funny you mention that resetting the board is tricky, I thought I wasn't doing it right or that is was incredibly sensitive to the timing of pressing and releasing.
For me is the opposite, if I grab it with my hand as if it were the tv remote and use my thumb to press both buttons tilting to the right and left to get the order right I almost always get it right in the first or second try, but never more than 3. but being careful not to touch anything or using both hands to hold it or having it rested on the table, I could be pressing those buttons for an hour and it wouldn't work.

I'm attaching a schematic of the circuit, pretty simple and straightforward. It is in fact an Arduino relay, it is triggered by GND signal but in the code you can change that, also when wiring you can set it to NO or NC, so I guess you choose whatever combination of parameters to get the results you want.

So in the end, I just had to define the function as an unsigned integer instead of just integer and use the 3.3v pin instead of the 5v pin.
May I bother you with explaining the integer part of the deal? as I said, I'm as green as they come with programming but this is how I like to learn.
Attachments
Shop Vac Auto Switch.jpg
Shop Vac Auto Switch.jpg (62.4 KiB) Viewed 1532 times
GonzoG
Posts: 403
Joined: Wed Jan 15, 2020 11:30 am
Answers: 26
Location: Prudnik, Poland

Re: Problems compiling

Post by GonzoG »

Arduino core is made to be as simple as possible so ppl don't have to worry about variable types (in most cases). Also many functions are very simple, or even just a simple macros (like max()).
STM32 core is "a bit" more advanced and some simple functions are more complex. Like max(), which is a standard C++ max() (std::max()), not just a simple bigger/smaller comparison.

Also you used int as first value (int32_t), which has 31 bits for value and 1b for sign, and uint32_t as second value.
There is no safe way of converting uint32_t to int32_t, because uints can store 2 times bigger values.
And in C/C++ every operation is done on type of the first value.
eg.

Code: Select all

uint16_t a= 10;
float b=11:
float result = a / b;
result = 0, because there is no fractions in integer values.
You need to cast first value to a floating point type.

Code: Select all

float result = (float)a / b;

As to your relay board - those are activated by low signal and it cannot be changed. Those have PNP transistors or optocouplers hardwired to Vcc, and you activate them providing GND.
As to those NO/NC outputs - those are 2 outputs from switching relay (SPDT - Single Pole Double Throw), which means that it has one input and 2 outputs, that you can switch between or use it as a SPST relay that only switches power on, or only switches power off.

I hate those boards, especially for use with Arduino (all boards type, AVR, SAMD, STM32, etc) and high current or high voltage operations. It's because when you set pin to work in digital output mode, it's by default set to LOW state, and this activates relay. You have to set this pin to HIGH state and it takes time (most ppl use digitalWire() which is really slow and they forget to do this in setup() ).
This means that for some short time relay is on and before every thing is checked, your machine is on, even when it's not safe to turn it on.
Post Reply

Return to “General discussion”