Notifications
Clear all

Function arguments

79 Posts
8 Users
12 Likes
2,688 Views
Inq
 Inq
(@inq)
Member
Joined: 2 years ago
Posts: 1900
 

@barrie - I'm sure @will has dug into your code more than my quick glance... also, this might not be your current version...  but I would like to suggest you put some of your own diagnostics in your code.  For instance... note the Serial.println() statements I added.  This way you can get a visual output in your Serial monitor... so you see the what code was being activated in relation to when you note the strange behavior.   

 

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);
    Serial.println("Case 1");
    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; 
    Serial.println("Case 6");
    //nSteps = 0;
    break;
 }  
x = revCount;
 while (x == revCount)
 {
//Change direction at the limits
  if (stepper1.distanceToGo() == 0){
    stepper1.moveTo(-stepper1.currentPosition());
    Serial.println("Step1-Reverse");
    revCount++;
  } 
  if (stepper2.distanceToGo() == 0){
      stepper2.moveTo(-stepper2.currentPosition());
      Serial.println("Step2-Reverse");
  } 
  
  stepper1.run();
  stepper2.run();
 }

}

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
 
Posted by: @will

  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.

I think @barrie's statement...

int(x) = 0;

is being interpreted by the compiler as...

int x = 0;

Otherwise the compiler would puke because x is not declared anywhere else.  I've never seen it written that way and I'm surprised as you that it doesn't puke, but if it compiles... that is the only assumption that can be made.

Also, he is setting the 2 steppers to the same acceleration and speed and moving them the same absolute distance, so although you are right about the while loop exiting when stepper1 bounces, stepper 2 should also bounce at the same time.  And even if it doesn't, it will as soon as the while loop is re-entered after it falls through case 2.  I would expect them to be synchronized.

So... @barrie, you're going to have to put into English what you want your project to do and what it is really doing before anyone can help.

 

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
(@yurkshirelad)
Member
Joined: 3 years ago
Posts: 493
 
Posted by: @will

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

Technically those are integers not floats, so 250.0 or even 250.0f, if my memory serves me correctly. Minor point but worth noting, especially if you ever decide to program in python, for example.


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

@yurkshirelad 

They are converted to floats at run time. That's why the advanced declaration of functions is important, so the compiler can prepare the interface with the proper types. Values sent to the interface will be changed as required.

Note that this is just C so no overloading of functions is possible.

 

Edit: I think it's done by the compiler (upon further reflection). The values sent to the function will always be of the type specified in the interface declaration (definition).

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  

Thank you @will. Very good point and may be one of the movement problems. I moved it to stepper 2 but still getting peculiar movement so I need to take a closer look at the accelstepper library.


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

@barrie 

What do you call "peculiar movement" ?

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


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

@inq It depends on board. I tried UNO and RP2040, all compiled at ALL diag level, but ESP32 puked as follows

sketch_sep28a:2:4: error: unnecessary parentheses in declaration of 'x' [-Werror=parentheses]
int(x) = 0;
^
cc1plus: some warnings being treated as errors
exit status 1
unnecessary parentheses in declaration of 'x' [-Werror=parentheses]

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.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
(@yurkshirelad)
Member
Joined: 3 years ago
Posts: 493
 
Posted by: @will

@yurkshirelad 

They are converted to floats at run time. That's why the advanced declaration of functions is important, so the compiler can prepare the interface with the proper types. Values sent to the interface will be changed as required.

Note that this is just C so no overloading of functions is possible.

 

Edit: I think it's done by the compiler (upon further reflection). The values sent to the function will always be of the type specified in the interface declaration (definition).

Good point. I think it's a good habit to be explicit, which helps if you move to a dynamically typed language, like python. Where the difference between 250 and 250.0 may result in unexpected functionality.


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

@will Thank you. I will explain the peculiar movement and try to explain clearly what I am trying to do tomorrow. I have to go now. Also thank you @inq and  @yurkshirelad.  I will prep a clear description with a little movie of my project tomorrow.


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

@barrie 

OK, see you tomorrow.

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


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 
Posted by: @will

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

Actually, it is well defined under the C++ standard:

void loop() {
  // Value initialisation...
  char(C)  = 'C';
  float(F) = 101.101;

  // Type conversion...
  Serial.println(C);
  Serial.println(int(C));
  Serial.println(F, 3);
  Serial.println(int(F));
 }

-- OUTPUT --
C, 67
101.101, 101

If you check out many of my previous posts, I have on many occasions used such syntax in for loop index declarations.


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@will

Posted by: @will

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.

The correct way to handle switch statements is to provide a catch all default label, and then handle the unexpected condition there - No ZERO conditions required:

For example:

  switch (revCount) {
    case 1:
      Serial.println("Value was: 1");
      break;
    case 2:
      Serial.println("Value was: 2");
      break;
    case 3:
      Serial.println("Value was: 3");
      break;
    default:
      Serial.println("Unexpected value encountered!");
      break;
  }

 


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@barrie

A quick look through your code, and I just offer some basic improvements, if you wish to accept them 🙂

1) const int Rled = 14; could be const byte Rled = 14; to reduce memory space, but not a big deal at this point of your learning, just an FYI if you run out of space.

2) Since you have defined constants, you could actually loop over them as follows, with a much smaller defined data type such as byte, without any global constants:

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

...otherwise, just hard code gpio = 14 and < 17...

Also note:- The 'void' keyword is not necessary in C++ function parameter lists, an empty parenthesis is sufficient.


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

I have ironed out most of the bugs and have a video showing exactly what the problem is. Unfortunately it is too big to post here. (40Meg) Is there any way I can show it? In the meantime I will try to condense the video down to 10Meg.


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

@barrie 

You have several choices. You can degrade the quality of your video enough to get under the 10 meg limit on the forum or you can upload the video online somewhere else and post an URL to it.

You could also split it up into smaller pieces and post those.

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


   
ReplyQuote
Page 2 / 6