Étape 2: Le croquis
Voici les esquisses Arduino, que vous devrez utiliser le compteur kilométrique. J’ai aussi inclus les fichiers .ino (code est le même que celui ci-dessous, mais vous pouvez télécharger les fichiers .ino étant donné que les balises HTML de nuisance peuvent apparaître dans le code incorporé)
Croquis de l’odomètre :
<p>#include <avr/eeprom.h> #include <avr/sleep.h> volatile unsigned long totRot, rotSinceClr; volatile boolean canClear;//determine if it is //"safe" to zero rotSinceClr //since interrupt could be triggered //during program setup void setup() { CLKPR = B10000000; CLKPR = B00000001;//divide ck by 2 canClear = false; pinMode(2, INPUT_PULLUP); totRot = eeprom_read_dword((uint32_t*)0) + 0x80;//round up rotSinceClr = eeprom_read_dword((uint32_t*)4); attachInterrupt(0, rot, FALLING);//int0 is pin 3 on micro attachInterrupt(1, clr, FALLING);//int1 is pin2 on micro } void clr() { if(canClear) { rotSinceClr = 0; eeprom_update_dword((uint32_t*)4, 0); } } void loop() { cli(); canClear = true; digitalWrite(13, LOW); if(totRot%(0x100) == 0) { eeprom_update_dword((uint32_t*)0, totRot); } if(rotSinceClr%(0x100) == 0) { eeprom_update_dword((uint32_t*)4, rotSinceClr); } //thanks to <a href="http://www.nongnu.org/avr-libc/user-manual/group__avr__sleep.html"> http://www.nongnu.org/avr-libc/user-manual/group_...> set_sleep_mode(SLEEP_MODE_PWR_DOWN); sleep_enable(); sei(); sleep_cpu(); sleep_disable(); sei(); digitalWrite(13, HIGH); //this is the only place in loop() where //interrupts should be called } void rot() { totRot++; rotSinceClr++; }</p>
Regarde un croquis de distance: (utilisez cette esquisse pour afficher la distance parcourue sur le moniteur de la série)
<p>#include <avr/eeprom.h> #define ROT_PER_MI 737//for a 700c wheel void setup() { unsigned long totRot = eeprom_read_dword((uint32_t*)0); unsigned long rotSinceClr = eeprom_read_dword((uint32_t*)4); while(!Serial); Serial.begin(9600); String toPrint = "totRot: "; toPrint += totRot; toPrint += "\nrotSinceClr: "; toPrint += rotSinceClr; Serial.println(toPrint); Serial.print("total miles: "); Serial.println(((float)totRot)/ROT_PER_MI); Serial.print("miles since clr: "); Serial.println(((float)rotSinceClr)/ROT_PER_MI); } void loop() { }</p>