function - outputs adding

Hi

i have problem with 2 functions:

function 2 can only be turned on if function 1 is in a set range,
however if i turn on func 2 first and then func 1 the loop adds 2+1

have tried alot of code combinations but it always does the same thing..

is there a simple way to kill func 2 if func 1 is activated?

Without seeing your code I have no idea

...R

yes

function 1

FTL = (analogRead(A1) > 562) && (analogRead(A0) > 562);
FTR = (analogRead(A1) > 562) && (analogRead(A0) < 562);

//

if (FTL)
{ 
  analogWrite(9, pYX);
  analogWrite(10, pwmX);
}
else if (FTR)
{
  analogWrite(10, pYX);
  analogWrite(9, pwmX);
}

function 2

  ctl1 = (pwmX < 30 && pwmY < 30);
  ctr1 = (pwmX < 30 && pwmY < 30);
  
  CTL = ctl1 && (analogRead(A2) < 497);
  CTR = ctr1 && (analogRead(A2) > 597);

//

else if (CTL)
{
  analogWrite(10, pwmZ);
  analogWrite(11, pwmZ);
}

where pwmX-Y-Z are pwm inputs

Here's a tip

sorry!

heres all the code then:

int pwmX = 0;
int pwmY = 0;
int pwmZ = 0;
int pYX = 0;
int FTL = 0;
int FTR = 0;
int RTL = 0;
int RTR = 0;
int ctl = 0;
int ctr = 0;
int CTL = 0;
int CTR = 0;

void setup() {

  Serial.begin(9600);
}

void loop() {
  
  Serial.print("pwmZ : ");
  Serial.println(pwmZ);

  pwmX = map(analogRead(A1), 0, 1023, 255, -255);
  pwmX = abs(pwmX);
  delay(10);
 
  pwmY = map(analogRead(A0), 0, 1023, 255, -255);
  pwmY = abs(pwmY);
  delay(10);
  
  pwmZ = map(analogRead(A2), 90, 1023, -255, 255);
  pwmZ = abs(pwmZ);
  pwmZ = constrain(pwmZ, 0, 180);
  delay(10);


  FTL = (analogRead(A1) > 562) && (analogRead(A0) > 562); 
  FTR = (analogRead(A1) > 562) && (analogRead(A0) < 562);
  RTL = (analogRead(A1) < 462) && (analogRead(A0) > 562);
  RTR = (analogRead(A1) < 462) && (analogRead(A0) < 562);
  
  ctl = (pwmX < 30 && pwmY < 30);
  ctr = (pwmX < 30 && pwmY < 30);
  CTL = ctl && (analogRead(A2) < 497);
  CTR = ctr && (analogRead(A2) > 597);
  
  pYX = pwmX - pwmY;
  pYX = constrain(pYX, 0, 255);
  
  int throttle = analogRead(A3);//engine throttle potentionmeter
  int autoT = (FTL) || (FTR) || (RTL) || (RTR) || (CTL) || (CTR);
  throttle = constrain(throttle, 0, 1023);
  
if (FTL)//forward and turn left
{ 
  analogWrite(9, pYX);
  analogWrite(10, pwmX);
}
else if (FTR)//forward and turn right
{
  analogWrite(10, pYX);
  analogWrite(9, pwmX);
}
else if (RTL)//reverse and turn left
{
  analogWrite(11, pYX);
  analogWrite(3, pwmX);
}
else if (RTR)//reverse and turn right
{
  analogWrite(3, pYX);
  analogWrite(11, pwmX);
}
else if (CTL)//center turn right
{
  analogWrite(10, pwmZ);
  analogWrite(11, pwmZ);
}
else if (CTR)//center turn right
{
  analogWrite(3, pwmZ);
  analogWrite(9, pwmZ);
}


else if (analogRead(A0))//joystick center cutoff
{
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  digitalWrite(3, LOW);
  digitalWrite(11, LOW);
  digitalWrite(5, LOW);
}
else if (analogRead(A1))//joystick center cutoff
{
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  digitalWrite(3, LOW);
  digitalWrite(11, LOW);
  digitalWrite(5, LOW);
}

if (autoT)
{
  analogWrite(5, throttle / 4);
}
}

What do you have attached to A0 and A1?
Also why do you treat analogRead(A0) and analogRead(A1) as if they're digital values? (I'm referring to this piece of code)

else if (analogRead(A0))//joystick center cutoff
{
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  digitalWrite(3, LOW);
  digitalWrite(11, LOW);
  digitalWrite(5, LOW);
}
else if (analogRead(A1))//joystick center cutoff
{
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  digitalWrite(3, LOW);
  digitalWrite(11, LOW);
  digitalWrite(5, LOW);
}

heres all the code then:

That does not sound like it came from a person who is trying to encourage others to give him free advice

And, after all that I can see only 2 functions in your program - setup() and loop().

Consequently I don't understand your question. What do you mean (in your original post) by func1 and func2

...R

A0, A1 and A2 are all potentiometers on a joystick (A2 turning/twisting the joystick)

i did this code to kill noise in on the output when the joystick is centered else it would flicker the leds!
however i have cleaned the code quite a bit after that issue so it could possibly be cut out now..

Robin2:
That does not sound like it came from a person who is trying to encourage others to give him free advice

very sorry about that, it was not my intention!

i dont know much about programing and wouldnt know what to cut and paste to make sens when my other post didnt!

And, after all that I can see only 2 functions in your program - setup() and loop().

Consequently I don't understand your question. What do you mean (in your original post) by func1 and func2

my bad again, the term im looking for is perhaps outputs?

mtnhll:
my bad again, the term im looking for is perhaps outputs?

