Stap 3: De schets
Deze sketch zullen goed gebruik van de instructie "als" maken. Het zal het gebruik van de knoppen 0 tot en met 5 op de afstandsbediening. Wanneer u op een willekeurig getal tussen 1 en 5, het controleert of de LED brandt dan het zal uitzetten of de LED is uitgeschakeld, dan zal het het weer op. Als u op 0 drukt, wordt het alle LEDs uitgeschakeld.
Nummer 1 zal het bepalen van de rode LED.
Nummer 2 zal de blauwe LED controle.
Nummer 3, zullen de oranje LED beheersen.
Nummer 4, zullen de gele LED beheersen.
Nummer 5 zal het bepalen van de groene LED.
Nummer 0 dooft alle de LEDs.
De schets:
Beginnen met het definiëren van de variabelen:
bij de IR ontvanger
int RECV_PIN = 2;
IRrecv irrecv(RECV_PIN);
decode_results resultaten;
voor de LEDs
int RLED = 8;
int BLED = 9;
int OLED = 10;
int YLED = 11;
int GLED = 12;
In de void setup, Start de ontvanger en stelt u de pin-modus voor alle LEDs op uitgang.
VOID Setup {}
irrecv.enableIRIn(); Start de ontvanger
pinMode (RLED, uitvoer); de pincode 8 uitgang instellen
pinMode (BLED, uitvoer); instellen van de pin 9 aan output
pinMode (OLED, uitvoer); instellen van de pin 10 voor de uitvoer van
pinMode (YLED, uitvoer); de pincode 11 uitgang instellen
pinMode (GLED, uitvoer); instellen van de pin 12 aan output}
-----------------------------------------------------------------------------------------------------------
Uit de eerder instructable, ontdekten we de codes voor 0 tot en met 5 en ze zijn:
1 is FF30CF
2 is FF18E7
3 is FF7A85
4 is FF10EF
5 is FF38C7
en 0 is FF6897
-----------------------------------------------------------------------------------------------------------
In de leegte lus beginnen we door te verifiëren als er geen signaal van de afstandsbediening is.
Als de IR één van de bovenstaande codes ontvangt (ik gebruikte de 0 x, omdat ik de hexadecimale code gebruikte) dan zal het ook weer uit of op de LED.
Eerst de code controleert of de LED uitgeschakeld is, als er dan blijkt het op, als het aanstaat wordt het uitgeschakeld het
Tenzij de code FF6897 (0) wordt ontvangen, dan wordt deze uitgeschakeld alle de LEDs
void loop() <br> { if (irrecv.decode(&results)) //this line checks if we received a signal from the IR receiver { if (results.value == 0xFF30CF) //if the result is 1, turn on or off the red LED { if (digitalRead(RLED) == LOW) //if the red LED is off { digitalWrite(RLED, HIGH); //turn it on irrecv.resume(); // wait for another signal from the remote } else {digitalWrite(RLED, LOW);} //if the red LED is on, then turn it off } if (results.value == 0xFF18E7) //if the result is 2, turn onor off the blue LED { if (digitalRead(BLED) == LOW) //if the blue LED is off { digitalWrite(BLED, HIGH); //turn it on irrecv.resume(); } else {digitalWrite(BLED, LOW);} //if the blue LED is on, then turn it off } if (results.value == 0xFF7A85) //if the result is 3, turn on of off the orange LED { if (digitalRead(OLED) == LOW) //if the orange LED is off { digitalWrite(OLED, HIGH); //turn it on irrecv.resume(); } else {digitalWrite(OLED, LOW);} //if the orange LED is on, then turn it off } if (results.value == 0xFF10EF) //if the result is 4, turn on or off the yellow LED { if (digitalRead(YLED) == LOW) //if the yellow LED is off { digitalWrite(YLED, HIGH); //turn it on irrecv.resume(); } else {digitalWrite(YLED, LOW);} //if the yellow LED is on, then turn it off } if (results.value == 0xFF38C7) //if the result is 5, turn on or off the green LED { if (digitalRead(GLED) == LOW) //if the green LED is off { digitalWrite(GLED, HIGH); //turn it on irrecv.resume(); } else {digitalWrite(GLED, LOW);} //if the green LED is on, then turn it off } if (results.value == 0xFF6897) //if the result is 0, turn off all the LEDs { digitalWrite(RLED, LOW); //turn off the red LED digitalWrite(BLED, LOW); //turn off the blue LED digitalWrite(OLED, LOW); //turn off the orange LED digitalWrite(YLED, LOW); //turn off the yellow LED digitalWrite(GLED, LOW); //turn off the green LED } irrecv.resume(); //receive the next value } }