Function arguments

Clear all

# Function arguments

79 Posts
8 Users
12 Likes
1,078 Views
(@will)
Famed Member
Joined: 2 years ago
Posts: 2289

@barrie

``` for (byte gpio = Rled; gpio == Wled; gpio++) {
pinMode(gpio, OUTPUT);
}
```

Did you mean gpio<=Wled ?

Experience is what you get when you don't get what you want.

(@barrie)
Estimable Member
Joined: 7 months ago
Posts: 86
Topic starter

@will

I am currently looking at drop box. Never used it but I believe I will be able to get a URL.

This post was modified 6 months ago by barrie

(@will)
Famed Member
Joined: 2 years ago
Posts: 2289

Sounds good

Experience is what you get when you don't get what you want.

(@barrie)
Estimable Member
Joined: 7 months ago
Posts: 86
Topic starter

The problem is in Case: 3. the yoyo function. The video shows the problem when the yellow LED lights up.

The two stepper statements are are working in series. They need to run in parallel. Or I need to do it another way.

I have to rush off for an hour or so.

```/*
//Two 28BYJ48 stepper
*/

#include <AccelStepper.h>

// ************** Constants *************
const int Rled = 14;
const int Yled = 15;
const int Gled = 16;
const int Bled = 17;
const int Wled = 18;

// Define step constants
#define FULLSTEP 4
#define HALFSTEP 8

// Define Motor Pins (2 Motors used)

#define motorPin1  8     // Blue   - 28BYJ48 pin 1
#define motorPin2  9     // Pink   - 28BYJ48 pin 2
#define motorPin3  10    // Yellow - 28BYJ48 pin 3
#define motorPin4  11    // Orange - 28BYJ48 pin 4

#define motorPin5  4     // Blue   - 28BYJ48 pin 1
#define motorPin6  5     // Pink   - 28BYJ48 pin 2
#define motorPin7  6     // Yellow - 28BYJ48 pin 3
#define motorPin8  7     // Orange - 28BYJ48 pin 4

// Define two motor objects
// The sequence 1-3-2-4 is required for proper sequencing of 28BYJ48
AccelStepper stepper1(FULLSTEP, motorPin1, motorPin3, motorPin2, motorPin4);
AccelStepper stepper2(FULLSTEP, motorPin5, motorPin7, motorPin6, motorPin8);

const long FullStepsPerRev = 2048*FULLSTEP;

// ********** Variables **********
long stepsPerRev = 2048;
int revCount = 0; // rotor revolutions
unsigned long startTime = 0;

//********* Setup ***************
void setup()
{
// set pins 14, 15, 16, 17, 18 to output pins
for (int i = 14; i < 19; i++) {
pinMode (i,OUTPUT);
}
Serial.begin (9600);

// 1 revolution Motor 1
stepper1.setMaxSpeed(1000.0);
stepper1.setAcceleration(500.0);
stepper1.setSpeed(500);
stepper1.moveTo(2048);
// 1 revolution Motor 2
stepper2.setMaxSpeed(1000.0);
stepper2.setAcceleration(500.0);
stepper2.setSpeed(200);
stepper2.moveTo(-2048);
}
//****** functions ****
void defaultSettings(){
// 1 revolution Motor 1 CW
stepper1.setMaxSpeed(1000.0);
stepper1.setAcceleration(500.0);
stepper1.setSpeed(500);
stepper1.moveTo(2048);
// 1 revolution Motor 2 CCW
stepper2.setMaxSpeed(1000.0);
stepper2.setAcceleration(500.0);
stepper2.setSpeed(500);
stepper2.moveTo(-2048);
}
bool elapsedTime (unsigned long interval)
{
unsigned long currentTime = millis();
if (startTime = 0)  {startTime = millis();}
if (currentTime - startTime >= interval) { startTime = 0; return true;}
else { startTime = currentTime; return false; }
}
//turn LED's off
void alloff() {
for (int i = 14; i < 19; i++){
digitalWrite (i,LOW);
}
}
//turn LED's on
void allon() {
for (int i = 14; i < 19; i++){
digitalWrite (i,HIGH);
}
}
void setStepper (float mxs,float acc,float spd,long mvt1,long mvt2, int seg)
{
// 1 revolution Motor 1
stepper1.setMaxSpeed(mxs);
stepper1.setAcceleration(acc);
stepper1.setSpeed(spd);
stepper1.moveTo(mvt1/seg);
// 1 revolution Motor 2
stepper2.setMaxSpeed(mxs);
stepper2.setAcceleration(acc);
stepper2.setSpeed(spd);
stepper2.moveTo(mvt2/seg);
}
void newSpeed(int spd){
stepper1.setSpeed(spd);
stepper2.setSpeed(spd);
}
void yoyo(int segment){
long target = FullStepsPerRev/segment;
stepper1.setCurrentPosition(0);
stepper2.setCurrentPosition(0);
stepper1.moveTo(target);
stepper2.moveTo(-target);
stepper1.runToPosition();
stepper2.runToPosition();
}
stepper1.moveTo(2048);
stepper2.moveTo(-2048);
stepper1.setSpeed(spd);
stepper2.setSpeed(-spd);
}
// ************************* Main loop ********************
void loop()
{
int x = 0;
int runCount = 0;
switch (revCount)
{
case 2: // slow down
alloff();
// mxs,acc,spd,mvt1,mvt2,seg
setStepper (200.0, 500.0, 200.0, 2048, -2048, 1);
digitalWrite (Rled,HIGH);
break;

case 3: // yoyo +1//8 to -1/8 from current position
alloff();
digitalWrite (Yled,HIGH);
yoyo(8);
break;

case 6: // same direction
alloff();
digitalWrite (Gled,HIGH);
break;

case 7: // default
digitalWrite (Bled,HIGH);
defaultSettings();
break;

case 8:
digitalWrite (Wled,HIGH);
break;

case 9: //re start cycle
alloff();
revCount = 0;
break;
}
x = revCount;
while (x == revCount){
//Change direction at the limits
if (stepper1.distanceToGo() == 0){
revCount++;
stepper1.setCurrentPosition(0);
stepper2.setCurrentPosition(0);
stepper1.moveTo(-stepper1.currentPosition());
stepper2.moveTo(-stepper2.currentPosition());
stepper1.setCurrentPosition(0);
stepper2.setCurrentPosition(0);
}
runCount++;
stepper1.run();
stepper2.run();
}
}```
This post was modified 6 months ago by barrie

