Putting 2 analogwrite commands in one if statement

Hi All,
I’m trying to code a joystick to control pitching of a quadcopter. I want to put the 2 analogWrite commands inside of the same if statement, if that is possible. If it isn’t possible, I’ll need to find an alternative I suppose…
When I try to compile the code the way I have it, it gives the error of no semicolon, but if I put a semicolon, the if statement will not include the second analogWrite command…
I’m also trying to declare the analog number value 2 (not a pin) as the word “slow” I’m not getting a compiler error so I think it works but I’m wondering why doesn’t the arduino thing that digital pin 2 is called “slow”?
Anyway here is the code
<
int x_plane = A1;
int y_plane = A0;
int x_pos;
int y_pos;
int sepin = 11;
int swpin = 9;
int nepin = 10;
int nwpin = 6;
int hover=40;
int fast=100;
int slow=2;

void setup() {

Serial.begin(9600);
pinMode(sepin, OUTPUT);
pinMode(swpin, OUTPUT);
pinMode(nepin, OUTPUT);
pinMode(nwpin, OUTPUT);
analogWrite(sepin, hover);
analogWrite(swpin, hover);
analogWrite(nepin, hover);
analogWrite(nwpin, hover);

}

void loop() {

x_pos = analogRead(x_plane);
y_pos = analogRead(y_plane);

if (x_pos<300)
analogWrite(swpin && nwpin, slow)
analogWrite(sepin && nepin, fast);

if (x_pos>580)
analogWrite(swpin && nwpin, fast)
analogWrite(sepin && nepin, slow);

if (y_pos<300)
analogWrite(swpin && sepin, slow)
analogWrite(nwpin && nepin, fast);

if (y_pos>580)
analogWrite(nepin && nwpin, slow)
analogWrite(sepin && swpin, fast);

delay(500);
}

You have already used curley brackets, {} in several places. Why have you not used them with your "if" statements?

Paul

Hi Paul,
Thank you for your comment. I just now put curly brackets on the if statements like this:
<
if (x_pos<300){
analogWrite(swpin && nwpin, slow)
analogWrite(sepin && nepin, fast);
}

The errors that I first described still exist now

At least you made an attempt to use code tags

Replace the < before your code by
** **[code]** **

Replace the > after your code by
** **[/code]** **

Your code does not compile due to missing semi-colons; fix that first

The IDE provides a function to properly indent your code; IDE menu tools → autoformat. After fixing the semi-colons and doing an autoformat, you will see

void loop() {

  x_pos = analogRead(x_plane);
  y_pos = analogRead(y_plane);

  if (x_pos < 300)
    analogWrite(swpin && nwpin, slow);
  analogWrite(sepin && nepin, fast);


  if (x_pos > 580)
    analogWrite(swpin && nwpin, fast)
    analogWrite(sepin && nepin, slow);


  if (y_pos < 300)
    analogWrite(swpin && sepin, slow);
  analogWrite(nwpin && nepin, fast);


  if (y_pos > 580)
    analogWrite(nepin && nwpin, slow);
  analogWrite(sepin && swpin, fast);


  delay(500);
}

The fact that the second analogWrite after an if is not indented indicates that it’s not part of the code that get executed conditionally. As @Paul_KD7HB said, embed what needs to be conditionally executed between { and }.

When you get erros, please post them.

analogWrite(swpin && nwpin, slow);?

analogWrite(swpin && nwpin, slow);

Are you trying to analogWrite() the 'slow' value to two different pins? That's not the way to do it. Each pin has to be written separately:

analogWrite(swpin, slow);
analogWrite(nwpin, slow);

Once you read the joystick, how are you planning on using that to control the quadcopter?

-jim lee

jimLee:
Once you read the joystick, how are you planning on using that to control the quadcopter?

-jim lee

Hi Jim Lee,
This joystick will control only the pitch of the quadcopter, and I will code another joystick for elevation and yaw. I was planning on using a separate arduino or other circuit board in the quadcopter to wirelessly connect to the one that has joysticks connected to it.

Thank you all for the replies, they've been helpful, and the code is better. It still needs lots of work