Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What language should I use for an automatic gear shifting program? [closed]

I was thinking about writing a program to automatically change the gear of my bike. It may require a microprocessor, so the question which I had in my mind was: is there any different language for programming a microprocessor or can this be done using C and C++?

Another thing is, regarding the program, can a switch statement do my work or do I need a pointer or linked list because the gear works both ways, up and down? I was a bit confused with the switch statement thing!

like image 816
abhinav Avatar asked Nov 26 '22 22:11

abhinav


2 Answers

You don't need the switch statement, just use the shift operator:

Shift up:

gear <<= 1;

Shift down:

gear >>= 1;

like image 169
Curd Avatar answered Nov 29 '22 13:11

Curd


I'd probably use neither pointer, link list or switch to write it tough.

First thing you need to know is what inputs you have and how to get them.
Then you need to know what outputs you can send and how.

Supposing you can read the rpm as a C variable, and that another variable controls the gear, this should work:

while (1) {
    if (rpm <= 3) chggear(-1);
    if (rpm >= 7.4) chggear(+1);
}

where the function chggear would change the gear and wait a convenient amount of time to make sure the next gear is engaged before returning to the loop.

int chggear(int direction) {
    gear += direction;
    sleep(10); /* wait for gear to engage */
    return gear; /* return currently engaged gear */
}

Edit you can also change directly to a specific gear no matter what the gearbox is doing:

int jumptogear(int geartojump) {
    gear = geartojump;
}

and use it like this

if (breaking) jumptogear(1); /* and possibly break gearbox */
like image 36
pmg Avatar answered Nov 29 '22 11:11

pmg