Arduino C++ code run efficiently advise

Nov 14, 2019 at 10:21am
Hi guys i've gotten back into coding so i'm super rusty at it

i'm making a program for my manual car
using arduino - joystick(X,Y axis)

1
2
3
4
5
6
7

so far i've used 

Sketch uses 3600 bytes (11%) of program storage space. Maximum is 32256 bytes.
Global variables use 480 bytes (23%) of dynamic memory, leaving 1568 bytes for local variables. Maximum is 2048 bytes.

 left for my code 


i'm trying to find the most efficient way to do it
so far i've got variables for
the absolute X,Y for each gear
1
2
3
4
5
6
7
8
9
10
11
12
13
nutural_X = 524;
nutural_Y = 504;

int almost_1st_or_2ndX = 524;// far left of joystick about to move into first or second gear
int almost_1st_or_2ndY = 1023;

int first_X  =0;
int first_Y = 1023;


//repeating each gear and its variable 6 times





the two options i can see are as follows

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

while x and y are in range{

if x and y == "moving into 1st or 2nd gear variable "
    if x and y == "first gear variables"
       print car is in first gear

    if x and y == "second gear variables"
              print car is in second gear

if x and y == "moving into 5th or 6th gear variable "
    if x and y == "fifth gear variables"
             print car is in fifth gear
//ect...
}

so basically the method 1 will check if the joystick is on the far left middle before moving on to the next conditional statement
(how shifting in a manual gearbox works in real life, you can't jump right into first without going left then up )


method 2
1
2
3
4
5
6
7
8
9
10


while x and y are in range{

    if x and y == "first gear variables"
       print car is in first gear

    else if x and y == "second gear variables"
              print car is in second gear
//ECT 


so basically method 2 skips the middle point in between gear changes, (neutral to first)

at the moment i'm coding both options to be flashed into the Arduino memory, and i'll have the option to change methods by a push of a button.
Last edited on Nov 14, 2019 at 10:26am
Nov 14, 2019 at 2:30pm
does it have branch prediction? Does it have an instruction pipeline? These hardware features are a big deal with trying to tune around them.

efficiency, are you looking for speed, memory used, or other?

text compares are slow. if you can have y be a state, instead of text, you remove 20 something comparisons under the hood of a string compare down to a single compare of an integer.
say y were the index into an array of text strings, then you just compare if y==value, and when you need the text, its array[y]. Using named enums for the values makes it pretty.
Last edited on Nov 14, 2019 at 2:32pm
Nov 14, 2019 at 5:31pm
Use method 3. Create a table of data that encodes when the car is in which gear. Write a small loop that reads through the table, comparing x & y values until you find a match.

Data tends to be much smaller than code.
Nov 14, 2019 at 6:22pm
So you have gears looking something like this?

1  3  5
|  |  |
*--N--*
|  |  |
2  4  6


Instead of copy/pasting your way to all sorts of variables with 1st, 2nd, 3rd .... suffixes;
structs, arrays and loops will significantly cut down on the amount of code to write.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
struct pos {
  int x;
  int y;
} gearPos[6] = {
  { 524, 1023 },
  // etc
};

// true, if the joystick position is getting close to one of the select positions
bool isNearTo(const struct pos *p, int x, int y ) {
  return abs(x-p->x) < 50 && abs(y-p->y) < 50;
}

// find the gear the joystick is near to
int gearSelect(int x, int y ) {
  for ( i = 0 ; i < 6 ; i++ ) {
    if ( isNearTo(&gearPos[i],x,y) ) {
      return i+1;
    }
  }
  return 0;  // Neutral
}


If you want the user to actually trace out the path (like going via the * position on the way to the 1 position), then you can enhance things.

But try to solve it once (generally), not 6 times with copy/paste.
Nov 14, 2019 at 6:46pm
also look at how often you actually change data. if the loop is running 10 times faster than the input, it can be throttled to let some other piece of code run more.
Nov 18, 2019 at 9:20am
thanks jonnin, i will be throttling it

so far i've dropped it down to
1
2
Sketch uses 2220 bytes (6%) of program storage space. Maximum is 32256 bytes.
Global variables use 200 bytes (9%) of dynamic memory, leaving 1848 bytes for local variables. Maximum is 2048 bytes.


and @salem c
am i doing the structure correctly?
i honestly forgot about them and classes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28


struct Position{
	int x;
	int y;
}
gearPositions[] = {
  { 524, 505},// natural gear
  {0, 1023},  //1st gear
  {1023, 1023}, //3rd gear
  {0, 504},  //4th gear
  {1023, 504}, //5th gear
  {0,0}, //6th gear
  {1023, 0}   //reverse
};


int main()
{
	Position firstGearX =gearPositions[0];
	Position firstGearY = gearPositions[1];
	firstGearX.x; 
	firstGearY.y;

	std::cout << firstGearX.x <<" " <<firstGearY.y;

}


edit: the completed led shifter program works well, and is also 237 lines long, the first 200 lines are from the condition statements checking what gear is engaged and pointing it to move to the next function.
Last edited on Nov 18, 2019 at 9:22am
Topic archived. No new replies allowed.