Skip to content

Instantly share code, notes, and snippets.

@murych
Created April 25, 2018 12:55
Show Gist options
  • Save murych/5bf80d84c4c2fb01be9a9405b99770a3 to your computer and use it in GitHub Desktop.
Save murych/5bf80d84c4c2fb01be9a9405b99770a3 to your computer and use it in GitHub Desktop.
// предполагается, что светодиоды
// подключаются подряд к портам 1, 2, 3 и т.д.
// допустим, есть 6 светодиодов
#define leds_max 6
#define pot A1
void setup() {
for (int pin = 0; pin < leds_max; ++pin) {pinMode(pin, OUTPUT);}
pinMode(pot, INPUT);
}
void light(int led_on) {
// const int led_on = 1;
int led_off = leds_max - led_on;
for (int led = 0; led < led_on; ++led){digitalWrite(led, HIGH);}
for (int led = led_on; led < led_off; ++led){digitalWrite(led, LOW);}
}
void loop() {
int x = analogRead(pot) / 256;
if (x==1) light(1);
if (x==2) light(2);
if (x==3) light(3);
if (x==4) light(4);
}
// ИЛИ
void loop() {
light(analogRead(pot) / 256)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment