Notifications
Clear all

Stepper and load cell

121 Posts
8 Users
23 Likes
1,462 Views
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6512
 

@the-apprentice I am not sure if you have seen the Load Cell library documentation yet so here is the link https://bityl.co/O65V . Lot's of good info here including general program flow, and how the load cell should be used. A BIG part of this kind of programming is learning about the devices. This is the holy grail for load cells, study it and learn.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PLI/1, Pascal, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6512
 

@the-apprentice I used the examples from the libraries and produced the following. It compiles but needs work. The comments tell the story.

/*
  I used the load cell example for read_medavg and the stepper example for setSpeed.
  I merged them and added a couple comments for you to deal with
  The key things to work out are
  1. What math to apply to the value returned by read_medavg to convert it to a speed setting
  2. Where to place and how to use a pot to establish a starting speed. 
    Possibly in setup I have included sample code commented out
*/

// Load sensor stuff
#include "HX711.h"
HX711 scale;
uint8_t dataPin = 6;
uint8_t clockPin = 7;
uint32_t start, stop;
volatile float f;

// Stepper stuff
#include <Stepper.h>
const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution for your motor
int stepCount = 0;                   // number of steps the motor has taken
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

void setup() {
  Serial.begin(115200);
  Serial.println();

  /*
  You will need to determine the values to use here experimentally
*/
  // read the speed pot value to establish initial speed
  int sensorReading = analogRead(A0);
  // map it to a range from 0 to 100:
  int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
  // set the motor speed:
  if (motorSpeed > 0) {
    myStepper.setSpeed(motorSpeed);
    // step 1/100 of a revolution:
    myStepper.step(stepsPerRevolution / 100);
  }

  scale.begin(dataPin, clockPin);

  /*
  The comments following are those of the original author and I have no idea what they mean
*/
  // TODO find a nice solution for this calibration..
  // load cell factor 20 KG
  scale.set_scale(127.15);

  // load cell factor 5 KG
  // scale.set_scale(420.0983);
  // reset the scale to zero = 0
  scale.tare();

  start = micros();
  f = 0;
  for (int i = 0; i < 100; i++) {
    f = scale.read_medavg(7);
  }
  stop = micros();
  Serial.print("100x read_medavg(7) = ");
  Serial.println(stop - start);
  Serial.print("  VAL: ");
  Serial.println(f, 2);
}

void loop() {
  // continuous scale once per second
  f = scale.read_medavg(7);
  Serial.println(f);

  /*
  rework the following to use the value returned from the load cell to control the speed instead of a pot
  don't forget to possibly add a pot into this to set an initial speed, maybe only in the setup code?
  You will need to experimentally determine what math to apply to the values returned from the load cell
  that will make sense to the setSpeed function. Something like map might work
*/

  // You need to experiment and determine the best function and arguments to use
  int motorSpeed = map(f, 0, 1023, 0, 100);

  if (motorSpeed > 0) {
    myStepper.setSpeed(motorSpeed);
    // step 1/100 of a revolution:
    myStepper.step(stepsPerRevolution / 100);
  }
  delay(1000);  //RCA needed???
}

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PLI/1, Pascal, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2502
 

Posted by: @the-apprentice

@will Thanks for helping. It seems the loadcell reading only runs once

The sketch updates the speed from the load cell by using the commandΒ 

units = scale.get_units(); // Read scales
Β 
every time the loop runs. Perhaps this is not the proper way to read the load cell. I don't know that because I don't have the library for it.

If this is not the correct way to read the cell, then please change it to the proper code. Also, you may need to adjust the limits used for the load cell's range if 0 and 5000 (readings in grams) is not the real range of the load cell's output. The range needs to be expressed in the actual values it produces if these are different from the weights represented.

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6512
 

@will I posted a new version that uses sample sketches from the stepper and load cell libraries. The interesting one is the load cell, it has about a dozen samples but the read median average seemed like a good fit. The OP will need to apply some sort of math to convert the result returned from the load cell read into a stepper speed. He also should add a few lines of reasonable checks on the delta change being within some pre-determined amount.

This was a quick and dirty, other functions in the library might be better and perhaps some way to gradually change the stepper speed in order to not break whatever the load cell is measuring.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PLI/1, Pascal, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
The apprentice
(@the-apprentice)
Member
Joined: 2 months ago
Posts: 47
Topic starter  

@zander I have run thru the examples but nothing works.


   
ReplyQuote
Marvin
(@rob42101)
The Paranoid Android
Joined: 3 weeks ago
Posts: 30
 

This may not be appropriate, or relevant: I'm new here and have (as yet) little understanding, so if this link is of no help what so ever, then please excuse me, but here goes...

I have been made aware of a very useful (to me, least ways) website, from which this tutorial shows how to control the direction and speed of a DC motor using an ESP32 and the L298N Motor Driver

I trust that it will hep you out, but (as I say), it may not.

Β 

