Paso 9: Sketch de Arduino
El sketch de Arduino es muy sencillo. Configura pines digitales 4 al 13 como salidas para los dígitos y pin analógico A0 como entrada (el potenciometro de ohmios k 10).
// 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; } }