 Binary Arithmetic and Port Manipulation © GPL3+

One port to role them all.

• 3,281 views
• 25 respects

Apps and online services

In this tutorial I will cover the basics of binary arithmetic and its practical uses in arduino.

Setting and storing the state of 8 leds, with only one byte of data.

Also covering some of the less known and used bit functions of Arduino, like bitSet, bitClear and some other bit functions.

For more read about binary arithmetic and its practical uses in arduino

https://playground.arduino.cc/Code/BitMath

Some more info on ports in arduino

https://www.arduino.cc/en/Reference/PortManipulation

Code

The finished example codeArduino
Show the steps (commented out) before using the port
//boolean ledState = {0, 0, 0, 0, 0, 0, 0, 0};
byte ledState = 0;

void setup() {
//  for (int i = 0; i < 8; i++) {
//    pinMode(i, OUTPUT);
//  }
DDRD = B11111111;
}

void loop() {
for (int i = 0; i < 8; i++) {
//digitalWrite(i,HIGH);
//ledState[i] = HIGH;
bitSet(ledState, i);
delay(100);

}
for (int i = 0; i < 8; i++) {
//digitalWrite(i,LOW);
//ledState[i] = LOW;
bitClear(ledState, i);
delay(100);
}
}

//  for (int i = 0; i < 8; i++) {
//    //digitalWrite(i,ledState );
//    digitalWrite(i, bitRead(ledState, i));
//  }
PORTD  = ledState;
}

Schematics • 8,160 views
• 26 respects

Binary Wristwatch

Project showcase by thallia

• 1,568 views
• 11 respects

Amazing Binary Clock using LED Matrix Module

Project in progress by LAGSILVA

• 11,315 views
• 61 respects

Digital And Binary Clock With Two LED Matrix And RTC

Project tutorial by LAGSILVA

• 10,803 views
• 42 respects

3-Bit Binary Calculator Using Arduino Uno

Project showcase by 22warehamD

• 5,122 views