to add: ah... I see now that what you're trying to do is to control a stepper motor, not a DC motor and as such, that link may not be of help, in this case, but I'll leave it as is, just because it may be of help, even if not in a direct way.

This post was modified 2 weeks ago 2 times by Marvin

The only thing necessary for the triumph of evil is for good men to do nothing.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6512
 

@the-apprentice In that case perhaps drop back and punt.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PLI/1, Pascal, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6512
 

@rob42101 All the libraries come with examples, and then of course there are the demonstrations that Bill (dronebot-workshop) does with the accompanying article with a downloadable zip of his examples but even though he has been made aware of all of that he apparently can't 'get it to work'. I doubt that another example is going to help him, some people are just not problem solvers.

BTW, the randomnerds are a resource we frequently point newbies to plus 'The Swiss Guy', Ralph Bacon, and Paul McWhorter. I assume even a newbie can use any search engine to find a YT site given a name.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PLI/1, Pascal, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
Marvin reacted
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6512
 

@the-apprentice Pretend that you are a mechanic and I call you saying 'my car doesn't work'. Do you think the mechanic can fix your car given only that information?

Put the link to the code you used here, links to the hardware used. Also a link to the circuit/schematic/wiring diagram.

Now be ready to answer questions like what is the voltage at point X on the circuit.

Good luck.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PLI/1, Pascal, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
Marvin reacted
ReplyQuote
Marvin
(@rob42101)
The Paranoid Android
Joined: 3 weeks ago
Posts: 30
 

Posted by: @zander

I doubt that another example is going to help him, some people are just not problem solvers.

At times, I've found that reading something about a topic (or watching an alternative Video), from a few different source, can be helpful: different authors, have different styles, and a different style can trigger an understanding that was somehow missed by the reader/watcher, as good as the writing/presentation may have been, by the author/presenter.

I agree that one needs to be a problem solver for this kind of hobby; it's the same with coding.

The only thing necessary for the triumph of evil is for good men to do nothing.


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2502
 

@rob42101Β 

Easy for you to say Marvin, you have a brain the size of a planet πŸ™‚

This forum seems to be a place where members try to help other members solve problems and move past obstacles, but NOT a place to get custom software.Β 

@the-apprentice has beenΒ forthcoming in stating that he's not much of a coder, but my impression from the code he has posted is that he's closer to newbie than just inexperienced, judging from the glaring errors in his code and things like not even recognizing that theΒ stepper motor was turning slowly. That's OK, we all have to start somewhere.

So, some people have been posting code trying to approximate the inadequate specifications that he has provided. But, as Ron (@zander) points out, we're getting nothing back to help progress. For instance, TA's reply "I have run thru the examples but nothing works" doesn't provide any new information (or, indeed, motivation) for anyone to do any work towards making a better. He neither specifies WHAT examples he ran (I.e., stepper or load cell) nor how they failed.

Ron is not trashing him, he's simply trying to impress upon him the need for more detail. You will realize the need for accurate and full requirements before you start coding because of your background, but a non-coder doesn't understand that and so makes it almost impossible for them to order a set of commands to make a computer do something. Until TA realizes that everything has to be explained (to the computer) in exquisite detail and in the proper order before it can do what he wants, he'll struggle. That's what Ron's trying to convey.

Anything seems possible when you don't know what you're talking about.


   
Ron and Marvin reacted
ReplyQuote
Marvin
(@rob42101)
The Paranoid Android
Joined: 3 weeks ago
Posts: 30
 

Posted by: @will

Ron is not trashing him, he's simply trying to impress upon him the need for more detail.

Indeed. I hope I didn't come across as having that impression. I understand that all anyone here is trying to do, is to help out. And that's all I was trying to do: I'm not passing judgement on anyone. 🙂Β 

The only thing necessary for the triumph of evil is for good men to do nothing.


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2502
 

Posted by: @rob42101

I'm not passing judgement on anyone..Β 

I don't feel that you are at fault in any way at at all.

I'm hoping that this will help explain things toΒ @the-apprentice and a lot of other new members of the forum. Quite often, people ask for help and give little, no, or confusing information. Then, when people try to explain why it's inadequate, impossible (or just wrong) they feel that they're being insulted and complain that the forum is belittling them or calling them stupid.

Β 

Anything seems possible when you don't know what you're talking about.


   
Marvin and Ron reacted
ReplyQuote
The apprentice
(@the-apprentice)
Member
Joined: 2 months ago
Posts: 47
Topic starter  

I'm taking all off this and trying to learn from it. I know I'm not the brightest mind but like trying. Sometimes it works and then I feel great and itΒ  motivates me to do another project. Thanks to everyone for trying to help me out. I will get it eventually. I hope. lol


   
ReplyQuote
The apprentice
(@the-apprentice)
Member
Joined: 2 months ago
Posts: 47
Topic starter  
Capture

I hope this helps.Β Β 


   
ReplyQuote
Page 4 / 9