Author Topic: stepper sketch  (Read 2253 times)

0 Members and 1 Guest are viewing this topic.

Offline m3vuvTopic starter

  • Super Contributor
  • ***
  • !
  • Posts: 1738
  • Country: gb
stepper sketch
« on: November 11, 2019, 08:10:30 pm »
Hi all,im making a coil widing machine,at the moment it has just 1x nema 23 hybrid stepper working the actual winder  that turns the spool with the set number of turns etc,I need to drive a 2nd small bipolar stepper to move a wire guide back and fourth to layer the windings,i have a small steper complete with a nut and threaded rod from a dlp laser projector focusing unit i want to use,i have the psu,a uno with a ver 3 cnc shield with drv 8825 drivers and some micro switches,i need to know what i have to put into the code posted to make it work,it needs to start cw until it trips a limit switch then reverse untill it hits the other switch then reverse againe,so basicaly just back and fourth,i need to be able to controll the speed as well,can anyone help?,thanks in advance.
code as is:#include <A4988.h>
#include <BasicStepperDriver.h>
#include <DRV8825.h>
#include <DRV8834.h>
#include <DRV8880.h>
#include <MultiDriver.h>
#include <SyncDriver.h>

#include <Stepper.h>

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}
 

Offline Buriedcode

  • Super Contributor
  • ***
  • Posts: 1954
  • Country: gb
Re: stepper sketch
« Reply #1 on: November 12, 2019, 07:08:59 am »
Did you leave out some code when you copied/pasted it?  It looks like you've got a blank Arduino sketch with some libraries included - and thats it.   I'm not sure anyone is going to write something all for you, especially if you haven't even tried yourself.  If you're struggling where to begin, start small - a simple loop that controls a stepper, sending it one direction, then the other, with delays in between so you can see whats happening.

Also, please put a space after a comma, and use upper case letters occasionally - they are free.
 

Offline m3vuvTopic starter

  • Super Contributor
  • ***
  • !
  • Posts: 1738
  • Country: gb
Re: stepper sketch
« Reply #2 on: November 12, 2019, 11:44:35 am »
I  made a cock up with the last post ! Heres my new code, I am trying to use a pot to control the speed but keep getting error messages when trying to compile it,can someone tell me what im doing wrong?,thanks in advance m3

code:

// this version uses millis() to manage timing rather than delay()
// and the movement is determined by a pair of momentary push switches
// press one and it turns CW, press the other and it turns CCW
#include <OneWire.h>
#include <Wire.h>
byte directionPin = 9;
byte stepPin = 8;

byte buttonCWpin = 10;
byte buttonCCWpin = 11;

boolean buttonCWpressed = false;
boolean buttonCCWpressed = false;

byte ledPin = 13;

unsigned long curMillis;
unsigned long prevStepMillis = 0;
unsigned long millisBetweenSteps = 25; // milliseconds
int i, j;

void setup() {

     pinMode(directionPin, OUTPUT);
     pinMode(stepPin, OUTPUT);
     pinMode(ledPin, OUTPUT);
     
     pinMode(buttonCWpin, INPUT_PULLUP);
     pinMode(buttonCCWpin, INPUT_PULLUP);
     
}

void loop() {
   
    curMillis = millis();
    readButtons();
    readButtons();
    delay(analogRead(0) / 4 + 5)
    if (i < 1) break;
}

void readButtons() {
   
    buttonCCWpressed = false;
    buttonCWpressed = false;
   
    if (digitalRead(buttonCWpin) == LOW) {
        buttonCWpressed = true;
    }
    if (digitalRead(buttonCCWpin) == LOW) {
        buttonCCWpressed = true;
    }
}

void actOnButtons()
   
    if (buttonCWpressed == true) {
        digitalWrite(directionPin, LOW);
        singleStep();
    }
    if (buttonCCWpressed == true) {
        digitalWrite(directionPin, HIGH);
        singleStep();
    }{ delay(analogRead(0) / 4 + 5);
    i--;
    if (i < 1) break;
}

void singleStep() {
    if (curMillis - prevStepMillis >= millisBetweenSteps) {
         += millisBetweenSteps;
        prevStepMillis = curMillis;
        digitalWrite(stepPin, HIGH);
        digitalWrite(stepPin, LOW);
    }
}
 

Offline newbrain

  • Super Contributor
  • ***
  • Posts: 1919
  • Country: se
