// Bonus points constructor, which allows the user to connect a custom function // to a bumper. RedBotBumper::RedBotBumper(int pin, void(*functionPointer)(void)) { setPinChangeInterrupt(pin, WHISKER); pinMode(pin, INPUT_PULLUP); setBumpFunction(pin, functionPointer); _pin = pin; // set local variable for the pin }
RedBotEncoder::RedBotEncoder(int lPin, int rPin) { // RedBot only breaks out ten valid pins: // A0-A5 a.k.a. D14-19 (PCINT 8-13) // D3 (PCINT 19) // D9-D11 (PCINT 1-3) // We'll need a whopping case statement to set up the pin change interrupts // for this; in fact, we'll need two, but I'll abstract it to a function. // A call to setPinChangeInterrupt() enables pin change interrupts for that // pin, and pin change interrupts for the group that pin is a part of. pinMode(lPin, INPUT); pinMode(rPin, INPUT); setPinChangeInterrupt(lPin, LENCODER); setPinChangeInterrupt(rPin, RENCODER); lCounts = 0; rCounts = 0; encoderObject = this; // We want a local pointer to the class member that is // instantiated in the sketch, so we can manipulate its // private members with other classes. }
// Bonus points constructor, which allows the user to connect a custom function // to a bumper. RedBotBumper::RedBotBumper(int pin, void(*functionPointer)(void)) { setPinChangeInterrupt(pin, WHISKER); pinMode(pin, INPUT_PULLUP); setBumpFunction(pin, functionPointer); }
// Standard class constructor, assumes that you want to halt the motors on a // bump. A more skilled programmer than I could figure out the error message // I get if I try to use the brake() function that's a part of the RedBotMotor // class; I worked around it by making a globally available one. RedBotBumper::RedBotBumper(int pin) { setPinChangeInterrupt(pin, WHISKER); pinMode(pin, INPUT_PULLUP); setBumpFunction(pin, &brake); }