IRremote sensor shows '0' only on monitor view

Hello. I'm making a RC car for a school project but I when I try to test the sensor out It shows only zeros. Here is the code.

#include <IRremote.h>
#include <IRremoteInt.h>

const int RECV_PIN = 8;


const int SolMotor1 = 5;
const int SolMotor2 = 6;
const int SolMotorEN = 9;
const int SagMotor1 = 7;
const int SagMotor2 = 8;
const int SagMotorEN = 10;

const int echoPin = 11;
const int trigPin = 12;

int Hiz = 255;

IRrecv irrecv(RECV_PIN);
decode_results results;

#define CH1 0xFFA25D
#define CH 0xFF629D
#define CH2 0xFFE21D
#define PREV 0xFF22DD
#define NEXT 0xFF02FD
#define PLAYPAUSE 0xFFC23D
#define VOL1 0xFFE01F
#define VOL2 0xFFA857
#define EQ 0xFF906F
#define BUTON0 0xFF6897 
#define BUTON100 0xFF9867
#define BUTON200 0xFFB04F
#define BUTON1 0xFF30CF
#define BUTON2 0xFF18E7
#define BUTON3 0xFF7A85
#define BUTON4 0xFF10EF
#define BUTON5 0xFF38C7
#define BUTON6 0xFF5AA5
#define BUTON7 0xFF42BD
#define BUTON8 0xFF4AB5
#define BUTON9 0xFF52AD

void setup()
{
  pinMode(SolMotorEN, OUTPUT);
  pinMode(SagMotorEN, OUTPUT);
  pinMode(SolMotor1, OUTPUT);
  pinMode(SolMotor2, OUTPUT);
  pinMode(SagMotor1, OUTPUT);
  pinMode(SagMotor2, OUTPUT);
  
  pinMode(echoPin, INPUT);
  pinMode(trigPin, OUTPUT);

  digitalWrite(SolMotorEN, LOW);
  digitalWrite(SagMotorEN, LOW);
  digitalWrite(SolMotor1, LOW);
  digitalWrite(SagMotor1, LOW);
  digitalWrite(SolMotor2, LOW);
  digitalWrite(SagMotor2, LOW);

  Serial.begin(9600);
  irrecv.enableIRIn();
}

void loop() {
  Serial.println(mesafe());
  if (mesafe() > 10)
  {

    if (irrecv.decode(&results))
    {
      if (Hiz >= 0 && Hiz <= 255 && results.value == VOL1)
      {
        Hiz = Hiz - 50;
        if (Hiz < 0)
        Hiz = 0;
      }
      if (Hiz >= 0 && Hiz <= 255 && results.value == VOL2)
      {
        Hiz = Hiz + 50;
        if (Hiz > 255)
        Hiz = 255;
      }
      if (results.value == BUTON2)
      {
        duzgit();
      }
      if (results.value == BUTON8)
      {
        gerigit();
      }
      if (results.value == BUTON4)
      {
        soladon();
      }
      if (results.value == BUTON6)
      {
        sagadon();
      }
      if (results.value == BUTON0)
      {
        dur();
    }
    irrecv.resume();
    }
  }
  else
  dur();
}

void sol_motor(String dir, int spd)
{
  if ( dir == "ILERI")
  {
    digitalWrite(SolMotor1, HIGH);
    digitalWrite(SolMotor2, LOW);
    analogWrite(SolMotorEN, spd);
  }
  if ( dir == "GERI")
  {
    digitalWrite(SolMotor1, LOW);
    digitalWrite(SolMotor2, HIGH);
    analogWrite(SolMotorEN, spd);
  }
}


void sag_motor(String dir, int spd)
{
  if ( dir == "ILERI")
  {
    digitalWrite(SagMotor1, HIGH);
    digitalWrite(SagMotor2, LOW);
    analogWrite(SagMotorEN, spd);
  }

  if ( dir == "GERI")
  {
    digitalWrite(SagMotor1, LOW);
    digitalWrite(SagMotor2, HIGH);
    analogWrite(SagMotorEN, spd);
  }
}

void duzgit()
{
  sag_motor("ILERI", Hiz);
  sol_motor("ILERI", Hiz);
}
void sagadon()
{
  sag_motor("GERI", Hiz);
  sol_motor("ILERI", Hiz);
}
void gerigit()
{
  sag_motor("GERI", Hiz);
  sol_motor("GERI", Hiz);
}
void soladon()
{
  sag_motor("ILERI", Hiz);
  sol_motor("GERI", Hiz);
}

