Help needed for my ...
 
Notifications
Clear all

Help needed for my stepper-ballscrew project

132 Posts
9 Users
17 Reactions
7,091 Views
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2585
 

@bradscopegems 

OK, I've cleaned up the sketch, added more comments and cleaned up some errors. This has compiled clean on my Mac which was set up to compile for an Arduino NANO. I believe it's simple enough that it should compile for any of the NANO, UNO or MEGA family members.

There's nothing in the loop() code because no behaviour is known except for homing the system.

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


   
ReplyQuote
(@davee)
Member
Joined: 4 years ago
Posts: 1871
 

Hi @will,  (cc @bradscopegems )

  Sorry my message suggesting a design with switches was written at the same time as you wrote yours suggesting a fixed there and back might do... they then crossed in the 'Ether'.

In reality the two suggestions are both heading in the same direction ... I just added two more switches and a bit more software thinking a 'fine adjustment' was probably going to be helpful. Presuming a simple 'standalone' box is required as an end product, I think you suggestion would have two push switches and two microswitches, and I recommend the same resistor and capacitor for each, to minimise problems of noise and switch bounce.

Please forgive my intrusion.

Best wishes, Dave


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

@davee 

No reason to be sorry, neither proposal is wrong; they're both just premature 🙂

Incomplete specifications.

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


   
DaveE reacted
ReplyQuote
Bradscopegems
(@bradscopegems)
Member
Joined: 1 year ago
Posts: 25
Topic starter  

@will    Wonderful!  Your cleaned-up version compiled perfectly on my Arduino Uno.  I am very excited, but will postpone uploading your sketch to the hardware until the morning, because I need to sleep now to have quick reflexes ( to do the testing without risk).  Thank you so much for this. I will report the results tomorrow.


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

@bradscopegems Why are you creating a docx file, just post (with Attach Files) the ino file you compiled.

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, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
ReplyQuote
Bradscopegems
(@bradscopegems)
Member
Joined: 1 year ago
Posts: 25
Topic starter  

@zander   Thanks for pointing this out! I had no reason to send a sketch to anyone until I discovered this forum, but when I searched the web for a way to print  sketches I found all kinds of arcane methods. A few years ago someone posted a trick (that actually does work if your IDE is of the right vintage) whereby one opens the file to send, then clicks on 'Edit' and then 'Copy as HTML' and pastes the Arduino file into Notepad. This gives one dozens of lines of incomprehensible text.  The next step is to resave to one's Desktop, giving a name such as MySketch2a.html.  If this is opened in one's browser, all the colours and format of the Arduino file reappear like magic and you can paste the result into a word file.  My method was simpler but basically similar. But , of course, you are right. If the person or group you are corresponding with has access to a computer loaded with the Arduino IDE, they can just be sent the sketch file directly and can open it. I tried this for the first time on receiving your message and noticed there was a little bit of ritual about the sketch file having to be in a folder of the same name, but I agree it was very easy and the colours are not lost and I will use your method from now on. It is a pity that printing and sharing is not mentioned in the Banzi bestselling Arduino book, although Parmesan and glue-sniffing are mentioned....


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

@bradscopegems I spoke to the development team and they admit the post as HTML is broken. Sadly they must be swamped because a fix was supposed to be done several months ago. Many of us on the forum post sketches frequently. The way I learned is to go to the practice forum and try all the methods that the IDE makes available (really only 2 with a couple of trivial modifications). If you find it a problem, open the file in the IDE, click Tools, then Archive. Post the zip file via the Attach Files dialogue.

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, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


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

@bradscopegems 

Here's a better version (this one has a correction, so it may even work 🙂

Note that there's nothing in the loop() so all this sketch will do is start up, home the stepper and then pause indefinitely. To be safe, you should start with the carriage some distance away from the end and press the limit switch manually to verify that it stops the carriage. If it doesn't cut power immediately.

I've added a 2 second delay into the setup() subroutine to allow time for all of the components to stabilize before performing any operations. This also allows a 2 second window during which it appears to be easier to start a compile and upload to add a new sketch onto the MCU.

This version was set up to use an UNO (instead of the original NANO) and compiled clean again.

The error I fixed was that in the homing routine I had omitted calling the stepper's run() command to tell it to take a step. The first line tells the stepper to move backwards one hundred thousand steps. It then goes into a while loop for as long as it has steps to go (i.e. it hasn't reached -100,000 steps yet). In the loop it checks to see if the limit switch is down (i.e. value is HIGH). 

If the switch was triggered, then it tells the stepper that it's 10 steps past zero and returns. This means that the carriage won't need to bump into the switch every time it's sent back to the origin. If it wasn't't triggered then it invites the stepper to move another step if it still has a way to go and enough time has passes since the last step.

Once the origin has been set, the stepper can be positioned very accurately by giving it an absolute step count, which is why one usually performs the homing at the beginning of the sketch. From then on you can move the stepper to predetermined locations very easily and precisely.

I've also added some more comments to help show you what's happening and where in the sketch.

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


   
DaveE reacted
ReplyQuote
Bradscopegems
(@bradscopegems)
Member
Joined: 1 year ago
Posts: 25
Topic starter  

