1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
#define rLedPin 6 #define gLedPin 5 #define bLedPin 3
#define irSensorPin 8 #define lightSensorPin A0
int ledR=0; int ledG=0; int ledB=0;
bool irReading; int lightRedaing; bool onOffState;
unsigned long previousIrMillis; unsigned long previousLightMillis; int irCheckInterval=500; int irCheckInterval=1000;
int colorIndex; int colorChangeDelay=1;
void setup(){ pinMode(rLedPin,OUTPUT); pinMode(gLedPin,OUTPUT); pinMode(bLedPin,OUTPUT); pinMode(irSensorPin,INPUT); Serial.begin(9600); Serial.println("Welcome!"); Serial.println("===System Start Sensor Cheak==="); irReading=digitalRead(irSensorPin); lightRedaing=anologRead(lightSensorPin); Serial.println("===Checking Sensor==="); Serial.print("irReading");Serial.println(irReading); Serial.print("lightRedaing");Serial.println(lightRedaing); Serial.println("======================"); }
void loop(){ unsigned long currentMillis=millis(); irCheak(currentMillis); lightCheak(currentMillis); if(irReading==HIGH){ irCheckInterval=30000; }else{ irCheckInterval=500; } if(irReading==HIGH&&lightRedaing>=880){ if(onOffState==0) fadeOn(); onOffState=1; if(colorIndex<=1535){ colorIndex++; }else if(colorIndex>1535){ colorIndex=0; } ledShowColor(colorIndex) }else{ if(onOffState==1) fadeOff(); onOffState=0; } }
void irCheak(unsigned long currentMillis){ if(currentMillis-previousIrMillis)>=irCheakInterval){ irReading=digitalRead(irSensorPin);
Serial.println("===Checking IR Sensor==="); Serial.print("irReading=");Serial.println(irReading); Serial.print("currentMillis");Serial.println(currentMillis); Serial.print("previousIrMillis");Serial.println(previousIrMillis); Serial.println("========================="); previousIrMillis=currentMillis; } }
void lightCheak(unsigned long currentMillis){ if(currentMillis-previousLightMillis)>=lightCheakInterval){ lightRedaing=anologRead(lightSensorPin); Serial.println("===Checking Light Sensor==="); Serial.print("lightReading=");Serial.println(lightRedaing); Serial.print("currentMillis");Serial.println(currentMillis); Serial.print("previousLightMillis");Serial.println(previousLightMillis); Serial.println("==========================="); previousLightMillis=currentMillis; } }
void ledShowColor(int colorIndex){ if(colorIndex>=0&&colorIndex<=255){ ledR=255-colorIndex; anologWrite(rLedPin,ledR); }else if(colorIndex>=256&&colorIndex<=511){ ledR=colorIndex-256; anologWrite(rLedPin,ledR); }else if(colorIndex>=512&&colorIndex<=767){ ledG=767-colorIndex; anologWrite(gLedPin,ledG); }else if(colorIndex>=768&&colorIndex<=1023){ ledG=colorIndex-768; anologWrite(gLedPin,ledG); }else if(colorIndex>=1024&&colorIndex<=1279){ ledB=1279-colorIndex; anologWrite(bLedPin,ledB); }else if(colorIndex>=1280&&colorIndex<=1535){ ledB=colorIndex-1280; anologWrite(bLedPin,ledB); } delay(colorChangeDelay); }
void fadeOn(){ Serial.println(""); Serial.println("Fade On"); int i; while(i<255){ i++; ledR++; ledG++; ledB++; anologWrite(rLedPin,ledR); anologWrite(gLedPin,ledG); anologWrite(bLedPin,ledB); } }
void fadeOff(){ while(ledR>0){ ledR--; anologWrite(rLedPin,ledR); } while(ledG>0){ ledG--; anologWrite(gLedPin,ledG); } while(ledB>0){ ledB--; anologWrite(bLedPin,ledB); } colorIndex=0; }
|