iwalther
Posts: 3
Joined: Tue Feb 04, 2020 12:00 pm

WiringPi: Two arguments for one pin active at the same time

Tue Feb 11, 2020 11:25 am

So I have a code for my Raspberry Pi robot that uses wiringpi to control two dc motors. The arguments I use are based on where on the frame the center of a ball is, if it is in the left, middle or right region. The code works fine when turning on two pins to turn the robot left or right, but when I have my ball in the middle region of the frame of my screen two pins are supposed to turn on in order to make the robot go towards the ball. That doesn't happen.

I believe this is because I am trying to turn on pins that are simultaneously being told to be turned off when not in the left or right region of the frame.

I am using pins 7, 0, 2 and 3. I use 0 and 3 when turning left, 7 and 2 when turning right and when going forward I want to use pin 7 and three, as shown below. So the problem is that I cannot enable pin 7 and 3 for going forward because, well, those pins are told to be turned off when the center of my ball is not in the left or right region.

I also tried not turning off the pins when center.x is not in their respective region, but the result of that was worse.

Is there a fix for this?

Code: Select all

if(center.x > leftBorder && center.x < rightBorder)
            {
                cout << "Ball is in the middle" << endl;
                softPwmWrite(7,wheelspeed);
                softPwmWrite(3,wheelspeed);

            }
            else
            {
                softPwmWrite(7,0);
                softPwmWrite(3,0);
            }

            if(center.x < leftBorder)
            {
                cout << "Ball is to the left" << endl;
                softPwmWrite(0,wheelspeed);
                softPwmWrite(3,wheelspeed);
            }
            else
            {
                softPwmWrite(0,0);
                softPwmWrite(3,0);
            }

            if(center.x > rightBorder)
            {
                cout << "Ball is to the right" << endl;
                softPwmWrite(7,wheelspeed);
                softPwmWrite(2,wheelspeed);
            }
            else
            {
                softPwmWrite(7,0);
                softPwmWrite(2,0);
            }
Last edited by iwalther on Tue Feb 11, 2020 11:51 am, edited 1 time in total.

pcmanbob
Posts: 9611
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: WiringPi: Two arguments for one pin active at the same time

Tue Feb 11, 2020 11:44 am

Hi.

So you are trying to turn the same gpio pin on and off at the same time !! using different parts of your code.

that will not work as you have found.

all that will happen is that one part of your code will turn the pin on and a micro second later another part of your code will turn it off, you end up with a voltage somewhere between 0 and 3.3v on your gpio pin depending on how fast the switching takes place.

you need to redesign your code so that only one part of the code can control the gpio pins at a time.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

iwalther
Posts: 3
Joined: Tue Feb 04, 2020 12:00 pm

Re: WiringPi: Two arguments for one pin active at the same time

Tue Feb 11, 2020 11:49 am

pcmanbob wrote:
Tue Feb 11, 2020 11:44 am
Hi.

So you are trying to turn the same gpio pin on and off at the same time !! using different parts of your code.

that will not work as you have found.

all that will happen is that one part of your code will turn the pin on and a micro second later another part of your code will turn it off, you end up with a voltage somewhere between 0 and 3.3v on your gpio pin depending on how fast the switching takes place.

you need to redesign your code so that only one part of the code can control the gpio pins at a time.
Precisely. My actual question should be: how I can redesign my code in that fashion?

pcmanbob
Posts: 9611
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: WiringPi: Two arguments for one pin active at the same time

Tue Feb 11, 2020 12:00 pm

looking at the code you posted you have an else statement after every if, this is what is causing your problem,

for example if the first if statement is true, then 7,3 are on, but then the second and third if statements are false so the 2 else statements set 0,3,7,2 to off.

what you need to do is set all pins for each if statement and remove the else statements.

so 1st if would set 7,3 on and 0,2 off and so on for each if statement.

you do realised wiringpi had be depreciated , http://wiringpi.com/wiringpi-deprecated/
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

iwalther
Posts: 3
Joined: Tue Feb 04, 2020 12:00 pm

Re: WiringPi: Two arguments for one pin active at the same time

Tue Feb 11, 2020 12:25 pm

pcmanbob wrote:
Tue Feb 11, 2020 12:00 pm
looking at the code you posted you have an else statement after every if, this is what is causing your problem,

for example if the first if statement is true, then 7,3 are on, but then the second and third if statements are false so the 2 else statements set 0,3,7,2 to off.

what you need to do is set all pins for each if statement and remove the else statements.

so 1st if would set 7,3 on and 0,2 off and so on for each if statement.

you do realised wiringpi had be depreciated , http://wiringpi.com/wiringpi-deprecated/
Firstly, I want to thank you! You're method of having an if statement for each pin worked! :)

Secondly, yes, I do know that wiringPi is deprecated, but that does not mean that it is not useful for control of the GPIO pins.

Return to “Automation, sensing and robotics”