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!
You don't need the switch statement, just use the shift operator:
Shift up:
gear <<= 1;
Shift down:
gear >>= 1;
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 */
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With