@will Alas, no joy this morning. When I run your latest sketch ( the one you sent while it was about 2 am in England) the motor does nothing, whatever I do with the switch. If I press the reset button on the Arduino while the motor power is on , the motor jitters, producing a horrid growling noise.    I have checked the connections and they are all as they should be. Older sketches work fine, so there is nothing fundamentally wrong with the motor wiring.

Brad

 


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

@bradscopegems I know @will well, and he is not usually up at 5AM so let me offer some ideas. Will said he got a clean compile, that usually means he did not run the sketch. He may not have the equipment, or he may not be sufficiently motivated to hook it up. I will bet he did show you the basic concepts but it is up to you to complete the job.

Study what he sent you and merge those ideas into what you already have working.

The concept is quite simple, move in one direction until the switch is closed, then stop and back off a wee bit. Optionally note that position as the limit. 

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, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


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

@bradscopegems I just took a harder look at the sketch. My lab isn't here yet, it's being shipped but I am wondering about the usage of SWITCH_A in the sketch. The pinmode is just INPUT. Here is what the manual ( Manual) says about that

This also means however, that pins configured as pinMode(pin, INPUT) with nothing connected to them, or with wires connected to them that are not connected to other circuits, will report seemingly random changes in pin state, picking up electrical noise from the environment, or capacitively coupling the state of a nearby pin.

 

The way I generally do that is to use INPUT_PULLUP, then wire the pin to C on the micro switch and the N/O to ground. Now just invert the reading logic, as in 

if (0 == (digitalRead(SWITCH_A))) { // If limit switch triggered, we're at the beginning
 

I might be off base, but my spidey senses are telling me the sketch is logically correct. It's just that the 'high impedance' phenomenon might be biting you in the proverbial axx.

 

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, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


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

@bradscopegems This is worrisome. It's not ritual, it's how the IDE works. 

 there was a little bit of ritual about the sketch file having to be in a folder of the same name, 

Look at the screen grab, that is where you tell the IDE the location of ALL your sketches (plus a few other things). Then the RULE is every new sketch with name XYZ is stored in a folder called XYZ in the 'Sketchbook location' and in that folder is a file called XYZ.ino. Nothig magical or special, just arbitrary rules to avoid chaos like drive on the right(left in UK), that way we avoid collisions.

Yes, mine is longer (more sub-folders) because of the way my files are organized.

Start at arduino.cc and read the documentation for pearls of wisdom like this.

Also read the Help for this forum, see 2nd pic. It's up and right from here.

Screenshot 2023 06 29 at 09.44.31
Screenshot 2023 06 29 at 09.52.07

 

 

 

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, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
ReplyQuote
Bradscopegems
(@bradscopegems)
Member
Joined: 1 year ago
Posts: 25
Topic starter  

@zander   Dear Ron,

                                  I was worried about floating voltages, so I have wired the switch input (pin 3 on the Arduino) so that it is connected to digital GND via a 10K resistor, unless the switch is closed, in which case it is connected directly to 5V on the Arduino. Is this not the correct thing to do?


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

@bradscopegems It might be, it's not what I do. INPUT_PULLUP applies 5V to the pin no need for a resistor, it's built into the board.

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, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
ReplyQuote
(@davee)
Member
Joined: 4 years ago
Posts: 1871
 

Hi @bradscopegems & Ron @zander,   SORRY 2nd CORRECTION

Late addition to this note, as Ron & I are writting conflicting messages at the same time:

 Sorry Ron, but as I was writing this I see you added a message including "no need for a resistor" ... for switches on leads away from the PCB that the Arduino processor itself is soldered to, this is incorrect. This is very poor practice. Please read on:

@bradscopegems  CORRECTION: You say you have a 10k resistor pulling the input to the 5V line on the Arduino, and the switch will connect to ground when closed , as per the circuit diagram I published in a previous message .. SORRY: I have just reread your message and realised you have connected the resistor to ground and the switch pulls up to 5V .. this should work if you DON'T use INPUT_PULLUP ... if you do use it the result is anybodies guess ... but I strongly recommend my configuration, which only means swapping two connections, and making sure the software is looking for high when switch is open low when it is closed.

-------------

  I haven't yet looked at the sketch, but it would be unusual for a system with only 'enable' switches to cause the motor to just 'growl' due to the enable not having a pull up, although it is plausible and stranger things have been known, so I don't rule it out.  As you have now added that you have a pullup resistor, presuming it is actually connected (breadboard contacts can sometimes fail etc.), this is now much less plausible.

  Ron is correct in saying every switch will need a pull up if the closed switch connects to ground. The use of INPUT_PULLUP will often 'work' if the switch is local to Arduino, and there aren't any local sources of electrical noise. However, the internal pullup resistor, whose value is usually very loosely defined in the data sheet, and which varies with different processors, is too high for reliable operation - It is only meant for occasions where any wiring to the pin is on the same PCB, not for when there are flying leads to the switch etc.

Also, it is liable to suffer from switch bounce problems. Hence, I recommended that any switches (push button or  microswitch) are supplied with a an external pullup resistor and a small capacitor. With this circuit, it doesn't matter if you use INPUT_PULLUP or not.

I re-iterate, my circuit suggestion is for 'reliable' operation ... it is possible to cut corners and just use INPUT_PULLUP, and you may get away with it, but it is likely to give problems in the future, like triggering when another piece of electrical equipment is switched on or off.

Best wishes all, Dave


   
ReplyQuote
Page 5 / 9