Conditional Operator

Is the conditional operator ___ ? ___ : ___ usable in the arduino IDE?

I'm trying to do:

arrows.nor = PS3.getButtonClick(UP) ? true : false;

For determining button clicks of the USBHost with a PS3 controller connected to it.

Arrows is a struct:

struct buttons{
boolean nor;
boolean eas;
boolean sou;
boolean wes;
};

And I have delcared arrows as:
buttons arrows = {false, false, false, false};

Is the operator usable (or am I using it incorrectly)? Or should I just use an if-else statement?

I think it is.

My Full code currently is:
#include <Usb.h>
#include <PS3USB.h>

#include <SPI.h>

short joyLX = 0;
short oJoyLX = 128;
short joyLY = 0;
short oJoyLY = 128;

short joyRX = 0;
short oJoyRX = 128;
short joyRY = 0;
short oJoyRY = 128;

short triL = 0;
short oTriL = 300;
short triR = 0;
short oTriR = 300;

struct buttons{
boolean nor;
boolean eas;
boolean sou;
boolean wes;
};

boolean trigL = false;
boolean trigR = false;

USB Usb;

PS3USB PS3(&Usb);

void setup(){
Serial.begin(115200);

if(Usb.Init() == -1){
Serial.print(“Failed start up.”);
while(1);
}
else
Serial.print(“PS3 startup succesful!”);

buttons shapes = {false, false, false, false};
buttons arrows = {false, false, false, false};
}

void loop(){
Usb.Task();

if(PS3.PS3Connected || PS3.PS3NavigationConnected){
joyLX = PS3.getAnalogHat(LeftHatX);
joyLY = PS3.getAnalogHat(LeftHatY);
joyRX = PS3.getAnalogHat(RightHatX);
joyRY = PS3.getAnalogHat(RightHatY);

if(joyLX != oJoyLX){
Serial.print(“Left Joystick X:”);
Serial.println(joyLX);
oJoyLX = joyLX;
}
if(joyLY != oJoyLY){
Serial.print(“Left Joystick Y:”);
Serial.println(joyLY);
oJoyLY = joyLY;
}
if(joyRX != oJoyRX){
Serial.print(“Right Joystick X:”);
Serial.println(joyRX);
oJoyRX = joyRX;
}
if(joyRY != oJoyRY){
Serial.print(“Right Joystick Y:”);
Serial.println(joyRY);
oJoyRY = joyRY;
}

shapes.nor = PS3.getButtonClick(TRIANGLE) ? true : false;
shapes.eas = PS3.getButtonClick(CIRCLE) ? true : false;
shapes.sou = PS3.getButtonClick(CROSS} ? true : false;
shapes.wes = PS3.getButtonClick(SQUARE) ? true : false;

arrows.nor = PS3.getButtonClick(UP) ? true : false;
arrows.eas = PS3.getButtonClick(RIGHT) ? true : false;
arrows.sou = PS3.getButtonClick(DOWN) ? true : false;
arrows.wes = PS3.getButtonClick(LEFT) ? true : false;

Serial.println("/\ O X ");
Serial.println(shapes.nor, shapes.eas, shapes.sou, shapes.wes);

Serial.println(“U R D L”);
Serial.println(arrows.nor, arrows.eas, arrows.sou, arrows.wes);
}


My errors are:
This report would have more information with
“Show verbose output during compilation”
enabled in File > Preferences.
Arduino: 1.0.6 (Windows NT (unknown)), Board: “Arduino Uno”
sketch_may01a.ino: In function ‘void loop()’:
sketch_may01a:79: error: ‘shapes’ was not declared in this scope
sketch_may01a:81: error: expected )' before '}' token sketch_may01a:81: error: expected ;’ before ‘}’ token
sketch_may01a:81: error: expected primary-expression before ‘?’ token
sketch_may01a:82: error: ‘shapes’ was not declared in this scope
sketch_may01a:84: error: ‘arrows’ was not declared in this scope
sketch_may01a:89: error: unknown escape sequence: ‘\040’

shapes is defined in the setup function, and worse yet only in that else block.

That means it is only available in that one else block in the setup function. It doesn't exist in the loop function or anywhere else. Maybe it should be global?

Look up "C++ variable scope" and see if you don't learn what you need to know here.

The ? operator is valid C++, so it is definitely available for programming on the Arduino since the Arduino is programmed using C++.

@ jgbowers : Please edit your post, select the code, and put it between [code][/code] tags.

You can do that by hitting the “Code” button above the posting area (It looks like a scroll with < > inside it).

if getButtonClick returns a boolean, why use a boolean to select an identical boolean:

      shapes.nor = PS3.getButtonClick(TRIANGLE) ? true : false;

instead of

      shapes.nor = PS3.getButtonClick(TRIANGLE);

? unless I'm missing something obvious?

aarg:
if getButtonClick returns a boolean, why use a boolean to select an identical boolean:

      shapes.nor = PS3.getButtonClick(TRIANGLE) ? true : false;

instead of

      shapes.nor = PS3.getButtonClick(TRIANGLE);

? unless I'm missing something obvious?

You are not. Much better idea haha. I guess I was just hung up on wanting to use the ?: operator.

Delta_G:
shapes is defined in the setup function, and worse yet only in that else block.

That means it is only available in that one else block in the setup function. It doesn't exist in the loop function or anywhere else. Maybe it should be global?

Look up "C++ variable scope" and see if you don't learn what you need to know here.

The ? operator is valid C++, so it is definitely available for programming on the Arduino since the Arduino is programmed using C++.

Delta_G:
shapes is defined in the setup function, and worse yet only in that else block.

That means it is only available in that one else block in the setup function. It doesn't exist in the loop function or anywhere else. Maybe it should be global?

Look up "C++ variable scope" and see if you don't learn what you need to know here.

The ? operator is valid C++, so it is definitely available for programming on the Arduino since the Arduino is programmed using C++.

Delta_G:
shapes is defined in the setup function, and worse yet only in that else block.

That means it is only available in that one else block in the setup function. It doesn't exist in the loop function or anywhere else. Maybe it should be global?

Look up "C++ variable scope" and see if you don't learn what you need to know here.

The ? operator is valid C++, so it is definitely available for programming on the Arduino since the Arduino is programmed using C++.

It's just setup, it's not in the else. The else didn't have { } so it only takes the first line after my else.

But you are correct, moving it out of setup worked.

I will do this in the future, the code block did not pop up when I edited.

I found my issue though, I had put a } instead of ) on one of my getClicks

Thank you all for the help!