I am new to this forum and this is my first post here. I am a newbie when it comes to programming. So far I have done everything by assembling parts of code that others wrote.
So I am building a controller from an Arduino NANO to control the current flow of a rotary Eddy current brake. The brake is directly connected to the flywheel of the engine with a flex shaft. The eddy current brake has a RPM variable reluctance sensor with a 60-tooth wheel. The signal is passed through a MAX9926 to input pin D8 to enable interrupt input capture for precise measurement. Timer 2 is used for the PWM output with pin D11 and OCR2A to control the amount of braking. Torque is measured with a HX711 module on pins D2 & D4, and atmospheric data with BME280 module via I2C interface on pins A4 & A5. 2 NTC sensors are connected to A0 & A1 for analog readout to measure coil temperatures. All the data is being sent to LABVIEW for monitoring and logging, and I also control the arduino from LABVIEW via serial communication.
So far I managed to make all the parts of the code do what I want them to do... I used Nick Gammon's code (Reply #12) for measuring the RPM signal. I also found a nice code for parsing the serial data received at serial port.
I used the standard PID library but changed to FastPID library to make it easier on the arduino for computation.
I have several issues that I can't resolve. One is printing float variables is very unreliable. Sometimes it prints the whole value but most of the time prints only part of the value (example: value=2500,prints 25 at serial port). Printing integers at serial port is very reliable.
The other problem is when I combine everything, I start running into a problem where the arduino seems to reset itself every second or so... I see on the internet that it might be a RAM issue. The way it behaves is that it loops back into the Void Setup () part and runs the serial.print part in void setup multiple times...
I have attached the INO file since it exceeds the 9000 characters limit.
Any suggestions on how to optimize the code would be very helpful!
I see on the internet that it might be a RAM issue.
So what does it say in the console after the compile. It will tell you how much RAM you have and how much you used and give a warning if you are close to the limit.
On the other hand it might equally well be lack of decoupling capacitors causing voltage spikes to reset the Arduino. Once you have an engine it can generate all sorts of nasty stuff that can upset the Arduino like this.
Sketch uses 14534 bytes (47%) of program storage space. Maximum is 30720 bytes.
Global variables use 737 bytes (35%) of dynamic memory, leaving 1311 bytes for local variables. Maximum is 2048 bytes.
The problem is not related to the engine, or electrical noise. The problem occurs even when I am in a different location with only the arduino connected to my computer...
I have tried the code on several different arduinos and all have the same issue.
Those memory figures look fine as they are. However, the dynamic memory allocation does not always pick up the full amount used due to reasons we can go into if you like but are not relevant at the moment.
The problem is not related to the engine, or electrical noise.
That is a very bold statement statement to make. One which I think you have no evidence to support.
The problem occurs even when I am in a different location with only the arduino connected to my computer.
Will it even run like this? Using a PID algorithm requires things for feedback to work.
What sort of construction do you have. Solderless bread board is not very reliable with its connections.
It is unusual for a software error to cause a reset of a processor because these are Harvard architecture machines which means the program space and the data space are two separate memory maps and can not be mixed. The only way to effect a reset is to vector through address zero, which while not being impossible is an unlikely thing to happen due to faulty software.
Mike, the program will run without any of the modules attached to the arduino. The only issue is the HX711 readout that occurs in every loop (instead of at 80Hz) and prints a value of 0. I have attached a printscreen of serial monitor where the reset occurs. The arduino is currently only attached to the pc (via usb cable).
The thing is, I haven't tried it yet with the engine running, so far I tested only using a power drill to run the eddy current brake.
If you can, upload the code to an arduino nano or uno and you will see the error yourself. You should comment the part to skip the readout of HX711 :
if (scale.is_ready()) {
T = scale.read();
Serial.print("[Tq");
Serial.print(T);
Serial.println("]");
}
The program seems to run fine (without resetting) when I comment out the part regarding the BME280 data print. So I have to try with the module connected. I remember that the module was working previously but the last time I tried with the module it was not working and that is when I noticed that the arduino was reseting instead of printing the values for atmospheric data.