Notifications
Clear all

Newbie needs help with arduino code

108 Posts
5 Users
1 Likes
31.5 K Views
Chip
 Chip
(@chip)
Member
Joined: 5 years ago
Posts: 79
Topic starter  

   
Quote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 

but all I am asking for is some help correcting my mistakes.

Now that is a very tall order! ? ? ? 

The first obvious mistakes are the following lines.

long previousMillis = 0;
long currentMillis = 0;

The variables should be declared "unsigned long"! Not critical, but this doubles the amount of time until the next reset to zero milliseconds.


   
ReplyQuote
Chip
 Chip
(@chip)
Member
Joined: 5 years ago
Posts: 79
Topic starter  
Posted by: @pugwash

The variables should be declared "unsigned long"! Not critical, but this doubles the amount of time until the next reset to zero milliseconds.

Thank you , i have corrected them in my code.

 

 

int setspeed = 0;
int RPMinterval = 500;
unsigned long previousMillis = 0;
unsigned long currentMillis = 0;
float rpmR = 0;
float rpmL = 0;


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 

@chip

Perhaps it might be a good idea to attach the .ino file. Being able to collapse the functions until needed, makes easier reviewing than constantly scrolling up and down through the whole sketch!


   
ReplyQuote
Chip
 Chip
(@chip)
Member
Joined: 5 years ago
Posts: 79
Topic starter  
Posted by: @pugwash

Perhaps it might be a good idea to attach the .ino file. Being able to collapse the functions until needed, makes easier reviewing than constantly scrolling up and down through the whole sketch!

Okay let me try to do that.

 

 


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 

@chip

I forgot to mention that doing math with different data types can lead to unexpected results, therefore you need to change, just for safety's sake.

int RPMinterval = 500; to 
unsigned long RPMinterval = 500;

 


   
ReplyQuote
Chip
 Chip
(@chip)
Member
Joined: 5 years ago
Posts: 79
Topic starter  
Posted by: @pugwash

I forgot to mention that doing math with different data types can lead to unexpected results, therefore you need to change, just for safety's sake.

Thank you I have changed the code.

 

int setspeed = 0;
unsigned long RPMinterval = 500;
unsigned long previousMillis = 0;
unsigned long currentMillis = 0;
float rpmR = 0;


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 

@chip

You have a lot of repetition between lines 430 and 509.

Perhaps you should consider one function and a passed variable like this

ckpt(103); //function call, replaces ckpt2

void ckpt(int hdg){
  if (heading==0){
    analogWrite(RpwmPin, STOPM);
        analogWrite(LpwmPin, STOPM);
       delay(15000);
  }
  Dheading = hdg; // at coffee table
}

Now you can get rid of ckpt2 to ckpt11 altogether. Leave ckpt1 and ckpt12 as they are!

 

Now we come to lines 347 to 384 (multiple if statements). This is a classic situation for "switch case" statements!

 

  switch(cmmoved){
    case (>= 2512 + rwaDiv):
      ckpt12(); //start line
    break;
    case (>= 2408 + rwaDiv):
      ckpt(110); //start line
    break;
    case (>= 2142 + rwaDiv):
      ckpt(205); //start line
    break;
... //you fill in the rest
}

NOTE that I have inversed the conditional, starting by checking for the highest value first and have included the function call shown above. This should make your code a little more compact.

Try it out and check that it works properly, before adopting it permanently!


   
ReplyQuote
Chip
 Chip
(@chip)
Member
Joined: 5 years ago
Posts: 79
Topic starter  
Posted by: @pugwash

   ckpt(110); //start line    break;    case (>= 2142 + rwaDiv):      ckpt(205); //start line    break;

I thank you again for taking your time to help me.

i am a little confused about " ckpt(110) and ckpt(205) as they are not in my code.

please let me know where I made my mistake(s)

also i am getting a compile error

chip2_10-27-2019.ino: In function 'void chipmove()':

chip2_10-27-2019:350:11: error: expected primary-expression before '>=' token

case (>= 2512 + rwaDiv):

^

chip2_10-27-2019:353:11: error: expected primary-expression before '>=' token

case (>= 2408 + rwaDiv):

^

chip2_10-27-2019:354:15: error: 'ckpt' was not declared in this scope

ckpt(110); //start line

^

chip2_10-27-2019:356:11: error: expected primary-expression before '>=' token

case (>= 2142 + rwaDiv):

^

exit status 1
expected primary-expression before '>=' token

 

IMG 1074

 

 

This post was modified 5 years ago by Chip

   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 

@chip

i am a little confused about " ckpt(110) and ckpt(205) as they are not in my code.

These values are taken from the following original code "Dheading" variable:

void ckpt10(){
   if (heading==0){
    analogWrite(RpwmPin, STOPM);
        analogWrite(LpwmPin, STOPM);
       delay(15000);
  }
  Dheading = 205;
}
void ckpt11(){
   if (heading==0){
    analogWrite(RpwmPin, STOPM);
        analogWrite(LpwmPin, STOPM);
       delay(15000);
  }
  Dheading = 110;
}

chip2_10-27-2019:354:15: error: 'ckpt' was not declared in this scope

ckpt(110); //start line

Declare "ckpt" at the top of the sketch!

The other errors I will have a look at!


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 

@chip

Don't bother declaring "ckpt"!


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 

@chip

Try compiling this, the errors should be gone, hopefully!! ? ?

 


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 

@chip

Forget my last post, the "switch case" is slightly more complex than I thought!


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 

@chip

I have reactivated all the "if" statements, "switch case" was not doing what I wanted it to do, but I have left the single function in this version.


   
ReplyQuote
Chip
 Chip
(@chip)
Member
Joined: 5 years ago
Posts: 79
Topic starter  
Posted by: @pugwash

Try compiling this, the errors should be gone, hopefully!! ?

now im geting this error

chip2_10-27-2019:350:35: error: the value of 'rwaDiv' is not usable in a constant expression

case (cmmoved >= 2512 + rwaDiv):

                                                  ^


   
ReplyQuote
Page 1 / 8