Trying to use PortB

I need to use the

DDRB y PORTB in order to take a byte, but it doesn´t works.

My code is the following, what I´m doing wrong?

void setup()
{

DDRB = B00111111; // digital pins -,-,13,12,11,10,9,8

PORTB = B00111000; // turns on 13,12,11; turns off 10,9,8
}

void loop()
{
}

I find nothing wrong with your code. I took the liberty to expand on your code to verify. It turns on and off the on board led every 2 seconds so it should be good.

void setup()
{
Serial.begin(9600);
DDRB = B00111111; // digital pins -,-,13,12,11,10,9,8

PORTB = B00111000; // turns on 13,12,11; turns off 10,9,8
}

void loop()
{
Serial.println(PINB, BIN);
Serial.println(PINB, HEX);
delay(2000);
PORTB = ~PINB;
}

Thank you for your response.

I tried your code and it doesn´t work (I´m using arduino mega 2560). It turns on 10 and 11.

With PortD it works as with this.

I don´t know what else I should make.

On the Mega, PortB is on different pins. Refer to the schematic, basically its pins 51 52 53 54 10 11 12 13. Maybe not that order though.

The code works but you have to match it to the hardware.

Cheers!

I tested the code with UNO Rev 3. If I am looking at the schematic correctly you need to shift your bits over to the left.

13 = PB7
12 = PB6
11 = PB5
10 = PB4

Thank you for your responses.

I copy the ports:

PC0=37
PC1=36
PC2=35
PC3=34
PC4=33
PC5=32
PC6=31
PC7=30

PB0=53
PB1=52
PB2=51
PB3=50
PB4=10
PB5=11
PB6=12
PB7=13

Thank you again