Arduino IR Servo Controller

I would like to make a servo IR controlled by my TV remote. I would like to control my surround sound system which doesn’t have a remote to control volume. The code i use is from someone else but i am trying to modify to work for me.

The code will detach the servo but will now attach the servo.

Here I am trying to detach the servo and attach the servo back when the button is pressed again:

            case SELECT_BUTTON:       
              ++OnOff;
              if (OnOff > 0; OnOff <= 1) {
                My_Servo.detach();
              }else if (OnOff > 1) {
                My_Servo.attach(9);
                OnOff = 0;
                My_Servo.write(pos);
              }
            break;

I would also like to put a LED in there so that i know whether it is on or not. Please help. This is one the first Arduino projects I’ve done and is the first with a servo.

Can you tell when this condition is true:

             if (OnOff > 0; OnOff <= 1)

I had guessed that it never compiles.

As a general hint: always add an unconditional “else” to every if-else-if construct, where you can place diagnostic output when this unexpected branch is taken.

Looks like you're doing a long wrong complicated thing when you're just trying to make a boolean toggle with a flag.

DrDiettrich:
Can you tell when this condition is true:

             if (OnOff > 0; OnOff <= 1)

I had guessed that it never compiles.

As a general hint: always add an unconditional “else” to every if-else-if construct, where you can place diagnostic output when this unexpected branch is taken.

The code compiles just fine. the statement will be true when 1 is added to the OnOff int. The servo will also shut off when i press the button but not turn on again.

INTP: Looks like you're doing a long wrong complicated thing when you're just trying to make a boolean toggle with a flag.

As I have stated I'm very new to this. Could you please explain?

The code compiles just fine. the statement will be true when 1 is added to the OnOff int.

Not for me, using IDE 1.6.11

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  int OnOff=0;
  if (OnOff > 0; OnOff <= 1) Serial.print("true");
}

void loop() {
  // put your main code here, to run repeatedly:

}

Result:

C:\Users\Justme\Desktop\Arduino\sketch_oct01a\sketch_oct01a.ino: In function 'void setup()':

sketch_oct01a:5: error: expected ')' before ';' token

   if (OnOff > 0; OnOff <= 1) Serial.print("true");

                ^

C:\Users\Justme\Desktop\Arduino\sketch_oct01a\sketch_oct01a.ino:5:16: warning: suggest braces around empty body in an 'if' statement [-Wempty-body]

sketch_oct01a:5: error: expected ';' before ')' token

   if (OnOff > 0; OnOff <= 1) Serial.print("true");

                            ^

C:\Users\Justme\Desktop\Arduino\sketch_oct01a\sketch_oct01a.ino:5:24: warning: statement has no effect [-Wunused-value]

   if (OnOff > 0; OnOff <= 1) Serial.print("true");

                        ^

exit status 1
expected ')' before ';' token

I think you mean

   if (OnOff == 1) {

jremington:
Not for me, using IDE 1.6.11

void setup() {

// put your setup code here, to run once:
  Serial.begin(9600);
  int OnOff=0;
  if (OnOff > 0; OnOff <= 1) Serial.print(“true”);
}

void loop() {
  // put your main code here, to run repeatedly:

}




Result:



C:\Users\Justme\Desktop\Arduino\sketch_oct01a\sketch_oct01a.ino: In function ‘void setup()’:

sketch_oct01a:5: error: expected ‘)’ before ‘;’ token

if (OnOff > 0; OnOff <= 1) Serial.print(“true”);

^

C:\Users\Justme\Desktop\Arduino\sketch_oct01a\sketch_oct01a.ino:5:16: warning: suggest braces around empty body in an ‘if’ statement [-Wempty-body]

sketch_oct01a:5: error: expected ‘;’ before ‘)’ token

if (OnOff > 0; OnOff <= 1) Serial.print(“true”);

^

C:\Users\Justme\Desktop\Arduino\sketch_oct01a\sketch_oct01a.ino:5:24: warning: statement has no effect [-Wunused-value]

if (OnOff > 0; OnOff <= 1) Serial.print(“true”);

^

exit status 1
expected ‘)’ before ‘;’ token




I think you mean


if (OnOff == 1) {

The code here is only part of the full code. I just cant get this part to function when uploaded. And I am using Arduino IDE 1.6.12

jremington: I think you mean

   if (OnOff == 1) {

This fixed the problem. I am a programmer and forgot to do the == thing. I am completely stupid.