Go Down

Topic: blink led with remote (Read 3120 times) previous topic - next topic

Henrywallace

This is the first code ive wrote all by myself and it seems to be working but not very good.  The led seems to only turn on when i hit a button on the remote like 50 percent of the time. The rest of the time i can see that my ir sensors build in light is blinking when i hit a button but nothing happens. I wasnt sure if i have to declare the receive pin as an input or not. I also wasnt sure if i had to declare my variables and include my libraries in the setup() statement or before it. Im using a attiny 85 processor and not sure what pins can be used for what tasks. I know they all are capable digital read/write, and only some are capable of analog read and write. If anyone has any ideas to make this code better and run more smoothly please let me know.

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

int RECV_PIN = 3;
int RELAY_PIN = 2;
int LED_PIN = 1;
int BLINK_PIN = 0;

bool on = 0;

IRrecv irrecv(RECV_PIN);
decode_results results;

void setup()
{
 pinMode(RELAY_PIN, OUTPUT);
 pinMode(LED_PIN, OUTPUT);
 pinMode(BLINK_PIN, OUTPUT);
 pinMode(RECV_PIN, INPUT);
 irrecv.enableIRIn();

 digitalWrite(RELAY_PIN, LOW);
 digitalWrite(LED_PIN, LOW);
 digitalWrite(BLINK_PIN, LOW);
}

void loop() {

digitalWrite(BLINK_PIN, HIGH);
delay(250);
digitalWrite(BLINK_PIN, LOW);
delay(250);

 if (irrecv.decode(&results)) {
     on = !on;
     digitalWrite(RELAY_PIN, on ? HIGH : LOW);
     digitalWrite(LED_PIN, on ? HIGH : LOW);    
   irrecv.resume();
 }
}

AWOL

#1
Aug 17, 2017, 04:49 pm Last Edit: Aug 17, 2017, 04:50 pm by AWOL
There's a guy here (currently enjoying a brief forum timeout for childish behaviour) with a very similar problem, and a very identical IP address.

Care to comment?

Please remember to use code tags when posting code.

Henrywallace

There's a guy here (currently enjoying a brief forum timeout for childish behaviour) with a very similar problem, and a very identical IP address.

Care to comment?

Please remember to use code tags when posting code.
I just read the thread you posted and couldn't find anything useful. My code is working but i just think there might be better way to code it so that it would be more consistent. Like i said its only working when i hit a button sometimes, and the rest of the time it seems like its not sending a signal from the ir sensor to the microprocessor.

AWOL

Quote
Like i said its only working when i hit a button sometimes, and the rest of the time it seems like its not sending a signal from the ir sensor to the microprocessor.
I'd get rid of the delays()s

Henrywallace

I'd get rid of the delays()s
Im at work right now so i cant test it out. Wouldnt that cause my BLINK_LED to flash so fast that it would just look like a flicker? Are you saying to get rid of the delay because it causing some sort of blank moment for 250 milliseconds where the microprocessor cant get signal from the ir sensor? Do you have any comment on me defining RECV_PIN as an input? I looked at some examples and they never define a pinMode() for the Ir recieve pin.

larryd

No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

AWOL

#6
Aug 17, 2017, 05:37 pm Last Edit: Aug 17, 2017, 05:38 pm by AWOL
No, it would cause the LED to blink so fast, it just looks like it is dim.
I would assume any library using a pin would set the pinMode itself, but I haven't used the IR library for a very long time.
You could check the documentation of the library to see if it is compatible with the processor you're using (timers etc).

I'd also edit my original post, to use code tags.

Henrywallace

Very useful. I think ive implemented this correctly but dont have the IDE here at work to compile it. It probably does set the pinmode automatically. Ill remove it because the example code from the library didnt have it. The only question i have now is if i should use the delay without timer in the part at the bottom where it decodes the signal? When i click a button on my ir remote, my ir sensor blinks really fast. Is there a way to add like a pause between clicks so that its only picking up me pressing the button once?
Code: [Select]
#include <IRremote.h>
#include <IRremoteInt.h>
 
int RECV_PIN = 3;
int RELAY_PIN = 2;
int LED_PIN = 1;
int BLINK_PIN = 0;

bool SIGNAL_ON = 0;
bool LED_ON = 0;

unsigned long currentMillis = millis();
unsigned long previousMillis = 0;
 
IRrecv irrecv(RECV_PIN);
decode_results results;
 
void setup()
{
  pinMode(RELAY_PIN, OUTPUT);
  pinMode(LED_PIN, OUTPUT);
  pinMode(BLINK_PIN, OUTPUT);
  irrecv.enableIRIn();

  digitalWrite(RELAY_PIN, LOW);
  digitalWrite(LED_PIN, LOW);
  digitalWrite(BLINK_PIN, LOW);
}
 
void loop() {

 if (currentMillis - previousMillis >= 250) {

previousMillis = currentMillis;
LED_ON = !LED_ON;
digitalWrite(BLINK_PIN, LED_ON);

}

  if (irrecv.decode(&results)) {
      SIGNAL_ON = !SIGNAL_ON;
      digitalWrite(RELAY_PIN, on ? HIGH : LOW);
      digitalWrite(LED_PIN, on ? HIGH : LOW);     
    irrecv.resume();
  }

}

AWOL

Quote
my ir sensor blinks really fast.
Are you sure you've wired it correctly?

AWOL

Code: [Select]
void loop() {

 if (currentMillis
I think you forgot something.

PaulS

Code: [Select]
      SIGNAL_ON = !SIGNAL_ON;
By convention, all capital letter names are reserved for constants. Constants do not belong on the left side of an equal sign (except when declared, to provide the constant value).

Henrywallace

Are you sure you've wired it correctly?
Yes and no. On my ir sensor I have my 5v hooked up to 5v on my breadboard, ground hooked up to ground on my breadboard, and out connected to pin 0 on my microprocessor. Im just not sure if pin 0 can be used for the input that its using. I looked it up and im pretty sure the ir sensor sends digital information, not analog. All the pins on my attiny 85 are capable or digital read and write.  So yes i believe that ive wired it correctly.

AWOL i looked and dont see what i forgot. That code came straight from link the other guy posted. Maybe its something simple but i dont see it, sorry about that.

PaulS im also having trouble understanding what youre saying. Are you saying all capitals is wrong and wont work, or it will work but not typical coding context?

Btw thank you all for your help so far. Im hoping when i get off ill be able to put new code on my microprocessor and test it out.

AWOL

Quote
That code came straight from link the other guy posted.
No, it didn't.

Henrywallace

No, it didn't.
The line that contains

if (currentMillis - previousMillis >= interval) {

I copy and pasted that line straight from the blink without delay webpage. Im not trying to argue, i honestly just dont see what i forgot.

AWOL

I didn't highlight just that line.
I did that for a reason.

Go Up