Advertisement
Jong

ATTiny + IR

Sep 23rd, 2013
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.96 KB | None | 0 0
  1. #define IR_PIN 1
  2. #define LED_PIN 2
  3.  
  4. int state = 0;
  5.  
  6. void setup()
  7. {
  8.   pinMode(IR_PIN, INPUT);
  9.   pinMode(LED_PIN, OUTPUT);
  10.   digitalWrite(LED_PIN, LOW);
  11.   //Serial.begin(9600);
  12. }
  13.  
  14. void loop()
  15. {
  16.   int startPulse, startTime, i;
  17.   startPulse = pulseIn(IR_PIN, LOW);
  18.   //Serial.print("startPulse = ");
  19.   //Serial.println(startPulse);
  20.   if(startPulse < 4500 || startPulse > 5500) //Starting pulse is 5ms
  21.     return;
  22.   startTime = millis();
  23.   for(i = 1; i <= 4; i++)
  24.   {  
  25.     int pulse = pulseIn(IR_PIN, LOW);
  26.     //Serial.print("Pulse #");
  27.     //Serial.print(i);
  28.     //Serial.print(" = ");
  29.     //Serial.println(pulse);
  30.     if(pulse < 800 || pulse > 1200) //Each subsequent pulse is 1ms
  31.       return;    
  32.   }
  33.   //Serial.print("millis() - startTime = ");
  34.   //Serial.println(millis() - startTime);
  35.   if(millis() - startTime < 100)
  36.   {
  37.     state = !state;
  38.     if(state)
  39.       digitalWrite(LED_PIN, HIGH);
  40.     else digitalWrite(LED_PIN, LOW);
  41.   }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement