Étape 3: Le Code
Cette horloge utilise les bibliothèques suivantes (et tout cela grâce à des auteurs de ces bibliothèques) :
- RTCLib Arduino bibliothèque : https://github.com/adafruit/RTClib
- FastLED Arduino bibliothèque v2.1 : http://fastled.io/https://github.com/FastLED/FastLED/releases/tag/ar... (ont lié la v2.1 de la bibliothèque car je ne sais pas si des modifications de code nécessaires pour soutenir la v3.0)
- SerialCommand Arduino Library : https://github.com/scogswell/ArduinoSerialCommand...
- TimerOne Arduino bibliothèque : https://code.google.com/p/arduino-timerone/
La dernière version du code peut être téléchargée depuis le projet github : https://github.com/dushyantahuja/Smart-Infinity-Mi...
! = 1) {/ / vérifier si les couleurs ont été ensemble ou pas EEPROM.write(0,255); / / secondes couleur - R-G-B - blanc EEPROM.write(1,255) ; EEPROM.write(2,255) ; EEPROM.write(3,255) ; Minutes de couleur - R-G-B - EEPROM.write(4,0) rouge ; EEPROM.write(5,0) ; EEPROM.write(6,0) ; Heures de couleur - R-G-B - vert EEPROM.write(7,255) ; EEPROM.write(8,0) ; EEPROM.write(9,0) ; BG couleur - R-G-B - noir EEPROM.write(10,0) ; EEPROM.write(11,0) ; EEPROM.write (12, 0) ; Légère sensibilité - faible EEPROM.write (13, 55) ; Sensibilité à la lumière - haute EEPROM.write (14, 15) ; Minutes pour chaque arc en ciel EEPROM.write(99,1) ; } / / Else lire les paramètres de l’EEPROM d’autre {seconds.r = EEPROM.read(0) ; seconds.g = EEPROM.read(1) ; seconds.b = EEPROM.read(2) ; minutes.r = EEPROM.read(3) ; minutes.g = EEPROM.read(4) ; minutes.b = EEPROM.read(5) ; hours.r = EEPROM.read(6) ; hours.g = EEPROM.read(7) ; hours.b = EEPROM.read(8) ; bg.r = EEPROM.read(9) ; bg.g = EEPROM.read(10) ; bg.b = EEPROM.read(11) ; light_low = EEPROM.read(12) ; light_high = EEPROM.read(13) ; pluie = EEPROM.read(14);} / / *** configurer les commandes série sCmd.addCommand ("MULTI" set_multi) ; sCmd.addCommand ("STAT", clockstatus) ; sCmd.addCommand ("SETRAIN", set_rainbow) ; sCmd.addCommand (« heure », set_hour) ; sCmd.addCommand ("MIN", set_minute) ; sCmd.addCommand (« SEC », set_second) ; sCmd.addCommand ("BG", set_bg) ; sCmd.addCommand ("Lumière", set_light) ; sCmd.addCommand ("TIME", set_time) ; sCmd.addCommand ("manqués", missedCall) ; sCmd.addCommand ("MISSEDOFF", missedOff) ; sCmd.addCommand ("RAINBOW", effets) ; sCmd.addCommand ("manqués", missedCall) ; sCmd.addCommand ("MISSEDOFF", missedOff) ; sCmd.addDefaultHandler(effects) ; *** Définir toutes les LED à couleur de fond pour (int i = 0; j’ai < NUM_LEDS; i ++) {leds [i] = bg;} pinMode(IRemitter,OUTPUT) ; Émetteur IR LED sur la broche numérique 2 digitalWrite(IRemitter,LOW); / / setup IR LED comme arrêt clockstatus() ; attachInterrupt (1, set_multi, chute) ; Timer1.Initialize() ; Timer1.attachInterrupt (État, 500000) ; } void loop() {sCmd.readSerial() ; if(readIR(10) > 50) {/ / interrupteur LED si quelqu'un se trouve à proximité du miroir - afin qu’il peut être utilisé comme un miroir. Éteindre les voyants entre 12:00 et 06:00 à économiser l’énergie et refroidir les LED et alimentations pour (int i = 0; j’ai < NUM_LEDS; i ++) {leds [i] = CRGB::Black;} FastLED.show() ; ledState = 1 ; FastLED.delay(200) ; } else {pour (int j’ai = 0; j’ai < NUM_LEDS; i ++) {leds [i] = bg;} if(multieffects) {/ / vérifier si le bouton pour multi-effets a été actionné uint8_t brocante ; seconde main = now.second() ; if (secondHand == 0) {currentPalette = RainbowColors_p ; currentBlending = mélange;} si (secondHand == 30) {currentPalette = RainbowStripeColors_p ; currentBlending = mélange;} uint8_t statique startIndex = 0; startIndex = startIndex + 1 ; FillLEDsFromPaletteColors (startIndex) ; FastLED.show() ; } if(ledState) d’autre {/ / Main horloge code / / réglage de luminosité en light_high int x = light_high; / / analogRead(IRpin) ; maintenant = rtc.now() ; if ((now.minute() % pluie == 0 & & now.second() == 0)) {effects();} pour (octets j’ai = 0; j’ai < =now.minute(); i ++) {/ / Serial.println(minutes) ; leds [i] = minutes;} / / Serial.println(now.hour(),DEC) ; pour (octets j’ai = 0; j’ai < 60 ; j’ai += 5) {leds [i] = CRGB::White;} pour (octets j’ai = (now.hour () % 12) * 5; i < =((now.hour()) % 12)*5+(now.minute()/12);i++) {leds [i] = heures;} if(now.hour() < 7) LEDS.setBrightness (contraindre () light_low, 0, 100)) ; Régler luminosité à light_low au cours de la nuit - refroidit de LED et alimentations. d’autre LEDS.setBrightness(constrain(light_high,10,255)) ; if(lastsec) {l=leds[now.second()]; leds[now.second()] = secondes ; lastsecond = now.second() ; lastsec = 0; / / Serial.println("ON");} else {leds [lastsecond] = l; if(missed) all_off(); / / Serial.println("OFF") ; lastsec = 1;} FastLED.show() ; ledState = 0 ; } / / delay(250) ; if(multieffects) FastLED.delay(1000 / UPDATES_PER_SECOND) ; }} void FillLEDsFromPaletteColors (colorIndex uint8_t) {uint8_t luminosité = 255 ; pour (int j’ai = 0; j’ai < NUM_LEDS; i ++) {leds [i] = ColorFromPalette (currentPalette, colorIndex, luminosité, currentBlending); colorIndex += 3;}} void set_multi() {statique last_interrupt_time long non signé = 0; unsigned long interrupt_time = millis() ; if (interrupt_time - last_interrupt_time > 200) {if(multieffects) {pour (int j’ai = 0; j’ai < NUM_LEDS; i ++) {leds [i] = bg;}} multieffects = ! Processing ; Serial.println(multieffects) ; } last_interrupt_time = interrupt_time ; } void set_rainbow() {pluie = atoi(sCmd.next()) ; EEPROM.write(14,rain) ; Serial.println ("RAINBOW TIME SET") ; } void clockstatus() {Serial.println ("statut:") ; Serial.Print ("BG:") ; Serial.Print(BG.r) ; Serial.Print("") ; Serial.Print(BG.g) ; Serial.Print("") ; Serial.println(BG.b) ; Serial.Print (« SEC: ") ; Serial.Print(seconds.r) ; Serial.Print("") ; Serial.Print(seconds.g) ; Serial.Print("") ; Serial.println(seconds.b) ; Serial.Print ("MINUTE:") ; Serial.Print(minutes.r) ; Serial.Print("") ; Serial.Print(minutes.g) ; Serial.Print("") ; Serial.println(minutes.b) ; Serial.Print ("heure:") ; Serial.Print(hours.r) ; Serial.Print("") ; Serial.Print(hours.g) ; Serial.Print("") ; Serial.println(hours.b) ; Serial.Print ("lumière ambiante:") ; Serial.println(analogRead(IRpin)) ; Serial.Print (« lumière jeu - haute: ") ; Serial.println(light_high,DEC) ; Serial.Print (« lumière jeu - basse: ") ; Serial.println(light_low,DEC) ; Serial.Print ("Date:") ; DateTime maintenant = rtc.now() ; DateTime(2014,5,2,22,30,0) ; Serial.Print(Now.Day(), DEC) ; Serial.Print('/') ; Serial.Print(Now.month(), DEC) ; Serial.Print('/') ; Serial.println(Now.Year(), DEC) ; Serial.Print ("temps:") ; Serial.Print(Now.Hour(), DEC) ; Serial.Print(':') ; Serial.Print(Now.minute(), DEC) ; Serial.Print(':') ; Serial.Print(Now.second(), DEC) ; Serial.println() ; Serial.Print ("Distance:") ; Serial.println(readIR(5),DEC) ; } void state() {ledState = 1;} const int colorWheelAngle = 255 / NUM_LEDS ; void effects() {Serial.println("RAINBOW") ; pour (int j = 0; j < 3; j ++) {pour (int i = 0; j’ai < 60; i ++) {FillLEDsFromPaletteColors(i) ; FastLED.show() ; Delay(30) ; lastsec}} = 1 ; } void missedCall() {raté = 1;} void missedOff() {raté = 0;} void all_off() {pour (int j’ai = 0; j’ai < NUM_LEDS; i ++) {leds [i] = CRGB::Black;}} void set_hour() {hours.r = atoi(sCmd.next()) ; hours.g = atoi(sCmd.next()) ; hours.b = atoi(sCmd.next()) ; EEPROM.write(6,hours.r) ; EEPROM.write(7,hours.g) ; EEPROM.write(8,hours.b) ; Serial.println ("SET de couleur de l’heure") ; } void set_minute() {minutes.r = atoi(sCmd.next()) ; minutes.g = atoi(sCmd.next()) ; minutes.b = atoi(sCmd.next()) ; EEPROM.write(3,minutes.r) ; EEPROM.write(4,minutes.g) ; EEPROM.write(5,minutes.b) ; Serial.println ("SET couleur MINUTE") ; } void set_second() {seconds.r = atoi(sCmd.next()) ; seconds.g = atoi(sCmd.next()) ; seconds.b = atoi(sCmd.next()) ; EEPROM.write(0,seconds.r) ; EEPROM.write(1,seconds.g) ; EEPROM.write(2,seconds.b) ; Serial.println ("couleur deuxième SET") ; } void set_bg() {bg.r = atoi(sCmd.next()) ; bg.g = atoi(sCmd.next()) ; bg.b = atoi(sCmd.next()) ; EEPROM.write(9,bg.r) ; EEPROM.write(10,bg.g) ; EEPROM.write(11,bg.b) ; Serial.println ("BG couleur SET") ; pour (int i = 0; j’ai < NUM_LEDS; i ++) {leds [i] = bg;}} void set_light() {light_low = atoi(sCmd.next()) ; light_high = atoi(sCmd.next()) ; EEPROM.write(12,light_low) ; EEPROM.write(13,light_high) ; Serial.println ("LIGHT SET") ; } void set_time() {String set_date, set_time ; set_date = (String)sCmd.next() + ' ' + (String)sCmd.next() + ' ' + (String)sCmd.next() ; set_time = (String)sCmd.next() ; rtc.adjust(DateTime(set_date.c_str(),set_time.c_str()));} int readIR (int fois) {int ambientIR; / / variable pour stocker la IR venant de l’obstacleIR ambiante int; / / variable pour stocker la IR venant de la valeur d’int objet [10]; / / valeurs de variable pour stocker la IR distance int; / / variable qui nous dira s’il y a un obstacle ou pas pour (int x = 0 ; x < fois; x ++) {digitalWrite(IRemitter,LOW); / / éteindre les LEDs IR lire le IR venant de la delay(1) ambiante; / / délai minimal nécessaire pour lire les valeurs ambientIR = analogRead(IRpin); / / stockage IR venant de la digitalWrite(IRemitter,HIGH) ambiante; / / allumer les LEDs IR lire le IR venant de l’obstacle delay(1); / / délai minimal nécessaire pour lire les valeurs obstacleIR = analogRead(IRpin); / / stockage IR venant de la valeur de l’obstacle [x] = ambientIR-obstacleIR; / / calcul des changements de valeurs IR et stockage Il moyenne future} pour (int x = 0; x < fois; x ++) {/ / calcul de la moyenne basée sur la valeur "précision" de += distance [x];} digitalWrite(IRemitter,LOW) ; en tournant les LEDs IR hors return(distance/times) ; retourne la valeur finale}