Binary Arithmetic and Port Manipulation

Binary Arithmetic and Port Manipulation © GPL3+

One port to role them all.

  • 3,281 views
  • 6 comments
  • 25 respects

Components and supplies

Apps and online services

About this project

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[8] = {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);
    adjustLedState();
    delay(100);

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

void adjustLedState() {
  //  for (int i = 0; i < 8; i++) {
  //    //digitalWrite(i,ledState[1] );
  //    digitalWrite(i, bitRead(ledState, i));
  //  }
  PORTD  = ledState;
}

Schematics

Schematics
Schamtics i95rbqcvqf

Comments

Similar projects you might like

Binary Wristwatch

Project showcase by thallia

  • 1,568 views
  • 0 comments
  • 11 respects

Amazing Binary Clock using LED Matrix Module

Project in progress by LAGSILVA

  • 11,315 views
  • 13 comments
  • 61 respects

Digital And Binary Clock With Two LED Matrix And RTC

Project tutorial by LAGSILVA

  • 10,803 views
  • 36 comments
  • 42 respects

3-Bit Binary Calculator Using Arduino Uno

Project showcase by 22warehamD

  • 5,122 views
  • 9 comments
  • 13 respects

Project-1, Binary Counting

by Team P.O.I.N.T.

  • 4,773 views
  • 1 comment
  • 15 respects
Add projectSign up / Login