Created
January 6, 2016 16:25
-
-
Save quanon/61142395be45cf7a4229 to your computer and use it in GitHub Desktop.
2 進数カウンター
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const int LED4 = 13; | |
const int LED3 = 12; | |
const int LED2 = 11; | |
const int LED1 = 10; | |
const int BTN1 = 7; | |
int old_input = LOW; | |
int new_input = LOW; | |
int count = 0; | |
void setup() { | |
pinMode(LED4, OUTPUT); | |
pinMode(LED3, OUTPUT); | |
pinMode(LED2, OUTPUT); | |
pinMode(LED1, OUTPUT); | |
pinMode(BTN1, INPUT); | |
// Serial.begin(38400); | |
} | |
void loop() { | |
digitalWrite(LED4, LOW); | |
digitalWrite(LED3, LOW); | |
digitalWrite(LED2, LOW); | |
digitalWrite(LED1, LOW); | |
new_input = digitalRead(BTN1); | |
if ((old_input == LOW) && (new_input == HIGH)) { | |
count++; | |
delay(10); // デバウンシング | |
// Serial.println(count); | |
} | |
// & : マスク (AND) | |
if ((count & 8) == 8) { | |
digitalWrite(LED4, HIGH); | |
} | |
if ((count & 4) == 4) { | |
digitalWrite(LED3, HIGH); | |
} | |
if ((count & 2) == 2) { | |
digitalWrite(LED2, HIGH); | |
} | |
if ((count & 1) == 1) { | |
digitalWrite(LED1, HIGH); | |
} | |
old_input = new_input; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment