Infrared DC Motor Control with Two Limit Switch Problem

Hi,

I check the TV lift using infrared signals with DC motors. When the control signal from the bottom and moving up or down depending on the state of the overlying limit switches. I am also sending a signal to open while moving up to the TV. TV's remote control when I press the power button repeatedly to produce two different codes for both code "or" I have written form. When I press program all power is working correctly, but when it comes to the different signals from the remote control (volume up and down, changing channels, etc.) hangs. I think I'm doing a loop error.
Furthermore, when the motor is in motion again when I send it to the switch control signal stopped moving again in the opposite direction.

Please help me.

My code is as follows:

#include <IRremote.h>

int RECV_PIN = 11;
int MOVE_UP = 8;
int MOVE_DOWN = 9;
int TOP_SW = 6;
int BOTTOM_SW = 7;

IRrecv irrecv(RECV_PIN);
IRsend irsend;
decode_results results;

void setup()

{

irrecv.enableIRIn();

pinMode(MOVE_DOWN,OUTPUT);
pinMode(MOVE_UP,OUTPUT);
pinMode(TOP_SW,INPUT);
pinMode(BOTTOM_SW,INPUT);

Serial.begin(9600);
}

void loop() {

if ((irrecv.decode(&results)) && ((results.value == 0x84C) || (results.value == 0x4C)) && digitalRead(TOP_SW) == HIGH ) {
digitalWrite(MOVE_DOWN, HIGH);
digitalWrite(MOVE_UP, LOW);
Serial.println("MOVE_DOWN");
irrecv.resume();

}

if ((irrecv.decode(&results)) && ((results.value == 0x84C) || (results.value == 0x4C)) && digitalRead(BOTTOM_SW) == HIGH ) {
digitalWrite(MOVE_UP, HIGH);
digitalWrite(MOVE_DOWN, LOW);
Serial.println("MOVE_UP");
sender();
irrecv.resume();

}

if ( digitalRead(BOTTOM_SW) == HIGH && digitalRead(MOVE_DOWN) == HIGH) {
digitalWrite(MOVE_UP, LOW);
digitalWrite(MOVE_DOWN, LOW);
Serial.println("BOTTOM_STOP");
irrecv.resume();

}

if ( digitalRead(TOP_SW) == HIGH && digitalRead(MOVE_UP) == HIGH) {
digitalWrite(MOVE_UP, LOW);
digitalWrite(MOVE_DOWN, LOW);
Serial.println("TOP_STOP");
irrecv.resume();

}

}

void sender()
{
irsend.sendRC5(0x84C, 12);
Serial.println("TV_ON");
delay(100);
setup();

}

Get irrecv.decode(&results) outside of your if tests - you call it too many times

You need to first receive the IR commands

if ( irrecv.decode(&results)) {
// do your tests here for the IR commands without calling decode Again
...
irrecv.resume(); 
}

// do your end switch detection here so that they happen all the time

Basically if IR key pressed then decide returned true then you know you have something to play with and you can do all your if statements with the results data

Don't forget to do the irrecv.resume(); at the end of that big first if

Is that posted twice?

Thank you for your interest.
I tried as you said, but I do not get results.
I could not figure out exactly where in the code change.

I tried as you said, but I do not get results.
I could not figure out exactly where in the code change.

so what did you try really?

post the code from your trial.

I think I'm doing something wrong.
I'm not very experienced in this field.
I have edited the code as follows.
now works complex

help please.

#include <IRremote.h>

int RECV_PIN = 11;
int MOVE_UP = 8;
int MOVE_DOWN = 9;
int TOP_SW = 6;
int BOTTOM_SW = 7;

IRrecv irrecv(RECV_PIN);
IRsend irsend;
decode_results results;

void setup()

{

irrecv.enableIRIn();

pinMode(MOVE_DOWN,OUTPUT);
pinMode(MOVE_UP,OUTPUT);
pinMode(TOP_SW,INPUT);
pinMode(BOTTOM_SW,INPUT);

Serial.begin(9600);
}

