Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
Tal O
Published © GPL3+

Binary Arithmetic and Port Manipulation

One port to role them all.

BeginnerProtip9,578
Binary Arithmetic and Port Manipulation

Things used in this project

Story

Read more

Schematics

Schematics

Code

The finished example code

Arduino
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;
}

Credits

Tal O
20 projects • 55 followers
Maker @ heart

Comments