general questions about arduino programming

I'm a beginner with arduino / arduino coding, got some stuff to play and learn with it, i'm stuck with something.

I wanna run code for a IR Remote to control a servo when i press the (+) button on the remote the servo start going to 180 degree but like one degree each press, like the volume thing on TV

i tried alot of thing with leds, servos from my background knowledge about simple stuff of programming and using some search help

i got almost everything work and fine i understand it

i came up with this

if (results.value == plus)
      {
        angel = angel+1;
        Serial.println (angel);
      }

what's in my mind is to make the angel write to the servo and everytime it get a +1
I decided to make it print so i see what's going on, when i call the function on loop it keep going to infinite if i pressed the button once

and including the func on setup don't recognize anything or run at all

Here's the full code including some led stuff i was trying :slight_smile:

#include <IRremote.h>
#include <Servo.h>
#define on 0xFF6897
#define off 0xFF30CF
#define plus 0xFF906F

Servo servo;
int ir = 11;
int led = 13;
int angel=0;

IRrecv irrecv(ir);

decode_results results;

void setup()
{
  servo.attach(4);
  pinMode(led, OUTPUT);
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
  
  
}

void loop()
{
  if (irrecv.decode(&results))
    {
     Serial.println(results.value, HEX);
     irrecv.resume();
    }
    
    if (results.value == on)
    {
      
      digitalWrite(led, HIGH);
      
    }
    
    if (results.value == off)
    
    {
      
      digitalWrite(led, LOW);
      
    }
    
servo_1();
}

void servo_1()
{
  if (results.value == plus)
      {
        angel = angel+1;
        Serial.println (angel);
      }
}

As long as you don't receive a new IR message, results.value will remain the same, so your function will keep on incrementing the angle.

The solution: only call your servo_1 function if irrecv.decode returns true.

Pieter

PieterP:
As long as you don't receive a new IR message, results.value will remain the same, so your function will keep on incrementing the angle.

The solution: only call your servo_1 function if irrecv.decode returns true.

Pieter

can u help me a little further, what will be the code for irrecv.decode returns true will look like?

You need to move your actions to inside the first if block

void loop()
{
  if (irrecv.decode(&results))
  {
    Serial.println(results.value, HEX);
    irrecv.resume();

    // do ALL your actions here that needs to be done when you detect a IR button press
    ...
    ...
  }

  // nothing here relating to the IR button press
}