void loop() {

decode_results results;

if (irrecv.decode(&results)) {

if ((results.value == 0x84C) || (results.value == 0x4C) && digitalRead(TOP_SW) == HIGH)
digitalWrite(MOVE_DOWN, HIGH);
digitalWrite(MOVE_UP, LOW);
Serial.println("MOVE_DOWN");
irrecv.resume();

if ((results.value == 0x84C) || (results.value == 0x4C) && digitalRead(BOTTOM_SW) == HIGH ) {
digitalWrite(MOVE_UP, HIGH);
digitalWrite(MOVE_DOWN, LOW);
Serial.println("MOVE_UP");
//sender();
irrecv.resume();

}

if ( digitalRead(TOP_SW) == HIGH && digitalRead(MOVE_UP) == HIGH) {

digitalWrite(MOVE_UP, LOW);
digitalWrite(MOVE_DOWN, LOW);
Serial.println("TOP_STOP");

}

if ( digitalRead(BOTTOM_SW) == HIGH && digitalRead(MOVE_DOWN) == HIGH) {

digitalWrite(MOVE_UP, LOW);
digitalWrite(MOVE_DOWN, LOW);
Serial.println("BOTTOM_STOP");

}

}

}

void sender()
{
irsend.sendRC5(0x84C, 12);
Serial.println("TV_ON");
delay(100);
setup();

}

use code tags - can't read that

your code should look like this in the forum

code here

does this even compile? are you closing all } ? clean up a bit the way it looks...

sorry

I use the forum for the first time.

I am sending code

#include <IRremote.h>

int RECV_PIN = 11;
int MOVE_UP = 8;
int MOVE_DOWN = 9;
int TOP_SW = 6;
int BOTTOM_SW = 7;

IRrecv irrecv(RECV_PIN);
IRsend irsend;
decode_results results;

void setup()

{

irrecv.enableIRIn();

pinMode(MOVE_DOWN,OUTPUT);
pinMode(MOVE_UP,OUTPUT);
pinMode(TOP_SW,INPUT);
pinMode(BOTTOM_SW,INPUT);

  Serial.begin(9600);
}

void loop() {

   decode_results results;

     if (irrecv.decode(&results)) { 
  
if ((results.value == 0x84C) || (results.value == 0x4C) && digitalRead(TOP_SW) == HIGH)   
            digitalWrite(MOVE_DOWN, HIGH);
            digitalWrite(MOVE_UP, LOW);
            Serial.println("MOVE_DOWN");
          irrecv.resume(); 

            
      
if ((results.value == 0x84C) || (results.value == 0x4C) && digitalRead(BOTTOM_SW) == HIGH ) { 
            digitalWrite(MOVE_UP, HIGH); 
            digitalWrite(MOVE_DOWN, LOW);
            Serial.println("MOVE_UP");
            //sender();     
                  irrecv.resume();       
  
}

  if ( digitalRead(TOP_SW) == HIGH && digitalRead(MOVE_UP) == HIGH) {
  
            digitalWrite(MOVE_UP, LOW); 
            digitalWrite(MOVE_DOWN, LOW);
            Serial.println("TOP_STOP");
     
 
  }

           if ( digitalRead(BOTTOM_SW) == HIGH && digitalRead(MOVE_DOWN) == HIGH) {
  
            digitalWrite(MOVE_UP, LOW); 
            digitalWrite(MOVE_DOWN, LOW);
            Serial.println("BOTTOM_STOP");
    
            
    
  
} 

     }
   
        
}



void sender()
{
irsend.sendRC5(0x84C, 12);          
   Serial.println("TV_ON");
   delay(100);
    setup();

}

Indent your code properly and you'll see that one of your if's in loop() is different to the others,
which is highly suggestive.

Thank you
but so I'm new to arduino.
Waiting for your help for my error in my code.

You have my help - now use it. Indent your code correctly and its obvious.

Select the code in question, then Tools > Auto-format

The first way to use my code
I made auto format. The Code 0x84c or 0x4c signals get working correctly. However, it is a different signal (eg volume up, down, etc.) it hangs.
Only when I mentioned it signals the code is working correctly.

Also, one of move_up MOVE_DOWN pin is activated when the signal came back (0x84c or 0x4c) and one of the switches is moving in the opposite direction is active.

#include <IRremote.h>

int RECV_PIN = 11;
int MOVE_UP = 8;
int MOVE_DOWN = 9;
int TOP_SW = 6;
int BOTTOM_SW = 7;

IRrecv irrecv(RECV_PIN);
IRsend irsend;
decode_results results;

void setup()

{

  irrecv.enableIRIn();

  pinMode(MOVE_DOWN, OUTPUT);
  pinMode(MOVE_UP, OUTPUT);
  pinMode(TOP_SW, INPUT);
  pinMode(BOTTOM_SW, INPUT);

  Serial.begin(9600);
}

