There are 10 kinds of people....

"There are 10 kinds of people, those who understand binary system and those who don't."

This is an old joke for programmers. Maybe you are wondering why 10 and not 2? The answer is that number 10 in binary system (the one that a computer can handle) really represents number 2 of decimal system.

Watch the video to understand how it works:

The following table shows how the numbers match:


Now, let's create a NodeMCU game with three LEDs and three buttons. Connect the components based on the following diagram:

ft

Everytime we press a button, the corresponding LED will be lighted. So, we can create any combination of activated and not activated LEDs (ON and OFF).

The code for the above setup is the following:

int led1Pin = 14;
int led2Pin = 2;
int led3Pin = 5;
int in1Pin = 10;
int in2Pin = 16;
int in3Pin = 4; 
int val = 0;  

void setup() {
  pinMode(led1Pin, OUTPUT); 
  pinMode(led2Pin, OUTPUT);
  pinMode(led3Pin, OUTPUT);
  pinMode(in1Pin, INPUT);
  pinMode(in2Pin, INPUT);
  pinMode(in3Pin, INPUT);   
}

void loop(){
  val = digitalRead(in1Pin); 
  if (val == LOW) {        
    digitalWrite(led1Pin, LOW); 
  } else {
    digitalWrite(led1Pin, HIGH); 
  }

  val = digitalRead(in2Pin); 
  if (val == LOW) {        
    digitalWrite(led2Pin, LOW); 
  } else {
    digitalWrite(led2Pin, HIGH); 
  }

  val = digitalRead(in3Pin); 
  if (val == LOW) {        
    digitalWrite(led3Pin, LOW); 
  } else {
    digitalWrite(led3Pin, HIGH); 
  }

}


Copy the above code, paste it on Arduino IDE and upload it to the NodeMCU. Then try the following: Create binary numbers from 0 to 7 using the three push buttons. You can get help from the table above. For example 5 is represented by 1st LED ON, 2nd LED OFF and 3rd LED ON.

Try it yourself. Then ask from the other students to guess the numbers you create and don't forget: Have fun with coding!