Binary Arithmetic and Port Manipulation

Binary Arithmetic and Port Manipulation © GPL3+

One port to role them all.

  • 7,270 views
  • 12 comments
  • 37 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 Arithmetic and Port Manipulation – Part II

Project tutorial by Tal O

  • 1,328 views
  • 0 comments
  • 3 respects

3-Bit Binary Calculator Using Arduino Uno

Project showcase by 22warehamD

  • 11,356 views
  • 9 comments
  • 17 respects

Binary Wristwatch

Project showcase by thallia

  • 3,152 views
  • 0 comments
  • 12 respects

Digital And Binary Clock With Two LED Matrix And RTC

Project showcase by LAGSILVA

  • 21,526 views
  • 47 comments
  • 58 respects
Add projectSign up / Login