Search code examples
cmicrocontroller

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


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!


Solution

    1. One solution designed for prototyping and hobby applications is the BASIC Stamp, based on a PIC microcontroller but including an interpreter. It is programmed in BASIC rather than C/C++ if that is what you are looking for. Keep in mind that that there are microcontrollers that are "worse" than the one you have; they have to be programmed in assembly language.

    2. No pointers or linked lists are required, which is good because microcontrollers usually have a severely limited memory capacity. The switch statement will work fine; just remember to include break statements to avoid problems with fall-through. It's also possible to use bit-shifting. Yes, Curd's answer has some truth in it:

      PORTA = PORTA & ~0x07 | (1 << selected_gear);
      

      where selectedGear starts at zero and your bike has three speeds. Just write some code to read the sensor inputs and determine the correct gear that the bike should be in. To do so, you would probably use a finite state machine.