lol im really, really going to try and follow that, ill post it when its done with that, but here is the code as it stands right now, I really wanna learn how to use some timer functions, for example something to check elapsed time, so that when I do animations they actually, ya know, work... ah well it will make sense in the code...
Mind you, alot of this isnt my code, and its VERY sloppy, this is the first write
Code:#define icpPin 8 // this interrupt handler must use pin 8
#define TICKS_PER_uS 2 // number of timer ticks per microsecond
#define NUMBER_PULSES 13 // number of pulses in on frame of sony remote
#define START_PULSE_LEN (2400 * TICKS_PER_uS) // start pulse is 2.4ms long (note clock counts in 0.5 us ticks)
int calibrationTime = 10;
long unsigned int lowIn;
long unsigned int pause = 5000;
boolean lockLow = true;
boolean takeLowTime;
int pirPin = 3;
int motionLED = 11;
int ir_pin = 2;
int powerLED = 12;
int outputLEDS[3];
int debug = 0;
int start_bit = 2000;
int bin_1 = 1000;
int bin_0 = 400;
int LEDAnimation = 0;
boolean playAnimation = false;
int animationStep = 0;
int OutputLED = 12;
boolean Calibrated;
boolean CheckSensor;
volatile unsigned int Pulses[ NUMBER_PULSES+ 1];
volatile uint8_t PulseCount;
ISR(TIMER1_CAPT_vect){
if(! bit_is_set(TCCR1B ,ICES1)){
TCNT1 = 0;
}
else {
if(ICR1 >= START_PULSE_LEN)
PulseCount = 0;
if(PulseCount < NUMBER_PULSES) {
Pulses[++PulseCount] = ICR1 / TICKS_PER_uS;
}
}
TCCR1B ^= _BV(ICES1);
}
void setup(){
Calibrated = false;
CheckSensor = false;
outputLEDS[0] = 10;
outputLEDS[1] = 9;
outputLEDS[2] = 7;
Serial.begin(9600);
pinMode(pirPin, INPUT);
pinMode(powerLED, OUTPUT);
pinMode(motionLED, OUTPUT);
pinMode(outputLEDS[0], OUTPUT);
pinMode(outputLEDS[1], OUTPUT);
pinMode(outputLEDS[2], OUTPUT);
pinMode(ir_pin, INPUT);
digitalWrite(powerLED, HIGH); //not ready yet
digitalWrite(motionLED, LOW);
digitalWrite(outputLEDS[0], LOW);
digitalWrite(outputLEDS[1], LOW);
digitalWrite(outputLEDS[2], LOW);
pinMode(icpPin,INPUT);
PulseCount = 0;
TCCR1A = 0x00;
TCCR1B = 0x02;
TIMSK1 = _BV(ICIE1);
delay(50);
}
int DecodeFrame() {
int result;
cli();
if(debug == 1) {
Serial.println("-----");
}
for(int i=1;i<NUMBER_PULSES;i++) {
if (debug == 1) {
Serial.print(Pulses[i]);
}
if(Pulses[i] > bin_1) {
Pulses[i] = 1;
} else {
if(Pulses[i] > bin_0) {
Pulses[i] = 0;
} else {
Pulses[i] = 2;
}
}
}
for(int i=1;i<NUMBER_PULSES;i++) {
if(Pulses[i] > 1) {
return -1;
}
}
result = 0;
int seed = 1;
for(int i=1;i<NUMBER_PULSES;i++) {
if(Pulses[i] == 1) {
result += seed;
}
seed = seed * 2;
}
PulseCount = 0;
sei();
return result;
}
void loop(){
int value;
if(PulseCount >= NUMBER_PULSES ){
value = DecodeFrame() ;
Serial.print(value);
}
if(value == 43)
{
CheckSensor = !CheckSensor;
if(CheckSensor)
{
Serial.println("Powering on...");
digitalWrite(powerLED, HIGH);
}
else
{
Serial.println("Powering down...");
digitalWrite(powerLED, HIGH);
digitalWrite(motionLED, LOW);
playAnimation = false;
Calibrated = false;
}
}
if(value == 2659)
{
LEDAnimation ++;
if(LEDAnimation > 2)
{
LEDAnimation = 0;
}
}
if(CheckSensor == true)
{
if(Calibrated == true)
{
digitalWrite(motionLED, HIGH);
CheckPIR();
}
else
{
CalibrateSensor();
Serial.println("Calibration Complete, starting program...");
digitalWrite(powerLED, LOW);
Calibrated = true;
}
}
else
{
Calibrated = false;
digitalWrite(motionLED, LOW);
}
delay(100);
}
void CheckPIR()
{
if(digitalRead(pirPin) == HIGH){
//TURN ON LEDS
if(lockLow){
lockLow = false;
delay(50);
}
takeLowTime = true;
}
if(digitalRead(pirPin) == LOW){
if(takeLowTime){
lowIn = millis(); //save the time of the transition from high to LOW
takeLowTime = false; //make sure this is only done at the start of a LOW phase
}
if(!lockLow && millis() - lowIn > pause){
lockLow = true;
//TURN OFF LEDS
delay(50);
}
}
return;
}