Materials: Ardiuno Leonardo (Iskra), motor, diode, buttons, Transistor (MOSFET), cables, 9V Battery.

#define MOTOR_PIN 9
#define FIRST_BUTTON_PIN 5
#define BUTTON_COUNT 3
//define step for motor speed change according press
//of concrete button
#define SPEED_STEP (255 / (BUTTON_COUNT - 1))

void setup ()
{
    pinMode(MOTOR_PIN, OUTPUT);
    //enabling pin input with pull up
    for (int i = 0; i < BUTTON_COUNT; ++i)
      pinMode(i + FIRST_BUTTON_PIN, INPUT_PULLUP);
}

void loop()
{
    for  (int i = 0; i < BUTTON_COUNT; ++i) {
      //if button is NOT pressed go to next step
      if (digitalRead(i + FIRST_BUTTON_PIN))
      continue;
      //if button is pressed switching the according speed
      int speed = i * SPEED_STEP;
      //send signal to the motor
      analogWrite(MOTOR_PIN, speed);
    }
}

address: 20 Myasnitskaya ulitsa
(metro stations ‘Lubyanka’ and ‘Kitay-Gorod’)
Moscow 101000 Russia

phone: +7(495)772-95-90 *15026

email: city@hse.ru