Pages: [1]   Go Down
Author Topic: IR receiver replacement for switch  (Read 772 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 30
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all, this is my first time posting in this forum after being a lurker for a fair few months now. I have searched around like crazy for the answer to this question but have been unable to find it.
I am after a code for my project that simply allows the option of both push button and IR activation depending on how I feel.
The code is currently designed to:
1) Have LED's delay, then flicker and fade on when a button is pressed.
2) Move 2 servos in opposite directions and have the LED's turn off when a button is pressed a second time
3) Move servos in reverse opposite directions and have the LED's delay, then flicker and fade on when the button is pressed a third time.
4) 2 and 3 are then repeated

I'd like to add the option of having the same thing happen when it receives an IR signal. My Arduino skills are absolutely awful and I'm on a very short time span, so have chosen to come to you guys after a lot of time fiddling and trial and error (mainly error).

I'm currently using an IR receiver(http://www.ebay.co.uk/itm/130822013572?ssPageName=STRK:MEWNX:IT&_trksid=p3984.m1497.l2649) that I bought from ebay and it is connected to pin 11 on the Arduino, then to pos and gnd. My TV remote is being used as the sender for now and ALL buttons are showing as 0 in the serial monitor.


This is the code I'm currently using. The serial monitor is recognising the use of IR but I have no idea how to get the rest of the code to act on the input. Thank you in advance for any help I receive.

Code:
#include <Servo.h>
#include <IRremote.h>

Servo myservo; // servo 0
Servo myservo1; // servo 1
int val; // variable for reading the pin status
int val2; // variable for reading the delayed/debounced status
int buttonState;
int pos = 0;
int pos1 = 180;
int servostatus = 0;
int switchPin =2; // Switch connected to digital pin 2
int ledPin = 5;    // led in pin 5
int ledPin2 = 18;  // this isnt here at the moment
int receiver = 11; // pin 1 of IR receiver to Arduino digital pin 11
IRrecv irrecv(receiver); // create instance of 'irrecv'

decode_results results;
void setup() // run once, when the sketch starts
{

myservo.attach(9);
myservo1.attach(10);
pinMode(receiver, INPUT);
pinMode(ledPin, OUTPUT);
buttonState = digitalRead(switchPin);
myservo.write(0);
myservo1.write(175);
pinMode(ledPin2, OUTPUT);
Serial.begin(9600);
irrecv.enableIRIn();
}


void loop() // run over and over again
{
val = digitalRead(switchPin); // read input value and store it in val
delay(10); // 10 milliseconds is a good amount of time
val2 = digitalRead(switchPin); // read the input again to check for bounces
if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    irrecv.resume(); // Receive the next value
  }
if (val == val2) { // make sure we got 2 consistant readings!
if (val != buttonState) { // the button state has changed!
if (val == LOW) { // check if the button is pressed

if (servostatus == 0) { // is the light off?
servostatus = 1; // turn light on!

myservo.write(0);
myservo1.write(180);
delay(1000);
digitalWrite(ledPin, HIGH);
delay(50);
digitalWrite(ledPin, LOW);
delay(00);
digitalWrite(ledPin, HIGH);
delay(50);
digitalWrite(ledPin, LOW);
delay(00);


// fading
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
delay(30);


}

} else {
servostatus = 0; // turn light off!


digitalWrite(ledPin, LOW);
delay(15);
digitalWrite(ledPin2, LOW);
myservo.write(180);
myservo1.write(0);






}
}
}
buttonState = val; // save the new state in our variable
}
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 30
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Anyone?
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 70
Posts: 3727
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Do two checks. One to see if the button is pressed, another seperate one to check if you got anything from the remote. Either check can set a global boolean variable true. Use that variable to decide whether to do the LED flashing and servo moving. Inside the if that controls this, set the variable false so it doesn't run again on the next iteration of loop.

IIRC though, the code that decodes the remote blocks waiting for some input, so you may find that it's not responsive to the switch.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 30
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Do two checks. One to see if the button is pressed, another seperate one to check if you got anything from the remote. Either check can set a global boolean variable true. Use that variable to decide whether to do the LED flashing and servo moving. Inside the if that controls this, set the variable false so it doesn't run again on the next iteration of loop.

IIRC though, the code that decodes the remote blocks waiting for some input, so you may find that it's not responsive to the switch.

I must say I am a complete and total noob when it comes to these things. I had to read your reply multiple times to understand what you meant.

Finally understand smiley

However, I literally have no idea whatsoever how to write that code. Due to my very short time spam I've concentrated on learning exactly what I've needed to for the specific project. I don't have a single clue where to start to even learn how to write the code to that location.

How do I get it to do 2 checks, and what is the second check I need in that location?
Logged

Pages: [1]   Go Up
Jump to: