Stap 9: Arduino Sketch
De Arduino schets is heel eenvoudig. Het stelt digitale pinnen 4 tot en met 13 als uitgangen aan de cijfers, en analoge pin A0 als input (de 10 k ohm potmeter).
// the potentiometer is on analog pin 0 const int pot_pin = A0; // this array sets up the digits pins used for the digits const int digit_pins[] = {4,5,6,7,8,9,10,11,12,13}; // the number of digits const uint8_t digit_count = 10; // stores the new index into the digit_pins array (0 to 9) as read from // the potentiometer int v = 0; // this incrementor is used a couple times int i = 0; // stores the current digit value int x = 0; // an obviously out of range digit to start with int last = 20; void setup() { // Serial.begin(9600); // for debugging delay(200); // let the Serial start up // loop over the digit_pins array to set their pin mode for (i = 0; i < digit_count; i++) { pinMode(digit_pins[i], OUTPUT); } // set pin mode fotr the potentiometer pinMode(pot_pin, INPUT); } void loop() { x = analogRead(pot_pin); v = (int) (x / 102.3); // only do something if the value has changed. if (v != last) { // for debugging // Serial.print(x); // Serial.print(' '); // Serial.println(v); // loop over the array; turn each digit on or off as appropriate for (i = 0; i < digit_count; i++) { if (v == i) { // turns a digit on digitalWrite(digit_pins[i], LOW); } else { // turns a digit off digitalWrite(digit_pins[i], HIGH); } } // update the value last = v; } }