(@will)
Famed Member
Joined: 2 years ago
Posts: 2289

Your problem is that the runToPosition causes each stepper to complete that operation before moving on to do anything else. Change it to ...

```void yoyo(int segment) {
long target = FullStepsPerRev / segment;
stepper1.setCurrentPosition(0);
stepper2.setCurrentPosition(0);
stepper1.moveTo(target);
stepper2.moveTo(-target);
while (stepper1.distanceToGo()>0) {
stepper1.run();
stepper2.run();
}
}

```

This will allow each stepper to step once inside the while loop and will appear to "share" the motion.

Experience is what you get when you don't get what you want.

Ron and ron bentley reacted
(@barrie)
Estimable Member
Joined: 7 months ago
Posts: 86
Topic starter

@will Thank you, that looked promising but there was no reaction. The steppers didn't blink and kept running.

By the way, did the video link work OK? Its my first use of the dropBox.

(@will)
Famed Member
Joined: 2 years ago
Posts: 2289

Maybe change >0 to !=0 ? Very curious.

Dropbox says that it can't preview items with no datatype, so you have to download the object and then guess at it's type, rename it and then play it.

Experience is what you get when you don't get what you want.

(@barrie)
Estimable Member
Joined: 7 months ago
Posts: 86
Topic starter

@inq Good advice, thanks. In your absence I posted a cleaned up version of my sketch along with a link to a video showing the latest problem.

(@will)
Famed Member
Joined: 2 years ago
Posts: 2289

Posted by: @barrie

The steppers didn't blink and kept running.

What do you mean they "kept running", I thought you said the problem was that one or the other would stop ?

Experience is what you get when you don't get what you want.

(@barrie)
Estimable Member
Joined: 7 months ago
Posts: 86
Topic starter

@will Sorry. I assumed you had seen the video. I am fixing it. I will send another link so you will have a better perspective.

(@zander)
Illustrious Member
Joined: 2 years ago
Posts: 4465

@barrie Try shooting the video in 640x480, NOT HighDef

Arduino says and I agree, in general, the const keyword is preferred for defining constants and should be used instead of #define
"Never wrestle with a pig....the pig loves it and you end up covered in mud..." anon
My experience hours are >75,000 and I stopped counting in 2004.
Major Languages - 360 Macro Assembler, Intel Assembler, PLI/1, Pascal, C plus numerous job control and scripting

(@will)
Famed Member
Joined: 2 years ago
Posts: 2289

So you're saying that replacing your entire module you with my entire module made no difference at all in execution ?

Did it not compile or reload properly ?

Experience is what you get when you don't get what you want.

(@barrie)
Estimable Member
Joined: 7 months ago
Posts: 86
Topic starter

@zander  First time video on iPhone. Options are 4K or HD each each with 24 or 30 or 60 options. What is 4K 24?

(@robotbuilder)
Noble Member
Joined: 4 years ago
Posts: 1739

Your problem is that the runToPosition causes each stepper to complete that operation before moving on to do anything else. Change it to ...

This example seems to be controlling two steppers simultaneously?

```// Copyright (C) 2014 Mike McCauley

#include <AccelStepper.h>

#define STEPPER1_DIR_PIN 3
#define STEPPER1_STEP_PIN 2
#define STEPPER2_DIR_PIN 7
#define STEPPER2_STEP_PIN 6

AccelStepper stepper1(AccelStepper::DRIVER, STEPPER1_STEP_PIN, STEPPER1_DIR_PIN);
AccelStepper stepper2(AccelStepper::DRIVER, STEPPER2_STEP_PIN, STEPPER2_DIR_PIN);

void setup()
{
stepper1.setMaxSpeed(2000.0);
stepper2.setAcceleration(200.0);
stepper1.moveTo(100);

stepper2.setMaxSpeed(1000.0);
stepper2.setAcceleration(100.0);
stepper2.moveTo(100);
}

void loop()
{
// change direction at limits
if (stepper1.distanceToGo() == 0)
stepper1.moveTo(-stepper1.currentPosition() );
if (stepper2.distanceToGo() == 0)
stepper2.moveTo(-stepper2.currentPosition() );
stepper1.run();
stepper2.run();
}

```

(@zander)
Illustrious Member
Joined: 2 years ago
Posts: 4465

@barrie 4K 24 is 4K resolution at 24frames per second (movie standard) You need to drop the 4K down to something a lot smaller, 1028 might fit in 10Meg but there is no need for a hollywood blockbuster, just a simple VGA 640x480 will suffice

You will probably have to run the raw video thru a downsizer.

Arduino says and I agree, in general, the const keyword is preferred for defining constants and should be used instead of #define
"Never wrestle with a pig....the pig loves it and you end up covered in mud..." anon
My experience hours are >75,000 and I stopped counting in 2004.
Major Languages - 360 Macro Assembler, Intel Assembler, PLI/1, Pascal, C plus numerous job control and scripting

Page 3 / 6