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:
- Have LED's delay, then flicker and fade on when a button is pressed.
- Move 2 servos in opposite directions and have the LED's turn off when a button is pressed a second time
- 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.
- 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.
#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
}
}