Read with PINx ??? Port manupulation as input

Hi

I want to use port manipulation to read 4 sensors with port manipulation.

Have a good idea to use it as outputs and st them to inputs, but reading them?

How do you call read pin X with PIN(register)?

Step 1: set DDR(register) = B00000000;
step 2: int val=0;
step3: ??? val= PIN(register) B???

READ digital pin 6

set DDRD input: DDRD = DDRD & B00111111; /or?/ DDRD=B00000000;
variable: int val=o;
read val = (PIND&(1<<PD7)); /or?/ Val = (PIND,B?????/)

You have to read the entire port into a variable.

Why? This makes your program much more difficult to wire, to write, to debug and restricts you to only working on one specific Arduino. It's only necessary if you need to read many things in an extremely short period of time and usually 4 sequential digitalRead() statements is fast enough for 99% of all possible Arduino programs.

Thanks for the reply

looking at my total project I am reading four sensor's and time is a factor.

So I am looking at porting to make my code run faster.

If it reads the entire port into a variable, how does one know whether the pin I am using has changed state?

You will have to mask that bit then compare it to the previous read bit.
What is your application that is running so fast?
.

Tjaart:
how does one know whether the pin I am using has changed state?

By comparing the value with the previous one?

how does one know whether the pin I am using has changed state?

The corresponding bits in the value of the variable will have changed state. Save the variable, read the port, compare the bits, maybe using bitRead() or masking them using the bitwise logical operators if speed is a factor. You were warned that it would be more complicated than using digitalRead() !

Ooo. :o

A bit more about the project:

4X sensors. 2X at the start and 2X at the finish. with a known distance between start and Finnish I can calculate the speed of a BULLET traveling trough 25cm space.

I have been at this before gave up and now started again.
Would you be so kind as to show me a code snippet as to how one would go about comparing this and that to come up with a state change on a pin.

I made a sketch that tries to to detect changes in bit(s) and to compare the runtime of the methodes.
(read digital pin 13 vs read PINB (as example))

byte valNow;
byte valPrev;
byte valChangedToOne;
byte valChangedToZero;

unsigned long lastSecond;
unsigned long loopsPerSecond;
unsigned long cyclesLast;
unsigned long cyclesNow;
bool alternative = false;

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(115200);
  Serial.println(F("Loops per second, duration of one loop, same in cycles, if better print gain in cycles"));
}

void loop() {
  unsigned long topLoop = millis();
  loopsPerSecond++;

  if (alternative) {

    valNow = digitalRead(LED_BUILTIN);
    if (valNow != valPrev) {
      valChangedToOne = valNow;
      valChangedToZero = !valNow;
      valPrev = valNow;
    } else {
      valChangedToOne = false;
      valChangedToZero = false;
    }

  } else {

    valNow = PINB;
    byte anyChange = valPrev ^ valNow;
    valChangedToOne = anyChange & valNow;
    valChangedToZero = anyChange & ~valNow;
    valPrev = valNow;

  }

  if (topLoop - lastSecond > 1000) {
    Serial.print(loopsPerSecond);
    Serial.print(F(" lps "));
    unsigned long nanosPerLoop = 1000000000L / loopsPerSecond;
    Serial.print(nanosPerLoop);
    Serial.print(F(" nS "));
    cyclesNow = nanosPerLoop / 65;
    Serial.print(cyclesNow);
    Serial.print(F(" cycles "));
    if (cyclesLast >= cyclesNow) {
      Serial.print(cyclesLast - cyclesNow);
    }
    Serial.println();
    alternative = !alternative;
    lastSecond = topLoop;
    loopsPerSecond = 0;
    cyclesLast = cyclesNow;
    digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
  }
}
Loops per second, duration of one loop, same in cycles, if better print gain in cycles
91295 lps 10953 nS 168 cycles 
59801 lps 16722 nS 257 cycles 
91326 lps 10949 nS 168 cycles 89
59795 lps 16723 nS 257 cycles 
91325 lps 10949 nS 168 cycles 89
59795 lps 16723 nS 257 cycles 
91325 lps 10949 nS 168 cycles 89
59795 lps 16723 nS 257 cycles 
91325 lps 10949 nS 168 cycles 89
59795 lps 16723 nS 257 cycles 
91325 lps 10949 nS 168 cycles 89

If timing of the transitions is important, you should be using interrupts to measure the transitions. Just reading the ports is not going to do it.

Thank you

Back to the drawing board.