code help

This code is supposed to make the motor move 90 degrees when it detects an ir signal, then move to 180 degrees after 3 seconds. Could someone tell me whats wrong? I`m a beginner so please keep it simple.

#include <IRremote.h>
#include <Servo.h>  // servo library
int RECV_PIN = 11;
int ledPin = 13;

IRrecv irrecv(RECV_PIN);
decode_results results;
Servo servo1;
void setup()
{
  pinMode(ledPin, OUTPUT);
  pinMode(13, OUTPUT);
  irrecv.enableIRIn(); // Start the receiver
    servo1.attach(9);
}

int on = 0;
unsigned long last = millis();

void loop() {
  if (irrecv.decode(&results))
  int position;
  {


    {


      {
        servo1.write(90);
        delay(3000);
        servo1.write(180);
        delay(1000);
      }
    }

    irrecv.resume(); // Receive the next value
  }
}

aaronyz123: This code is supposed to make the motor move 90 degrees when it detects an ir signal, then move to 180 degrees after 3 seconds. Could someone tell me whats wrong? I`m a beginner so please keep it simple.

It would be a lot easier to tell what's wrong if you told us what actually happens and how that compares to what you expected.

BTW: Why all the extra braces in that code?

Also, it might be worth noting that everyone here needs code help. If you give a more descriptive title, you might get a lot more views and stand a much better chance of getting the help you need.

Ok, dude. You have two potential problems.

1 - IR not being read

2 - motor not moving like it's supposed to.

What we usually do is add some debugging prints to find out what's going wrong.

In your setup, add

Serial.begin(9600);
while(!Serial);
Serial.println("OK! I am good to begin debugging!")

And check that you see the message on your IDE serial monitor.

Next thing to do is to add a print message to indicate "ok, I have received an IR message".

Having said that, looking at your code I can see the problem - the compiler thinks that the conditional part of that if() statement is simply that variable declaration and nothing else. After conditionally crating a variable and doing nothing with it, it then proceeds to execute the next thing - that block in braces - unconditionally.

To put it another way:

An if statement looks at a condition and then does one thing. Of course, that one thing may be a statement list enclosed in braces. but what you have written:

  int position;
  {
    // some stuff here
  }

is two things. The if() only conditionally executes the first. What you meant was:

  {
    int position;
    // some stuff here
  }

Besides what’s been pointed out, you don’t need both of these:-

pinMode(ledPin, OUTPUT);
pinMode(13, OUTPUT);

Both do the same thing, since ‘ledPin’ holds 13.

This should do what you want. ie When any valid IR code is received, it will move the servo as described.:-

#include <IRremote.h>
#include <Servo.h>  // servo library

const byte RECV_PIN = 11;
const byte ledPin = 13;
const byte servoPin = 9;

IRrecv irrecv(RECV_PIN);
decode_results results;
Servo servo1;

void setup()
{
    pinMode(ledPin, OUTPUT);
    irrecv.enableIRIn(); // Start the receiver
    servo1.attach(servoPin);
}

void loop()
{
    if (irrecv.decode(&results))
    {
        servo1.write(90);
        delay(3000);
        servo1.write(180);
        delay(1000);
        irrecv.resume(); // Receive the next value
    }
}

Keep in mind, though, that using ‘delay()’ stops the program from doing anything else while the servo is doing it’s routine.
You’ll need to use ‘millis()’-based timing to get around this.
For more info, see the “BlinkWithoutDelay” example in the IDE, and Demonstration code for several things at the same time

Thanks that really helps!

wait... It doesn't work. It could be I wired it wrong or something but I doubt it cause it worked with different code

aaronyz123:
wait… It doesn’t work. It could be I wired it wrong or something but I doubt it cause it worked with different code

Hmm. That code should work fine. It’s about as simple as it gets, similar to the “IRrecvDemo” sketch provided with the library. (I don’t have an IR receiver set up at the moment, so I can’t test it myself, but I can’t see anything wrong with the code.)

This is the “IRrecvDemo” sketch, (copied straight from the “Examples” folder).
Try it and see if it works for you. You should see the received code(s) in the serial monitor. (Make sure that you set the serial monitor to the same baud rate as the code, or vice versa.):-

#include <IRremote.h>

int RECV_PIN = 11;

IRrecv irrecv(RECV_PIN);

decode_results results;

void setup()
{
    Serial.begin(9600);
    irrecv.enableIRIn(); // Start the receiver
}

void loop()
{
    if (irrecv.decode(&results))
    {
        Serial.println(results.value, HEX);
        irrecv.resume(); // Receive the next value
    }
    delay(100);
}

I’d say that you have a hardware problem, perhaps your wiring, despite saying this:-

It could be I wired it wrong or something but I doubt it cause it worked with different code

Show the code that works.

Thanks