Stap 2: De schets
Hieronder staan de Arduino schetsen die u zult moeten gebruiken de kilometerteller. Ik omvatte ook de Ino-bestanden (code is hetzelfde zoals hieronder vermeld, maar u kunt de bestanden te downloaden Ino aangezien overlast HTML tags kunnen worden weergegeven in de ingesloten code)
Kilometerteller schets:
<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>
Bekijken afstand schets: (gebruik deze schets te bekijken op de seriële monitor afgelegde afstand)
<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>