Étape 5: Le code
Codage pour moi est une douleur!!!
J’ai shearched sur le croquis d’arduino travail web.
Beaucoup d'entre eux ne fonctionne pas, mais je trouve ce gars sur youtube :
Il fait un bon travail et donner un bon code du travail sur son codebender :
J’ai fait cuire son code avec plus de modèles comme : bonbon, roues de style ou arc en ciel théâtre
Ceci est mon code de travail :
#include < SPI.h > #include < Ethernet.h > #include < Adafruit_NeoPixel.h > / / paramètres du réseau Local de mac byte [] = {0xBC, 0x2D, 0 x 41, 0 x 70, 0 x 07, 0x98} ; Doit être unique sur le réseau local #define luminosité 5 //Set luminosité à 5/10e / / vous pouvez ajuster la luminosité #define complète (255/luminosité) #define thingSpeakInterval 18000 / / heure d’intervalle, en millisecondes, d’obtenir des données de ThingSpeak (nombre de secondes * 1000 = intervalle) / / Configuration Variable long lastConnectionTime = 0 ; String lastCommandString = « black » ; lastConnected booléen = false ; int failedCounter = 0 ; Bande de Adafruit_NeoPixel = Adafruit_NeoPixel (167, 6, NEO_GRB + NEO_KHZ800) ; Pour moi, 167 pixels sur ma bande / / Initialize Arduino Ethernet Client EthernetClient client ; void setup() {delay(100); / / configuration série Serial.begin(9600) ; delay(100) ; Serial.Flush() ; Delay(100) ; Strip.Begin() ; Strip.Show() ; Commencer l’Ethernet sur Arduino startEthernet() ; } void loop() {/ / commandes de processus CheerLights if(client.available() > 0) {delay(100); / / Serial.println(client.available()) ; Chaîne de réponse ; charIn char ; {charIn = client.read(); / / lecture d’un char de la mémoire tampon réponse += charIn; / / ajouter ce char à la chaîne de réponse} while (client.available() > 0) ; Serial.println(Response.length()) ; Serial.println(Response) ; Si (réponse == « blanc » || réponse == « blanc chaud » || réponse == « oldlace ») {lastCommandString = « blanc »; theaterChaseRainbow(50);} ElseIf (réponse == « noir » || réponse == « off ») {lastCommandString = « black »; theaterChase (bande. Color(0,0,0),50) ; } ElseIf (réponse == « rouge ») {lastCommandString = « red »; //theaterChase (bande. Color(255,0,0),50) ; CandyCane(30,8,50) ; } ElseIf (réponse == « vert ») {lastCommandString = « vert »; theaterChase (bande. Color(0, 255, 0), 50) ; } ElseIf (réponse == « bleu ») {lastCommandString = « blue »; theaterChase (bande. Color(0,0,255),50) ; } ElseIf (réponse == « cyan ») {lastCommandString = « cyan »; theaterChase (bande. Color(0,255,255),50) ; } ElseIf (réponse == « magenta ») {lastCommandString = « magenta »; theaterChase (bande. Color(255,0,255),50) ; } ElseIf (réponse == « jaune ») {lastCommandString = « jaune »; theaterChase (bande. Color(255,255,0),50) ; } ElseIf (réponse == « purple ») {lastCommandString = « pourpre »; theaterChase (bande. Color(102,51,204),50) ; } ElseIf (réponse == « orange ») {lastCommandString = « orange »; theaterChase (bande. Color(255,153,0),50) ; theaterChase (bande. Color(0, 255, 0), 50) ; } ElseIf (réponse == « rose ») {lastCommandString = « rose »; theaterChase (bande. Color(255,53,153),50) ; } delay(200) ; Serial.Print ("CheerLight commande reçue:") ; Serial.println(lastCommandString) ; Delay(200) ; } / / Débrancher ThingSpeak si (! client.connected() & & lastConnected) {Serial.println("...disconnected") ; client.stop();} / / s’abonner au canal de ThingSpeak et sur le terrain si ()! client.connected() & & (millis() - lastConnectionTime > thingSpeakInterval)) {subscribeToThingSpeak();} checkcolor(lastCommandString) ; Delay(500) ; Vérifier si l’Arduino Ethernet doit être redémarré si (failedCounter > 3) {startEthernet();} lastConnected = client.connected() ; Delay(100) ; } // End loop void subscribeToThingSpeak() { if (client.connect("api.thingspeak.com", 80)) { Serial.println("Connecting to ThingSpeak..."); failedCounter = 0 ; Serial.println ("envoi de demande ») ; client.println (« GET /channels/1417/field/1/last.txt") ; client.println() ; lastConnectionTime = millis() ; } else {failedCounter ++ ; Serial.println ("Connection to ThingSpeak a échoué ("+ String(failedCounter, DEC) +")") ; Serial.println() ; lastConnectionTime = millis() ; Sub}} startEthernet() {client.stop() ; Serial.println ("connexion Arduino pour réseau...") ; Serial.println() ; Delay(1000) ; Se connecter au réseau amd obtenir une adresse IP via DHCP si (Ethernet.begin(mac) == 0) {Serial.println ("DHCP a échoué, reset Arduino pour essayer à nouveau") ; Serial.println() ; } else {Serial.println ("Arduino connectée au réseau via DHCP") ; Serial.println() ; } delay(1000) ; } / / Remplir les points un après l’autre avec une colorWipe Sub de couleur (uint32_t c, uint8_t attente) {pour (uint16_t j’ai = 0; j’ai} //Theatre-style lumières rampants. void theaterChase (uint32_t c, uint8_t attente) {pour (int j = 0; j < 36; j ++) {//do 36 cycles de chasse pour (q int = 0; q < 3; q ++) {pour (int j’ai = 0; j’ai < strip.numPixels(); i = i + 3) {strip.setPixelColor (i + q , c); allumer chaque pixel troisième} strip.show() ; Delay(Wait) ; pour (int j’ai = 0; j’ai < strip.numPixels(); i = i + 3) {strip.setPixelColor (i + q, 0); //turn chaque pixel troisième arrêt}}}} //Theatre-style rampant s’allume avec arc-en-ciel effet Sub theaterChaseRainbow (uint8_t attente) {pour (int j = 0; j < 256; j ++) {/ / cycle tous les 256 couleurs dans la roue de (q int = 0; q < 3; q ++) {pour (int j’ai = 0; j’ai < strip.numPixels(); i = i + 3) {strip.setPixelColor (i + q Roue ((i + j) % 255)) ; allumer chaque pixel troisième} strip.show() ; Delay(Wait) ; pour (int j’ai = 0; j’ai < strip.numPixels(); i = i + 3) {strip.setPixelColor (i + q, 0); //turn chaque pixel troisième arrêt}}}} / / arc-en-ciel roue Sub rainbowCycle (uint8_t attente) {int i, j; pour (j = 0; j < 256 * 5; j ++) {/ / 5 cycles de 25 toutes les couleurs dans la roue pour (j’ai = 0; j’ai < strip.numPixels(); i ++) {strip.setPixelColor (i, roue (((i * 256/strip.numPixels()) + j) % 256));} strip.show(); / / écriture tous les pixels sur delay(wait);}} Sub arc-en-ciel (uint8_t attente) {int j’ai , j ; pour (j = 0; j < 256; j ++) {/ / 3 cycles de tous les 256 couleurs dans la roue pour (i = 0; j’ai < strip.numPixels(); i ++) {strip.setPixelColor (i, roue ((i + j) % 255));} strip.show(); / / écriture tous les pixels sur delay(wait);}} / * Fonctions d’assistance * / / / créer une valeur de couleur de 24 bits de R, G, B uint32_t couleur (octet r, g octet, byte b) {uint32_t c; c = c; r << = 8; c | = g; c << = 8; c | = b; c retour;} //Input une valeur de 0 à 255 pour obtenir une valeur de couleur. Les couleurs sont une transition r - g -b - retour à r uint32_t roue (WheelPos octets) {si (WheelPos < 85) {retourner couleur (WheelPos * 3, 255 - WheelPos * 3, 0);} ElseIf (WheelPos < 170) {WheelPos-= 85 ; retour Color (255 - WheelPos * 3, 0, WheelPos * 3);} d’autre {WheelPos-= 170 ; retour couleur (0, WheelPos * 3, 255 - WheelPos * 3);}} void checkcolor (couleurs de la chaîne) {si (couleurs == « blanc ») {theaterChaseRainbow(50);} ElseIf (couleurs == « noir ») {theaterChase (bande. Color(0,0,0),50) ; } ElseIf (couleurs == « rouge ») {//theaterChase (bande. Color(255,0,0),50) ; CandyCane(30,8,50) ; 30 ensembles, 8 pixels de large, 50us délai} ElseIf (couleurs == "vert") {theaterChase (bande. Color(0, 255, 0), 50) ; } ElseIf (couleurs == « bleu ») {theaterChase (bande. Color(0,0,255),50) ; } ElseIf (couleurs == « cyan ») {theaterChase (bande. Color(0,255,255),50) ; } ElseIf (couleurs == « magenta ») {theaterChase (bande. Color(255,0,255),50) ; } ElseIf (couleurs == « jaune ») {theaterChase (bande. Color(255,255,0),50) ; } ElseIf (couleurs == « purple ») {theaterChase (bande. Color(102,51,204),50) ; } ElseIf (couleurs == « orange ») {theaterChase (bande. Color(255,153,0),50) ; theaterChase (bande. Color(0, 255, 0), 50) ; } ElseIf (couleurs == « rose ») {theaterChase (bande. Color(255,53,153),50) ; } }