Skip to main content
added 1 character in body
Source Link

I'm not sure why you're checking for z's and so on but your first else if clause ensures that none of the clauses in the rest of the block is ever executed.

else if (state != 'z' && state!=0) {
  digitalWrite(ledG, LOW);}    // turn the LED off by making the voltage LOW

When state is any value that's neither z nor 0 (x, y, etc) then this block is executed and the rest are skipped. It seems state is seldom 0, so all you get is an LED that's off for any x or y values. What you should probably do instead is something like:

int orientx;
int orienty;
int orientz;

switch (state) {
  case 'z':
    orientz = valueZ.toInt();
    digitalWrite(ledG, HIGH);
    digitalWrite(ledR, LOW);
    digitalWrite(ledW, LOW);
    break;
 case 'y':
    orienty = valueY.toInt();
    digitalWrite(ledG, LOW);
    digitalWrite(ledR, HIGH);
    digitalWrite(ledW, LOW);
    break;
case 'x':
    orientx = valueX.toInt();
    digitalWrite(ledG, LOW);
    digitalWrite(ledR, LOW);
    digitalWrite(ledW, HIGH);
    break;
}
delay(1);

You can also use if-else statements if you want, though I find this more concise if you'll only be testing the value of the one variable state.

An even more elegant solution would be:

int orientx;
int orienty;
int orientz;

switch (state) {
  case 'z':
    orientz = valueZ.toInt();
    break;
  case 'y':
    orienty = valueY.toInt();
    break;
  case 'x':
    orientx = valueX.toInt();
    break;
}
digitalWrite(ledG, (state == 'z'));
digitalWrite(ledR, (state == 'y'));
digitalWrite(ledW, (state == 'x'));

I'm not sure why you're checking for z's and so on but your first else if clause ensures that none of the clauses in the rest of the block is ever executed.

else if (state != 'z' && state!=0) {
  digitalWrite(ledG, LOW);}    // turn the LED off by making the voltage LOW

When state is any value that's neither z nor 0 (x, y, etc) then this block is executed and the rest are skipped. It seems state is seldom 0, so all you get is an LED that's off for any x or y values. What you should probably do instead is something like:

int orientx;
int orienty;
int orientz;

switch (state) {
  case 'z':
    orientz = valueZ.toInt();
    digitalWrite(ledG, HIGH);
    digitalWrite(ledR, LOW);
    digitalWrite(ledW, LOW);
    break;
 case 'y':
    orienty = valueY.toInt();
    digitalWrite(ledG, LOW);
    digitalWrite(ledR, HIGH);
    digitalWrite(ledW, LOW);
    break;
case 'x':
    orientx = valueX.toInt();
    digitalWrite(ledG, LOW);
    digitalWrite(ledR, LOW);
    digitalWrite(ledW, HIGH);
    break;
}
delay(1);

You can also use if-else statements if you want, though I find this more concise if you'll only be testing the value of the one variable state.

I'm not sure why you're checking for z's and so on but your first else if clause ensures that none of the clauses in the rest of the block is ever executed.

else if (state != 'z' && state!=0) {
  digitalWrite(ledG, LOW);}    // turn the LED off by making the voltage LOW

When state is any value that's neither z nor 0 (x, y, etc) then this block is executed and the rest are skipped. It seems state is seldom 0, so all you get is an LED that's off for any x or y values. What you should probably do instead is something like:

int orientx;
int orienty;
int orientz;

switch (state) {
  case 'z':
    orientz = valueZ.toInt();
    digitalWrite(ledG, HIGH);
    digitalWrite(ledR, LOW);
    digitalWrite(ledW, LOW);
    break;
 case 'y':
    orienty = valueY.toInt();
    digitalWrite(ledG, LOW);
    digitalWrite(ledR, HIGH);
    digitalWrite(ledW, LOW);
    break;
case 'x':
    orientx = valueX.toInt();
    digitalWrite(ledG, LOW);
    digitalWrite(ledR, LOW);
    digitalWrite(ledW, HIGH);
    break;
}
delay(1);

You can also use if-else statements if you want, though I find this more concise if you'll only be testing the value of the one variable state.

An even more elegant solution would be:

int orientx;
int orienty;
int orientz;

switch (state) {
  case 'z':
    orientz = valueZ.toInt();
    break;
  case 'y':
    orienty = valueY.toInt();
    break;
  case 'x':
    orientx = valueX.toInt();
    break;
}
digitalWrite(ledG, (state == 'z'));
digitalWrite(ledR, (state == 'y'));
digitalWrite(ledW, (state == 'x'));
added 1 character in body
Source Link

I'm not sure why you're checking for z's and so on but your first else if clause ensures thethat none of the clauses in the rest of the block is ever executed.

