Set Port from variable

Hi all, I have just gotten myself an uno after having played with picaxe for many years.
I am feel that picaxe may have spoilt me with the options for simplification along side the basic code.
While working on a project that will involve linear actuators controlled most likely by some stepper motors I ran some test code

This is how it looked on the picaxe, its work flawlessly

symbol steppos    = b0  		'set position variable
symbol lope 	= w2  		'set loop variable
symbol stepper    = b2  		'set output variable

let dirsb = %11111111
init:
	let steppos = 0   		'set startup setting
	gosub stepinc
main:
	for lope = 0 to 4096    	'loop 4096, or 1 rotoation
		inc steppos 		'set lookup value
		if steppos > 7 then     'if lookup value is exceed number of value in table then set to begining
		let steppos = 0   	
		end if	
		gosub stepinc     	'run sub procedure
	next lope
	pause 200   			'wait for 200ms
	for lope = 0 to 2048    	'set half rotoation
		dec steppos 		'decrease position
		if steppos > 7 then     'check for underflow
		let steppos = 7
		end if
		gosub stepinc     	'run subprocedure
	next lope
	goto main
	
stepinc:
	lookup steppos,(128,192,64,96,32,48,16,144),stepper
						'find pin value for portb
	let pinsb = stepper     	'set portb value
	return

so I decided to see if I could create a similar programme for the uno, that has been some research and a fair bit of head scratching, but cannot find how to set the port to the value stored in val

int lope = 0;
int steppos = 0;
int look [] = {128,192,64,96,32,48,16,144};
int val;
void setup() {
  DDRD = B11110000;    //set pins 5,6,7,8 as output
  val = increment();
}

void loop() {
  while (lope < 4096){
    steppos = steppos + 1;
    if (steppos > 7){
      steppos = 0;
    }
    val = increment();
    lope = lope + 1;
  }

  delay (200);
  lope = 0;
  while (lope < 2048){
    steppos = steppos + 1;
    if (steppos < 0){
      steppos = 7;
    }
    val = increment();
    lope = lope + 1;
  }
}

int increment() {
  val = look[steppos];
  return val;
}

I know that most will tell me that I not ideal to access the port directly, but the results are rather please on picaxe at least.
I am wondering if val should be set to a byte and the lookup values set to what I would like the port to do
Any thought on this would be apreciated

I use direct port manipulation all the time, nothing wrong with that. May make code less portable across chips, but I try & write for speed, not portability.

PORTD = PORTD & 0b00001111; // clear upper 4 bits, leave lower 4 alone PORTD = PORTD | (val & 0b11110000); // OR in upper 4 bits of val, leave lower4 of PORTD alone

OK, at least I know all should be fine.

I must admit that I am still a little confused as to how that works, but I will trawl my way through some more information and see what I can find.

Thanks for the help

AND bit with 0 clears a bit, AND bit with 1 leaves the bit alone: 0 & 1 = 0 1 & x = x, x can be 0 or 1

OR bit with 0 leave a bit as it was, OR bit with 1 makes the bit a 1. 0 | x = x, x can be 0 or 1 1 | x = 1

OK that makes sense thanks, I also take it that clearing the port must be done before I send the new value

You had a couple of errors in your conversion (and the below one will also have something I’ve missed).
Main error was…

  DDRD = B11110000;    //set pins 5,6,7,8 as output

as pin 8 on the UNO is part of PORTB, I altered the description so it reflects the pins 4-7.
It’s all a bit hard to read but I have tried to do the Arduino equivalent inline with the basic version.

const byte look [] = {128,192,64,96,32,48,16,144};

// symbol steppos    = b0  		'set position variable
byte steppos = 0;
// symbol lope 	= w2  		'set loop variable
int lope = 0;
// symbol stepper    = b2  		'set output variable
byte stepper = 0;

void setup(){
  DDRD = B11110000;    //set pins 4,5,6 & 7 as output
// let dirsb = %11111111
// init:
	// let steppos = 0   		'set startup setting
	// gosub stepinc
  stepinc(steppos);
}

void loop(){
// main:
  for(lope = 0; lope <= 4096; lope++){
	// for lope = 0 to 4096    	'loop 4096, or 1 rotoation
    steppos++;
		// inc steppos 		'set lookup value
    if(steppos > 7){
		// if steppos > 7 then     'if lookup value is exceed number of value in table then set to begining
      steppos = 0;
		// let steppos = 0 
  	}
		// end if	
    stepinc(steppos);
		// gosub stepinc     	'run sub procedure
  }
	// next lope
  delay(200);
	// pause 200   			'wait for 200ms
  for(lope = 0; lope <= 2048; lope++){
	// for lope = 0 to 2048    	'set half rotoation
    steppos--;
		// dec steppos 		'decrease position
    if(steppos > 7){
		// if steppos > 7 then     'check for underflow
      steppos = 7;
		// let steppos = 7
    }
		// end if
    stepinc(steppos);
		// gosub stepinc     	'run subprocedure
  }
	// next lope
}
	// goto main
	
void stepinc(byte index){
// stepinc:
  stepper = look[index];
	// lookup steppos,(128,192,64,96,32,48,16,144),stepper
						// 'find pin value for portb
	// let pinsb = stepper     	'set portb value
  byte temp = PIND & 0x0F;    // Read port & strip off upper 4 bits
  stepper = stepper | temp;   // OR with stepper bits
  PORTD = stepper;            // Write it out
}
	// return