Continue reading values from sensor inside a "case"

Hello everyone,

This is my very first post on the forums and I apologise in advance for any possible mistakes, however, I will try to make this as clear as possible.

I’m working on a tensioning device as part of my final engineering thesis. In very basic terms, I need to be able to find the tension in a thread then adjust it according to the required value which is preset by the user.

The components I am using to achieve this are Stepper motor, load cell and IR remote.

The function is as follows:

The thread starts from the back beam, goes under the intermediate beam and finally through the opening on top of the load cell. Reading the “weight” on the load cell it can be translated into tension in the thread. If the current tension is lower than the required, a signal is sent to the motor to rotate clockwise UNTIL the tension is achieved. Please refer to the 3D model that I have attached.

Individually, all the functions on the Arduino work great - readings from the load cell are correct and motor works perfectly when controlled from the remote.

However, I want the system to do all that when i press “Play” on the remote. When i transfer the code (that works) inside the “case” when play is pressed, the code seems to only consider the very first value from the load cell - the readings are not changing. Hence, if the system is under-tensioned, when i press play, the system will correctly detect that but it will make the motor rotate forever since the value will not change even if more load goes on the load cell.

Is there something wrong with my code or is there a special way to tell the program to continuously monitor the sensor value?

Again, I apologise if all this was too much information or not detailed enough. Looking forward to your replies and thank you in advance.

I have also attached my code.

Fiinal_Pogram.ino (9.28 KB)

Code was not attached, just a picture of the mechanical setup.

Don't try to measure the sensor "again" inside some control structure. Concentrate on getting your loop() function to run thousands of times per second. Never stop and wait for anything. If you need specific times, then record the time you did it last and then check (thousands of times per second) if it's time to do it again.

So your important sensor gets read at the top of the loop() and every thing under that can make a decision with that value. If it's time to stop or start the motor, then do so, but don't wait for the motor. Just let it go to the end of loop() and it will go back to the beginning and read the sensor again.

Whoops - So sorry guys, I have now attached the code. Thank you for the prompt reply.

MorganS:
Don't try to measure the sensor "again" inside some control structure. Concentrate on getting your loop() function to run thousands of times per second. Never stop and wait for anything. If you need specific times, then record the time you did it last and then check (thousands of times per second) if it's time to do it again.

So your important sensor gets read at the top of the loop() and everything under that can make a decision with that value. If it's time to stop or start the motor, then do so, but don't wait for the motor. Just let it go to the end of loop() and it will go back to the beginning and read the sensor again.

I apologise but I'm not too sure what you mean by that. The sensor value is correctly shown before the remote button is pressed. However, whatever that initial value remains constant inside the "case". This means that even if the motor did the job it will keep rotating forever.

pthrasou:
I apologise but I'm not too sure what you mean by that. The sensor value is correctly shown before the remote button is pressed. However, whatever that initial value remains constant inside the "case". This means that even if the motor did the job it will keep rotating forever.

I agree with @MorganS. You need to structure your program differently. In very simple terms your loop() function might be

void loop() {
   readSensor();
   controlMotor();
}

and if that repeats hundreds or thousands of time per second there will always be an up-to-date sensor value for the motor code to act on.

Have a look at Planning and Implementing a Program or the shorter Several Things at a Time

...R
PS. Please use the AutoFormat tool to indent your code consistently and make it easy to read.