Now that you know "function" is the wrong word, it would help if you re-write your question so I don't have to guess what you mean. Use the variable names from your code so there is no confusion.

...R

ok ill try my best:

the inputs from 3 pots goes to 4 pwm outputs,

pot 1 and 2 control the outputs in pairs: (9 & 10) and (3 & 11)
pot 3 controls the same outputs but as pairs: (3 & 9) and (10 & 11)

when pots 1-2 is active pot 3 cant be activated
but when pot 3 is input first and then pot 1-2 it adds that output on top off the pot 3 output

there can never be more than 2 outputs (hardware will brake) but as described above i get 3 outputs at once (even 4 if i jiggle a bit in low values of the pots)

everything works besides that i need to kill the pot 3 output when i get pot 1-2 input
all i've tried either does nothing or mess with the pot 1-2 outputs

does this make more sens or just worse?
this code will control a 2 ton chunk and kind of needs to work proper...or things can go bad... :stuck_out_tongue:

mtnhll:
ok ill try my best:

the inputs from 3 pots goes to 4 pwm outputs,

pot 1 and 2 control the outputs in pairs: (9 & 10) and (3 & 11)
pot 3 controls the same outputs but as pairs: (3 & 9) and (10 & 11)

when pots 1-2 is active pot 3 cant be activated
but when pot 3 is input first and then pot 1-2 it adds that output on top off the pot 3 output

there can never be more than 2 outputs (hardware will brake) but as described above i get 3 outputs at once (even 4 if i jiggle a bit in low values of the pots)

everything works besides that i need to kill the pot 3 output when i get pot 1-2 input
all i've tried either does nothing or mess with the pot 1-2 outputs

does this make more sens or just worse?
this code will control a 2 ton chunk and kind of needs to work proper...or things can go bad... :stuck_out_tongue:

I think you need to draw a diagram.

KenF:
I think you need to draw a diagram.

well... yeah... will do! :slight_smile:

well since a computer crash im stuck with the good stuff.. ms paint..

hope this will clarify something at least!

flowchart2.jpg

mtnhll:
ok ill try my best:

That description is much better.

But I can't figure out how the Arduino knows which state it should be in - how does it know

when pots 1-2 is active pot 3 cant be activated
but when pot 3 is input first

What do you mean by "is active" and how do you distinguish it from "is not active"

...R

  ctl = (pwmX < 30 && pwmY < 30);
  ctr = (pwmX < 30 && pwmY < 30);
  CTL = ctl && (analogRead(A2) < 497);
  CTR = ctr && (analogRead(A2) > 597);

this piece of code prohibits the use of pot 3 (A2) when there is a input from pot1 pwmX(A1) or pot2 pwmY(A0), but it only prohibits you from begining to use pot 3 input!

Robin2:
What do you mean by "is active" and how do you distinguish it from "is not active"

...R

the input and output from pot 3 is supposed to be secondery, mening that if an input comes from pot 1 or 2 it overrides the pot 3 input/output..
ive been trying alot of true/false and other to define that but to no avail...

mtnhll:
this piece of code prohibits the use of pot 3 (A2) when there is a input from pot1 pwmX(A1) or pot2 pwmY(A0), but it only prohibits you from begining to use pot 3 input!

I can't see how that will work the way you want it to.

What it amounts to is that the use of potA is determined by the position that potB is set at. But there is nothing to prevent potB being changed to a different position by the user.

It seems to me that you want a situation where you can waggle potA as you wish and certain things will happen. Then you want to move potA to a special position following which you can waggle potB as you wish and different things will happen. Presumably you need to move potB to its special position before you can go back to waggling potA.

If that is what you want to do I think it would be easy to code. The complicated thing would be to ensure the user put the pot in the correct parking position.

But if you need to be able to use both pots at the same time in either or both circumstances that simple state distinction cannot be made. Something special would have to be done to tell the Arduino it is now in stateA or stateB. That something could be a separate switch. Or maybe it could happen if potA is parked in position1 and potB is parked in position2 for (say) 5 secs. Then to switch to the other state you could put potA in position2 and potB in position1. But that sounds very inconvenient.

...R

Robin2:
It seems to me that you want a situation where you can waggle potA as you wish and certain things will happen. Then you want to move potA to a special position following which you can waggle potB as you wish and different things will happen.

this is indeed what i want, and the code is in fact working like this except:

Waggle potA and then potB (without parking potA), nothing else happens- works great!

Waggle potB and then potA (without parking potB), output becomes potB+potA= the problem..

the pots are spring returned so if you let them go they return to their neutral center position..

Robin2:
Presumably you need to move potB to its special position before you can go back to waggling potA.

this i should achieve by replicating my working code for potA,

BUT what i really would want is not having to park potB, but waggling potA would suspend and surpass potBs actions, however this might not be possible then?? (in a simple way, so even i could do it :)))

mtnhll:
the pots are spring returned so if you let them go they return to their neutral center position

It has taken a long time to get this important piece of information.
What else have you hidden up your sleeve?

Do you consider the neutral position to be the parked position?

I'm conscious that if the neutral position is in the centre the pot will pass through it lots of time during a waggle. How will the Arduino distinguish a passing visit from a "park"?

BUT what i really would want is not having to park potB, but waggling potA would suspend and surpass potBs actions, however this might not be possible then??

The only way I can think of is by keeping track of whether the position is changing. I.e. if the position of potA has not changed for X seconds potB is to be considered the master and while it is the master any movement of potA will be ignored. If you stop moving potB for X seconds then potA would become the master. But I don't know if that is practical in your application and I suspect it would not be reliable - how would the user hold the pot steady for X seconds?

...R