Re: stepper sketch
« Reply #3 on: November 12, 2019, 01:25:41 pm »
I will not offer a code review or a solution, but just some hints for getting effective help:
  • Use the code tag to paste code into a post. It will make it more readable.
  • If your code does not compile, I'm sure the compiler is complaining about something. Please share it with us, if you want help.
  • The code looks quite sloppily written (even for Arduino  standards), with repeated stuff and possible leftover statements from a different structure.
    Make sure you have decent code before compiling, and even more so before asking for help.
  • There are also sylistic issues, but those might partly be a matter of personal taste.
Nandemo wa shiranai wa yo, shitteru koto dake.
 

Offline Buriedcode

  • Super Contributor
  • ***
  • Posts: 1954
  • Country: gb
Re: stepper sketch
« Reply #4 on: November 12, 2019, 04:19:04 pm »
It also might be helpful to make a crude flow diagram to show exactly what behavior you want.  Maybe even pseudocode.  Sometimes writing it down in plain English can help you structure code better.  Eg:

Code: [Select]
Check button 1
   If pressed -> call this function.
   else do nothing.
Check button 2
   If pressed -> call this function.
   else -> call this function.

I think its fairly clear what you want to do just from your code but there are some errors.  I will try to point a few out:


..
void loop() {
   
    curMillis = millis();
    readButtons();
    readButtons();
    delay(analogRead(0) / 4 + 5)
    if (i < 1) break;
}

This requires a semi-colon after "delay(analogRead(0) / 4 + 5)".  Also, why are you trying to break out of the main loop?  Surely you want this to run as a loop, with everything you need happening inside it.
A break in main will reset the micro back to the setup - this can work in some instances but isn't best practice.


void readButtons() {
   
    buttonCCWpressed = false;
    buttonCWpressed = false;
   
    if (digitalRead(buttonCWpin) == LOW) {
        buttonCWpressed = true;
    }
    if (digitalRead(buttonCCWpin) == LOW) {
        buttonCCWpressed = true;
    }
}

You set the boolean vars buttonXXXpressed when the buttons are pressed (active low) but you don't check these vars anywhere else in the code.  All this does is set bits if a button is pressed - it doesn't act on it with anything else.

void actOnButtons()
   
    if (buttonCWpressed == true) {
        digitalWrite(directionPin, LOW);
        singleStep();
    }
    if (buttonCCWpressed == true) {
        digitalWrite(directionPin, HIGH);
        singleStep();
    }{ delay(analogRead(0) / 4 + 5);
    i--;
    if (i < 1) break;
}

This function is never called, so it will never change the pins or call "singlestep".  Also you have an extra opening curly brace before "delay". 


void singleStep() {
    if (curMillis - prevStepMillis >= millisBetweenSteps) {
         += millisBetweenSteps;
        prevStepMillis = curMillis;
        digitalWrite(stepPin, HIGH);
        digitalWrite(stepPin, LOW);
    }
}

You're missing a var before " += millisBetweenSteps;" I can only assume you're adding this to something.   Also, what happens if this function is called in less than 25ms between curMillis and prevStepMillis?  It will check that >25ms has passed, but it hasn't so it won't change the step pin.  That's fine, as it will just skip until more than 25ms has passed, but for now just use a simple delay of say 100ms.  This means it will always cause a motor step, and will block, but for simplicity, its just to get it working.


All the above means it won't compile - the Arduino IDE will provide errors, and those errors will tell you which lines the problem(s) lie.  A missed semicolon will flag up the line after the missing semicolon, so you should look at the line before.  Unfortunately it can also hide warnings such as "function never used" which can be handy to point out you're never actually calling a function you need.
 

Offline newbrain

  • Super Contributor
  • ***
  • Posts: 1919
  • Country: se
Re: stepper sketch
« Reply #5 on: November 13, 2019, 09:56:54 am »
A break in main will reset the micro back to the setup - this can work in some instances but isn't best practice.
Nope, this cannot work, and a conforming compiler must emit a diagnostic:
a break in that position is not allowed by the language and is a constraint violation:
Quote
6.8.6.3 The break statement
Constraints
A break statement shall appear only in or as a switch body or loop body.

Note: this is from the C99 draft standard, I know Arduino uses C++ but I'm quite sure this is still valid. Checked: 6.6.1 of C++ 11.

EtA: the original code shows very little effort in getting it right. I'm of the opinion that our efforts should be proportionate.
« Last Edit: November 13, 2019, 12:24:39 pm by newbrain »
Nandemo wa shiranai wa yo, shitteru koto dake.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->