Notifications
Clear all

Function arguments

79 Posts
8 Users
12 Likes
1,373 Views
barrie
(@barrie)
Estimable Member
Joined: 9 months ago
Posts: 86
Topic starter  

I am writing a function with several arguments that have different data types and no return. eg:-

void setModule (float afloat ,float clong dlong e, int f)
 
Is this the correct format?

   
Quote
Inq
 Inq
(@inq)
Noble Member
Joined: 1 year ago
Posts: 1072
 

Looks great to me! 👍 

3 lines of code = InqPortal = Complete IoT, App, Web Server w/ GUI Admin Client, Access Point Manager, Drag & Drop File Manager, OTA, Performance Metrics, Web Socket Comms, Easy App API, All running on ESP8266...
Even usable on ESP-01S - Quickest Start Guide


   
ReplyQuote
Inq
 Inq
(@inq)
Noble Member
Joined: 1 year ago
Posts: 1072
 

... I'm assuming that you plan to use more human friendly names though.  You'll thank or curse yourself down the road when you say... now what does "a" mean?  Remember - there is no increase in binary code size just because you used a long name... float voltage, float current, ...

 

3 lines of code = InqPortal = Complete IoT, App, Web Server w/ GUI Admin Client, Access Point Manager, Drag & Drop File Manager, OTA, Performance Metrics, Web Socket Comms, Easy App API, All running on ESP8266...
Even usable on ESP-01S - Quickest Start Guide


   
frogandtoad and Ron reacted
ReplyQuote
ron bentley
(@ronbentley1)
Honorable Member
Joined: 1 year ago
Posts: 391
 
Posted by: @barrie

I am writing a function with several arguments that have different data types and no return. eg:-

void setModule (float afloat ,float clong dlong e, int f)
 
Is this the correct format?

Hi, yes, this example is okay and will compile.

I echo the previous post that suggests the parameters are crafted as meaningful variables.

Cheers

Ron B

Ron Bentley
Creativity is an input to innovation and change is the output from innovation. Braden Kelley
A computer is a machine for constructing mappings from input to output. Michael Kirby
Through great input you get great output. RZA
Gauss is great but Euler rocks!!


   
frogandtoad reacted
ReplyQuote
Ron
 Ron
(@zander)
Illustrious Member
Joined: 3 years ago
Posts: 4801
 

@barrie AS well as proper var names, you might want to consider more specific data types. On this forum, some integer types are 16 bit, some are 32 bit. Don't know if it will make a difference, but what a b*tch to find.

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


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

Thanks for your comments. I am stuck with an error I don't understand and I suspected my argument format was the issue. I'll post the code and mark the suspect. The error disappeared when I blanked out the line marked. ****ERROR***** Here is the error message:

" expected primary-expression before 'float' "

 

/*
Two 28BYJ48 stepper YOYO 
*/
 
#include <AccelStepper.h>
 
// ******************* Define 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);
 
 // ********************* Variables *****************************
int segments = 1;
float acceleration = 500;
float stepSpeed = 500;
long reverseSteps = -2048;
long stepsPerRev = 2048;
long FullStepsPerRev = 2048*FULLSTEP;
bool startUp = false;
bool goyo = false;
bool goAround = false;
bool changeSpeed = false;
int revCount = 0; // rotor revolutions 
int nSteps = 0;
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 CW
  stepper1.setMaxSpeed(250.0);
  stepper1.setAcceleration(250.0);
  stepper1.setSpeed(250);
  stepper1.moveTo(2048);  
  
  // 1 revolution Motor 2 CCW
  stepper2.setMaxSpeed(250.0);
  stepper2.setAcceleration(250.0);
  stepper2.setSpeed(250);
  stepper2.moveTo(-2048); 
 
}
 //******************** functions ************************
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; }
}
void (resetStepper(void)){
  revCount = 0;
}

