Going insane about a mistake i cant find

Never used an IR receiver before in a project but everything seems to be working fine until i combine it with a motor, and once it reads the serial monitor it keeps spinning and printing the words forever :,( just want to make a curtain closer.

#include <IRremote.h>

IRrecv rec(9);
int bwrd = 12;
int frwd = 13;


void setup() {
  // put your setup code here, to run once:
  rec.enableIRIn();
  pinMode(bwrd, OUTPUT);
  pinMode(frwd, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (rec.decode()) {
    Serial.println(rec.decodedIRData.decodedRawData, HEX);
    delay(1500);
  }
if (rec.decodedIRData.decodedRawData==0xF30CFF0) {
    digitalWrite(frwd, HIGH);
    delay(200);
    Serial.println("stahp!!");
    digitalWrite(frwd, LOW);
}

if (rec.decodedIRData.decodedRawData==0xE718FF00) {
    digitalWrite(bwrd, HIGH);
    delay(200);
    Serial.println("wait!!");
    digitalWrite(bwrd, LOW);}
rec.resume();
}

I also know like nothing about programming or electronics beyond college basics.

What kind of motor? How is it powered? How is it controlled?

Just says 3-6 volt motor on my starter kit but I'm using a l293d to operate it with an external 5v power supply.

Does this ever get cleared out or does it retain its value?

i have no idea i think it retains its value until it resumes at the end which makes it start the search again but i could be entirely wrong ive just seen a few tutorials.

It's a class variable that would need to have something done to it to change its value.

Can we take you at you word on this

everything seems to be working fine

but I would prefer testing that would allow you to say "it works perfectly doing what I write the code to do...". You may need to add to your sketch to prove it.

a7

ok so take the motors out of the equation for now and just read the IR. Also remove the delays. There is a better non-blocking way to add a delay. There is an example sketch called Blink Without Delay.

the IR sensor works and reads values on its own perfectly fine and starts the motor in the right direction ive programmed it to it just doesn't stop lol

then once you want the motors to stop, you need a way to clear rec.decodedIRData.decodedRawData. There should be a way to do that, otherwise you will need a third button to stop the motors yourself.

@htodd

Just a few misplaced braces, try

void loop() {
  // put your main code here, to run repeatedly:
  if (rec.decode()) {
    Serial.println(rec.decodedIRData.decodedRawData, HEX);
    delay(1500);

    if (rec.decodedIRData.decodedRawData == 0xF30CFF0) {
      digitalWrite(frwd, HIGH);
      delay(200);
      Serial.println("stahp!!");
      digitalWrite(frwd, LOW);
    }

    if (rec.decodedIRData.decodedRawData == 0xE718FF00) {
      digitalWrite(bwrd, HIGH);
      delay(200);
      Serial.println("wait!!");
      digitalWrite(bwrd, LOW);
    }
  }
  rec.resume();
}

that worked thank you !!! ill be a lazy fellow closing and opening my curtains with a remote control in no time.

Yeah, thinking about it, any button on the remote other than the two coded, should have stopped the motor.

:grin:

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