Notifications
Clear all

Function arguments

79 Posts
8 Users
12 Reactions
2,876 Views
barrie
(@barrie)
Member
Joined: 2 years 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)
Member
Joined: 2 years ago
Posts: 1900
 

Looks great to me! 👍 

3 lines of code = InqPortal = Complete IoT, App, Web Server w/ GUI Admin Client, WiFi 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)
Member
Joined: 2 years ago
Posts: 1900
 

... 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, WiFi 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)
Member
Joined: 2 years ago
Posts: 385
 
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)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 7115
 

@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.

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
barrie
(@barrie)
Member
Joined: 2 years ago
Posts: 86
Topic starter  

   
ron bentley reacted
ReplyQuote
barrie
(@barrie)
Member
Joined: 2 years ago
Posts: 86
Topic starter  

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


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

@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*

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


   
frogandtoad reacted
ReplyQuote
barrie
(@barrie)
Member
Joined: 2 years ago
Posts: 86
Topic starter  

@will You are right again. Thank you.


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

@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.

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


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

@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.

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


   
ReplyQuote
barrie
(@barrie)
Member
Joined: 2 years 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)
Member
Joined: 3 years ago
Posts: 2542
 
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 🙂

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


   
ReplyQuote
Inq
 Inq
(@inq)
Member
Joined: 2 years ago
Posts: 1900
 
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, WiFi 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)
Member
Joined: 3 years ago
Posts: 2542
 
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.

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


   
ReplyQuote
Page 1 / 6