View Full Version : Pointer to pwm
bronxbomber92
05-02-2008, 19:37
I just wanted to make sure this was safe to do.
typedef struct
{
// stuff
unsigned char* pwm;
} Motor;
void InitMotor( Motor* motor, unsigned char* pwm, /* more parameters */ )
{
// stuff
motor->pwm = pwm;
}
then if I want to change the motor speed
Motor myMotor; InitMotor( myMoter, pwm10 );
myMotor.pwm = 127; // set it to nuetral
Is that all right to do? I assume it is, but just want to make sure.
Thanks
typedef struct
{
// stuff
unsigned char* pwm;
} Motor;
void InitMotor( Motor* motor, unsigned char* pwm, /* more parameters */ )
{
// stuff
motor->pwm = pwm;
}
then if I want to change the motor speed
Motor myMotor; InitMotor( &myMotor, &pwm10 );
*myMotor.pwm = 127; // set it to nuetral
need * to dereference the pointer and & to pass a pointer in the places shown
bronxbomber92
05-02-2008, 20:03
Nope, you're right!
Thanks
Jon Stratis
05-02-2008, 23:36
that seems like an OK way to do it... what might end up being easier, though, is to just do something like:
#define leftMotor pwm01
#define rightMotor pwm02
then you can use your left and right motors wherever you want, and if someone plugs something in backwards and doesn't want to unplug it you just have to change it in the one location in the header file. Just another option to consider!
We use Left(int) and Right(int)void Left(int speed) {
pwm01 = speed;
if(pwm01 > 254) {
pwm01 = 254;
} else if(pwm01 < 1) {
pwm01 = 1;
}
}And the right one assigns 254-speed. Just yet another form of abstraction :]
bronxbomber92
07-02-2008, 15:57
that seems like an OK way to do it... what might end up being easier, though, is to just do something like:
#define leftMotor pwm01
#define rightMotor pwm02
then you can use your left and right motors wherever you want, and if someone plugs something in backwards and doesn't want to unplug it you just have to change it in the one location in the header file. Just another option to consider!
That's what I have currently, and I've debating if I should use a more OO approach, which I'm still not sure of. Just brain storming :)
vBulletin® v3.6.4, Copyright ©2000-2017, Jelsoft Enterprises Ltd.