//turn LED's off
void alloff(void) {
for (int i = 14; i < 19; i++){
digitalWrite (i,LOW);
}
}
//turn LED's on
void allon(void) {
for (int i = 14; i < 19; i++){
digitalWrite (i,HIGH);
}
}
 
 void setStepper (float mxs,float acc,float spd,long mvt1,long mvt2, int seg) 
 {
  stepper1.setMaxSpeed(mxs);
  stepper1.setAcceleration(acc);
  stepper1.setSpeed(spd);
  stepper1.moveTo(mvt1/seg);  
  
  // 1 revolution Motor 2 CCW
  stepper2.setMaxSpeed(mxs);
  stepper2.setAcceleration(acc);
  stepper2.setSpeed(spd);
  stepper2.moveTo(-mvt2/seg); 
  }
  void roundAbout(void){
    float spd = stepper1.speed();
    stepper2.setSpeed(spd);
}
  void yoyo (int seg){
      long stepTarget = (stepsPerRev)/seg;
    stepper1.moveTo(stepTarget);  
    stepper2.moveTo(stepTarget); 
    }

 // ************************* Main loop *****************
void loop()
{ 
int(x) = 0;
 switch (revCount)
 {
    case 1: 
     alloff();
    // set stepper to yoyo 1/8 of revolution
    setStepper (float 250, float 250, float 250, long 2048, long -2048, int 8);// *ERROR*****
    digitalWrite (Rled,HIGH);
    break; 
    case 2:
    // set stepper to default
    //setStepper (250, 250, 250, long 2048, long -2048, int 1);
    digitalWrite (Yled,HIGH);
     break;
   case 3: 
     digitalWrite (Gled,HIGH);
    // resetStepper();
    break;
    case 4: 
     digitalWrite (Bled,HIGH);
    
    break;
    case 5: 
     digitalWrite (Wled,HIGH);
    
    break; 
    case 6:
    alloff(); 
    // set stepper to default
    //setStepper(float 250, float 250, float 250, long 2048, long -2048, int 1);
    setStepper(250, 250, 250, 2048, -2048, 1);
    revCount = 0; 
    //nSteps = 0;
    break;
 }  
x = revCount;
 while (x == revCount){
//Change direction at the limits
  if (stepper1.distanceToGo() == 0){
    stepper1.moveTo(-stepper1.currentPosition());
    revCount++;
  } 
  if (stepper2.distanceToGo() == 0){
      stepper2.moveTo(-stepper2.currentPosition());
  } 
  
  stepper1.run();
  stepper2.run();
 }

}

 

 


   
ron bentley reacted
ReplyQuote
barrie
(@barrie)
Estimable Member
Joined: 9 months ago
Posts: 86
Topic starter  

*Error** didn't show. The line is under switch..... Case 1 starts setStepper.


   
ReplyQuote
Will
 Will
(@will)
Famed Member
Joined: 2 years ago
Posts: 2326
 

@barrie 

You need to specify data type when you define the function, not when you call it.

 

Change

setStepper (float 250, float 250, float 250, long 2048, long - 2048, int 8); // *ERROR*

 

to

 

setStepper (250, 250, 250, 2048, - 2048, 8); // *ERROR*

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


   
frogandtoad reacted
ReplyQuote
barrie
(@barrie)
Estimable Member
Joined: 9 months ago
Posts: 86
Topic starter  

@will You are right again. Thank you.


   
ReplyQuote
Will
 Will
(@will)
Famed Member
Joined: 2 years ago
Posts: 2326
 

@barrie 

I've been looking at your sketch and there are some very shady parts.

for instance, just inside the loop you have the statement

  int(x) = 0;

 I don't know what you think that's doing, but I can be pretty sure it's not doing it. In fact, it's not doing anything. The function int(something) returns the integer part of the something that it's given. So, in your case, int(x) returns the integer (whole number) part of x.

So, if x were 3.14159265 (almost pi) then int(x) returns 3. Note that this value CANNOT IN ANY WAY be changed to zero. You're trying to tell the uController "hey, make this 3 into a zero" - it just ai't gonna work.

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


   
ReplyQuote
Will
 Will
(@will)
Famed Member
Joined: 2 years ago
Posts: 2326
 

@barrie 