void loop() {



  if ((irrecv.decode(&results)) && ((results.value == 0x84C) || (results.value == 0x4C)) && digitalRead(TOP_SW) == HIGH ) {
    digitalWrite(MOVE_DOWN, HIGH);
    digitalWrite(MOVE_UP, LOW);
    Serial.println("MOVE_DOWN");
    irrecv.resume();

  }

  if ((irrecv.decode(&results)) && ((results.value == 0x84C) || (results.value == 0x4C)) && digitalRead(BOTTOM_SW) == HIGH ) {
    digitalWrite(MOVE_UP, HIGH);
    digitalWrite(MOVE_DOWN, LOW);
    Serial.println("MOVE_UP");
    sender();
    irrecv.resume();

  }

  if ( digitalRead(BOTTOM_SW) == HIGH && digitalRead(MOVE_DOWN) == HIGH) {
    digitalWrite(MOVE_UP, LOW);
    digitalWrite(MOVE_DOWN, LOW);
    Serial.println("BOTTOM_STOP");
    irrecv.resume();

  }

  if ( digitalRead(TOP_SW) == HIGH && digitalRead(MOVE_UP) == HIGH) {
    digitalWrite(MOVE_UP, LOW);
    digitalWrite(MOVE_DOWN, LOW);
    Serial.println("TOP_STOP");
    irrecv.resume();

  }


}


void sender()
{
  irsend.sendRC5(0x84C, 12);
  Serial.println("TV_ON");
  delay(100);
  setup();

}

why are you re-posting the old code for which we already provided comments of what was wrong?

cf my answer #1

Hi,

Kodu bu şekilde düzenledim. Sorunsuz çalışıyor.
Yardımlarınız için teşekkürler.

#include <IRremote.h>

int RECV_PIN = 11;
int MOVE_UP = 8;
int MOVE_DOWN = 9;
int TOP_SW = 6;
int BOTTOM_SW = 7;

IRrecv irrecv(RECV_PIN);
IRsend irsend;
decode_results results;

void setup()

{

  irrecv.enableIRIn();

  pinMode(MOVE_DOWN, OUTPUT);
  pinMode(MOVE_UP, OUTPUT);
  pinMode(TOP_SW, INPUT);
  pinMode(BOTTOM_SW, INPUT);

  Serial.begin(9600);
}

void loop() {

  if ( digitalRead(TOP_SW) == HIGH && digitalRead(MOVE_UP) == HIGH) {

    digitalWrite(MOVE_UP, LOW);
    digitalWrite(MOVE_DOWN, LOW);
    Serial.println("TOP_STOP");


  }

  if ( digitalRead(BOTTOM_SW) == HIGH && digitalRead(MOVE_DOWN) == HIGH) {

    digitalWrite(MOVE_UP, LOW);
    digitalWrite(MOVE_DOWN, LOW);
    Serial.println("BOTTOM_STOP");


  }

  if (irrecv.decode(&results)) {

    if ((irrecv.decode(&results)) && ((results.value == 0x84C) || (results.value == 0x4C)) && digitalRead(TOP_SW) == HIGH ) {
      digitalWrite(MOVE_DOWN, HIGH);
      digitalWrite(MOVE_UP, LOW);
      Serial.println("MOVE_DOWN");


    }

    if ((irrecv.decode(&results)) && ((results.value == 0x84C) || (results.value == 0x4C)) && digitalRead(BOTTOM_SW) == HIGH ) {
      digitalWrite(MOVE_UP, HIGH);
      digitalWrite(MOVE_DOWN, LOW);
      Serial.println("MOVE_UP");
      sender();


    }


    irrecv.resume();
  }

}




void sender()
{
  irsend.sendRC5(0x84C, 12);
  Serial.println("TV_ON");
  delay(100);
  setup();

}

(I don't speak Turkish - it's an English forum please stick to English)

Making some progress

if ([color=red]irrecv.decode(&results)[/color]) {
    if (([color=red]irrecv.decode(&results))[/color] && (([color=orange]results.value == 0x84C[/color]) [color=red]||[/color] ([color=orange]results.value == 0x4C)[/color]) && digitalRead(TOP_SW) == HIGH ) {

Notice 2 weird things?

I'm sorry, I accidentally wrote Turkish.
I edit the code I sent the previous one. Smooth running.
Thank you for your help.