else if (state != 'z' && state!=0) {
  digitalWrite(ledG, LOW);}    // turn the LED off by making the voltage LOW

When state is any value that's neither z nor 0 (x, y, etc) then this block is executed and the rest are skipped. It seems state is seldom 0, so all you get is an LED that's off for any x or y values. What you should probably do instead is something like:

int orientx;
int orienty;
int orientz;

switch (state) {
  case `z`'z':
    orientz = valueZ.toInt();
    digitalWrite(ledG, HIGH);
    digitalWrite(ledR, LOW);
    digitalWrite(ledW, LOW);
    break;
 case `y`'y':
    orienty = valueY.toInt();
    digitalWrite(ledG, LOW);
    digitalWrite(ledR, HIGH);
    digitalWrite(ledW, LOW);
    break;
case `x`'x':
    orientx = valueX.toInt();
    digitalWrite(ledG, LOW);
    digitalWrite(ledR, LOW);
    digitalWrite(ledW, HIGH);
    break;
}
delay(1);

You can also use if-else statements if you want, though I find this more concise if you'll only be testing the value of the one variable state.

I'm not sure why you're checking for z's and so on but your first else if clause ensures the none of the clauses in the rest of the block is ever executed.

else if (state != 'z' && state!=0) {
  digitalWrite(ledG, LOW);}    // turn the LED off by making the voltage LOW

When state is any value that's neither z nor 0 (x, y, etc) then this block is executed and the rest are skipped. It seems state is seldom 0, so all you get is an LED that's off for any x or y values. What you should probably do instead is something like:

int orientx;
int orienty;
int orientz;

switch (state) {
  case `z`:
    orientz = valueZ.toInt();
    digitalWrite(ledG, HIGH);
    digitalWrite(ledR, LOW);
    digitalWrite(ledW, LOW);
    break;
 case `y`:
    orienty = valueY.toInt();
    digitalWrite(ledG, LOW);
    digitalWrite(ledR, HIGH);
    digitalWrite(ledW, LOW);
    break;
case `x`:
    orientx = valueX.toInt();
    digitalWrite(ledG, LOW);
    digitalWrite(ledR, LOW);
    digitalWrite(ledW, HIGH);
    break;
}
delay(1);

You can also use if-else statements if you want, though I find this more concise if you'll only be testing the value of the one variable state.

I'm not sure why you're checking for z's and so on but your first else if clause ensures that none of the clauses in the rest of the block is ever executed.

else if (state != 'z' && state!=0) {
  digitalWrite(ledG, LOW);}    // turn the LED off by making the voltage LOW

When state is any value that's neither z nor 0 (x, y, etc) then this block is executed and the rest are skipped. It seems state is seldom 0, so all you get is an LED that's off for any x or y values. What you should probably do instead is something like:

int orientx;
int orienty;
int orientz;

switch (state) {
  case 'z':
    orientz = valueZ.toInt();
    digitalWrite(ledG, HIGH);
    digitalWrite(ledR, LOW);
    digitalWrite(ledW, LOW);
    break;
 case 'y':
    orienty = valueY.toInt();
    digitalWrite(ledG, LOW);
    digitalWrite(ledR, HIGH);
    digitalWrite(ledW, LOW);
    break;
case 'x':
    orientx = valueX.toInt();
    digitalWrite(ledG, LOW);
    digitalWrite(ledR, LOW);
    digitalWrite(ledW, HIGH);
    break;
}
delay(1);

You can also use if-else statements if you want, though I find this more concise if you'll only be testing the value of the one variable state.

Source Link

I'm not sure why you're checking for z's and so on but your first else if clause ensures the none of the clauses in the rest of the block is ever executed.

else if (state != 'z' && state!=0) {
  digitalWrite(ledG, LOW);}    // turn the LED off by making the voltage LOW

When state is any value that's neither z nor 0 (x, y, etc) then this block is executed and the rest are skipped. It seems state is seldom 0, so all you get is an LED that's off for any x or y values. What you should probably do instead is something like:

int orientx;
int orienty;
int orientz;

switch (state) {
  case `z`:
    orientz = valueZ.toInt();
    digitalWrite(ledG, HIGH);
    digitalWrite(ledR, LOW);
    digitalWrite(ledW, LOW);
    break;
 case `y`:
    orienty = valueY.toInt();
    digitalWrite(ledG, LOW);
    digitalWrite(ledR, HIGH);
    digitalWrite(ledW, LOW);
    break;
case `x`:
    orientx = valueX.toInt();
    digitalWrite(ledG, LOW);
    digitalWrite(ledR, LOW);
    digitalWrite(ledW, HIGH);
    break;
}
delay(1);

You can also use if-else statements if you want, though I find this more concise if you'll only be testing the value of the one variable state.