Also, in loop( you do a switch on revCount and handle the case where it is 1, 2, 3, 4, 5 or 6 but you do't handle the case where it is zero. The, despite the fact that the first time through, revCount has been set to zero by its initializer.

This is not necessarily and error, but it would be be better to include a case for 0, even if it just shows explicitly that it doesn't do anything. That is

case 0:

     // Nothing to do

     break;

 

I also note that you set x = revCount. From this, I suspect that what you meant in the first line was

int   x = 0; and not int(x) = 0;

Then, after the switch you have

   x = revCount;
   while (x == revCount) {

Now, x and revCount are both declared as int, so both wind up with the same value in the same form, so x==revCount will ALWAYS be true and this section will ALWAYS be executed. Since x is never changed, this means that the while is executed until revCount changes. This only happens when stepper1 changes direction. Again, this may be what you're intending, its just a strange way to go about it.

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


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

@will

Wow that is an interesting analysis. int x = 0 is the initiation of the While loop that runs the steppers. I didn't capture x = 0 in the switch because I wanted the switch to be bypassed so the default stepper settings (in the setup loop) are immediately implemented in the while loop. When revCount hits 1 ( and x = 1) the while loop is broken and  the function at case1: resets the stepper and the While loop is closed in order to prevent multiple calls at switch case 1. The 5 Led's on cases 1,2,3,4 and 5 light up in sequence as expected case 6 turns them off, resets the counter and repeats the cycle. I am always open to suggestions if you think my code could be improved. 

Here is my biggest problem. In each switch case (1,2,3,4, and 5)  different stepper settings will change the steppers motion. If anyone here is familiar ( @inq ?) with the AccelStepper library please let me know. I am having difficulty getting the steppers to do exactly what I have in mind.


   
ReplyQuote
Will
 Will
(@will)
Famed Member
Joined: 2 years ago
Posts: 2326
 
Posted by: @barrie

Here is my biggest problem. In each switch case (1,2,3,4, and 5)  different stepper settings will change the steppers motion. If anyone here is familiar ( @inq ?) with the AccelStepper library please let me know. I am having difficulty getting the steppers to do exactly what I have in mind.

I don't understand your problem. The AccelStepper library is quite straightforward and should not impact your logic in the switch statement nor in the while statement.

Can you explain exactly what you want to do and where you want to do it ? Perhaps we can suggest methods to help you set up the steppers and control the motion as needed.

Maybe just one case: at a time, to keep the description and the complexity in reason 🙂

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


   
ReplyQuote
Inq
 Inq
(@inq)
Noble Member
Joined: 1 year ago
Posts: 1072
 
Posted by: @barrie

If anyone here is familiar ( @inq ?) with the AccelStepper library please let me know. I am having difficulty getting the steppers to do exactly what I have in mind.

I have my own stepper library that I use in my Inqling Jr robot.  But... happen stance I did play around with the AccelStepper library a couple days ago and got it to do what I wanted, but I'm no expert with it by any stretch.  Ask your question(s)... and I'll take a stab or someone else will.  But I'm leaving town tomorrow morning for the weekend.

 

3 lines of code = InqPortal = Complete IoT, App, Web Server w/ GUI Admin Client, Access Point Manager, Drag & Drop File Manager, OTA, Performance Metrics, Web Socket Comms, Easy App API, All running on ESP8266...
Even usable on ESP-01S - Quickest Start Guide


   
ReplyQuote
Will
 Will
(@will)
Famed Member
Joined: 2 years ago
Posts: 2326
 
Posted by: @barrie

Wow that is an interesting analysis. int x = 0 is the initiation of the While loop that runs the steppers. I didn't capture x = 0 in the switch because I wanted the switch to be bypassed so the default stepper settings (in the setup loop) are immediately implemented in the while loop. When revCount hits 1 ( and x = 1) the while loop is broken and  the function at case1: resets the stepper and the While loop is closed in order to prevent multiple calls at switch case 1. The 5 Led's on cases 1,2,3,4 and 5 light up in sequence as expected case 6 turns them off, resets the counter and repeats the cycle. I am always open to suggestions if you think my code could be improved. 

The only places that x is used is when you set x=revCount and then test if it's still equal to revCount inside the while statement.

Since x has just been set to revCount, the while loop will ALWAYS be executed and will ALWAYS be executed until stepper1 reaches its target (i.e. distanceToGo is zero) and then revCounter will be changed and the while statement will finish. Note that this does not guarantee that stepper2 will finish.

On the next call, the same thing will happen - x will be set to revCount, the while loop will be entered and proceed until stepper1 changes direction.

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


   
ReplyQuote
Page 1 / 6