pwm controlled by relay

in our code we want to be able to control pwms through buttons on the controller, but the only place we can find button commands are on relay blocks, and IF statements haven’t worked for us so far, any ideas? Thanks in advance.

You can use relays to control motors, but you wont have the speed control that using a PWM value would give you.

When you program it assign a button to forward and reverse

relay1_fwd = p1_sw_aux1;
relay1_rev = p1_sw_aux2;

We have done exactly this in the past.

if (p1_sw_top && !p1_sw_trig)
{
pwm04 = 225;
}
else if (!p1_sw_top && p1_sw_trig)
{
pwm04 = 50;
}
else
{
pwm04 = 127;
}

We have to do the same thing, but we have to have the motor turn one direction all the time and the other only when the button is pressed.


if(p2_sw_aux1 == 1)
	{
		relay1_fwd = 1;
		relay1_rev = 0;
	}
else
	{
		relay1_fwd = 0;
		relay1_rev = 1;
	}

I think I should be a little more specific, we want to be able to switch from one set up to another by the push of a button, but we can’t find a way to switch pwms through that. All of our if statements haven’t worked, is there a way to assign for example, tank drive to trigger and arcade mode to aux?

You could use static variables to achieve these means.

static unsigned char Tank_Drive = 1; //default is tank drive

if (p1_sw_trig) Tank_Drive = 1; // enable tank drive
else if (p1_sw_aux1) Tank_Drive = 0; // enable other drive system

if(Tank_Drive)
{
     // code for tank drive here
}
else
{
     // code for other drive system here
}

What this will do is switch to tank drive when you press the trigger and switch to another drive system when you press the aux button. Not when you hold them down, but when you press them.

Yep. That is exactly what we do to produce an “on/off” button effect. It has to be modified slightly to use 1 button for “toggle” instead of 2 distinct “tank” and “other” buttons. I don’t know why you want to drag a relay into this, and I am almost sure I don’t understand what you are asking. Could you explain better?

but the only place we can find button commands are on relay blocks

What about buttons on the joysitck? Digital inputs on the OI? (Or even resistors in parallel?)