#include "Adafruit_LEDBackpack.h" #include <IRremote.h> int servoPin=12; int button = 0; int Gas=0; int GasFlag=0; int SevoFlag=0;
Adafruit_7segment led_display1 =Adafruit_7segment(); int mapCodeToButton(unsigned long code) {
if ((code & 0x0000FFFF) == 0x0000BF00) { code >>= 16; if (((code >> 8) ^ (code & 0x00FF)) == 0x00FF) { return code & 0xFF; } } return -1; }
int readInfrared() { int result = -1; if (IrReceiver.decode()) { unsigned long code = IrReceiver.decodedIRData.decodedRawData; result = mapCodeToButton(code); IrReceiver.resume(); } return result; }
void setup() { // Initialize the NeoPixellibrary. led_display1.begin(112); IrReceiver.begin(2); led_display1.println("----"); led_display1.writeDisplay(); Serial.begin(9600); pinMode(A0,INPUT); pinMode(servoPin,OUTPUT); pinMode(4,OUTPUT); pinMode(5,OUTPUT);
}
void loop() { if(SevoFlag==1) { digitalWrite(servoPin,HIGH); delayMicroseconds(1500); digitalWrite(servoPin,LOW); delay(20); SevoFlag=1; } if(GasFlag==1) { digitalWrite(servoPin,HIGH); delayMicroseconds(0); digitalWrite(servoPin,LOW); digitalWrite(5,LOW); digitalWrite(4,HIGH); Gas = analogRead(A0); Serial.print("Gas: "); Serial.println(Gas); GasFlag=1; } button = readInfrared(); if (button >= 0) { led_display1.println(button); led_display1.writeDisplay(); } delay(10); switch(button) { case 16: SevoFlag=1; break; case 17: SevoFlag=0; GasFlag=1; break; case 18: SevoFlag=0; GasFlag=0; digitalWrite(4,LOW); digitalWrite(5,HIGH); Serial.println("Gas:NO WORK "); break; case 20: Serial.println("你的學號"); break; // default: } }
|