void dur()
{
  sag_motor("ILERI", 0);
  sol_motor("ILERI", 0);
}


int mesafe()
{
  
const int echoPin = 11;
const int trigPin = 12;
  long duration;
  long int distance;
digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = duration/58,2;
  delay(50);
  if (distance >= 50)
    return 50;
  return distance;
}

Try reading the results of this object call (below) to help find the cause.

irrecv.decode(&results)

I believe you might have a library that wants another way of calling the object... try...

irrecv.decode()

Where do I type those

All the code I showed is from your sketch.

check if your IR circuit is working.

if you run this code (with everything else connected) and press the remote buttons, do you see the right info printed in the Serial monitor (set at 115200 Bauds)?

#include <IRremote.h>
#include <IRremoteInt.h>

const int RECV_PIN = 8;


const int SolMotor1 = 5;
const int SolMotor2 = 6;
const int SolMotorEN = 9;
const int SagMotor1 = 7;
const int SagMotor2 = 8;
const int SagMotorEN = 10;

const int echoPin = 11;
const int trigPin = 12;

IRrecv irrecv(RECV_PIN);
decode_results results;

#define CH1 0xFFA25D
#define CH 0xFF629D
#define CH2 0xFFE21D
#define PREV 0xFF22DD
#define NEXT 0xFF02FD
#define PLAYPAUSE 0xFFC23D
#define VOL1 0xFFE01F
#define VOL2 0xFFA857
#define EQ 0xFF906F
#define BUTON0 0xFF6897
#define BUTON100 0xFF9867
#define BUTON200 0xFFB04F
#define BUTON1 0xFF30CF
#define BUTON2 0xFF18E7
#define BUTON3 0xFF7A85
#define BUTON4 0xFF10EF
#define BUTON5 0xFF38C7
#define BUTON6 0xFF5AA5
#define BUTON7 0xFF42BD
#define BUTON8 0xFF4AB5
#define BUTON9 0xFF52AD

void setup()
{
  pinMode(SolMotorEN, OUTPUT);
  pinMode(SagMotorEN, OUTPUT);
  pinMode(SolMotor1, OUTPUT);
  pinMode(SolMotor2, OUTPUT);
  pinMode(SagMotor1, OUTPUT);
  pinMode(SagMotor2, OUTPUT);
  
  pinMode(echoPin, INPUT);
  pinMode(trigPin, OUTPUT);

  digitalWrite(SolMotorEN, LOW);
  digitalWrite(SagMotorEN, LOW);
  digitalWrite(SolMotor1, LOW);
  digitalWrite(SagMotor1, LOW);
  digitalWrite(SolMotor2, LOW);
  digitalWrite(SagMotor2, LOW);

  Serial.begin(9600);
  irrecv.enableIRIn();
}

void loop() {
  if (irrecv.decode(&results)) {
    switch (results.value) {
      case CH1:       Serial.println("CH1");        break;
      case CH:        Serial.println("CH");         break;
      case CH2:       Serial.println("CH2");        break;
      case PREV:      Serial.println("PREV");       break;
      case NEXT:      Serial.println("NEXT");       break;
      case PLAYPAUSE: Serial.println("PLAYPAUSE");  break;
      case VOL1:      Serial.println("VOL1");       break;
      case VOL2:      Serial.println("VOL2");       break;
      case EQ:        Serial.println("EQ");         break;
      case BUTON0:    Serial.println("BUTON0");     break;
      case BUTON100:  Serial.println("BUTON100");   break;
      case BUTON200:  Serial.println("BUTON200");   break;
      case BUTON1:    Serial.println("BUTON1");     break;
      case BUTON2:    Serial.println("BUTON2");     break;
      case BUTON3:    Serial.println("BUTON3");     break;
      case BUTON4:    Serial.println("BUTON4");     break;
      case BUTON5:    Serial.println("BUTON5");     break;
      case BUTON6:    Serial.println("BUTON6");     break;
      case BUTON7:    Serial.println("BUTON7");     break;
      case BUTON8:    Serial.println("BUTON8");     break;
      case BUTON9:    Serial.println("BUTON9");     break;
      default:        Serial.println("Unknown command");
    }
    irrecv.resume();
  }
}

(typed here from your code and untested, so mind typos)

BTW - I see already a conflict here

const int RECV_PIN = 8;
const int SagMotor2 = 8;

➜ what's connected to pin 8 ?


Note that you probably copied an old code for the IRLibrary and this is outdated. You should look at the newer examples on how to handle the remote.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.