Programming ATtiny48

Hi everyone, i need help. I don't know if it is the right topic but I'll try anyway. I have a ATtiny48, I made a small PCB to program it with an arduino uno and it works well. I'm able to code it to use all of the pin as output to drive LED (I need 18 pins for that). The part that's making me go mad is when i need to use input. Despite all my effort, I'm not able to read pins. I want to have 5 button for my project, but nothing work. I'll add a copie of my code. I know the output works, it's really just the input that doesn't cooperate.

void setup()
{
  DDRC |= (1 << PC0);   // pin as output
  DDRB &= ~(1 << PB0);  // pin as input
}

void loop()
{
  if (PINB & (1 << PB0))      // When the pin is HIGH
  {
    PORTC |= (1 << PC0);      // LED  light
  }
  else
  {
    PORTC &= ~(1 << PC0);     // LED no light
  }
}

You are using direct port manipulation, which is an advanced topic, but appear to be having a basic problem. Is that AI generated code?
Have you connected a pull down resistor (say 10k) to pin PB0 ?

No it’s not AI generated, I do have a 10k pull down on my inputs. I just don’t understand how to Read my inputs in that coding way. I’m not that knowing on that

Which Arduino core are you using for programming the attiny48 ?
Maybe PB0 has been assigned a strange value other than the 0 you are expecting. Maybe an arduino pin number instead. Try replacing PB0 with simply 0 and see if you get further.
Can you use Serial.print() for printing debug information?

The code looks correct to me.
Are you sure that you have your switch on the pin that you think it is on? Can you post a photo of your hardware setup?

When you say that it doesn't work, you mean that your LED doesn't light even when you push the button?

For buttons wired PIN >> BUTTON >> GND, configure the button pin with INPUT_PULLUP by setting PB0 as output (1), set PB0 HIGH (1), then set PB0 as input (0).

Try getting your project working without port access... then translate the code to port access.

byte buttonPin[] = {0, 1, 2, 3, 4}; // your PBx
byte ledPin[] = {5, 6, 7, 8, 9}; // your PCx
byte pinSize = sizeof(buttonPin)/sizeof(buttonPin[0]);

void setup() {
  for (byte i = 0; i < pinSize; i++) {
    pinMode(buttonPin[i], INPUT_PULLUP);
    pinMode(ledPin[i], OUTPUT);
  }
}

void loop() {
  for (byte i = 0; i < pinSize; i++) {
    if (digitalRead(buttonPin[i]) == 0)
      digitalWrite(ledPin[i], HIGH);
    else
      digitalWrite(ledPin[i], LOW);
  }
}

And you were able to write that code yourself without understanding what you were doing? Wow!

I'm not sure what level the OP is at, and the cool looking direct port manipulation stuff he/she has churned out may give a misleading impression, but it is probably worth mentioning explicitly that, in this code sample case, the talked about 10k pull down resistors on the input pins have to be removed.

I see. Changing the "c" example from INPUT_PULLUP to INPUT and button nominal state as LOW.

byte buttonPin[] = {0, 1, 2, 3, 4}; // your PBx
byte ledPin[] = {5, 6, 7, 8, 9}; // your PCx
byte pinSize = sizeof(buttonPin) / sizeof(buttonPin[0]);

void setup() {
  for (byte i = 0; i < pinSize; i++) {
    pinMode(buttonPin[i], INPUT);
    pinMode(ledPin[i], OUTPUT);
  }
}

void loop() {
  for (byte i = 0; i < pinSize; i++) {
    if (digitalRead(buttonPin[i]) == HIGH)
      digitalWrite(ledPin[i], HIGH);
    else
      digitalWrite(ledPin[i], LOW);
  }
}

I did a lot of research for programming it that way. It's just that I'm still new to it (it's literally my first time) and the reading part is the only one that I can't make work.

I did try using code that way but the microcontroller doesn't works when I do it that way. That's the reason I went with port access. It's the only way I found to make my output works so I guess it's also the way to make my input work.

Post the code you were using that does not work, and explain the results from the code you observe versus the results from the code you expect.

If I use exactly the on you sent, my outputs are barely able to light up a red LED (i see a bit of red but it's not much), a green one doesn't show anything. Even if I code it to make all my pins as output on HIGH, not all of them are able to light up my LED, even barely. It's the same for the input, nothing i do is able do make them read anything. Since I only have the chip, I can't use the serial port to debug ( I program the ATtiny48 on my arduino UNO then I put it on my board to test it).

Then why bother??? Just use your Arduino instead of the ATtiny48!
Just kidding

Did you try to set PB0 as output? Does it work? If yes, try the opposit of your code in #1:
PB0 as output and PC0 as input

I posted bad code on the first try (I did not pay attention to your pulldown resistors). I amended that in the second try for the pulldown resistors.

I think this is valid. Use the Arduino, with its Serial Monitor, to program reading and writing registers, and when it is time to compile for the ATtiny48, make your code use ifdef ATTINY48 to use the correct DDRs/PORTs/PINs and no serial monitor.

I did use the second code you sent, for the part about using the arduino I don't think i understand.

I did try to use other pin as input, neither of them seams to works as input but all of them works as output for LED.

Could you find the definition of PINB to see what address this is pls? (ctrl + click on it should do the thing)

That's what I found :

#define PIND _SFR_IO8(0x09)
#define PIND0 0
#define PIND1 1
#define PIND2 2
#define PIND3 3
#define PIND4 4
#define PIND5 5
#define PIND6 6
#define PIND7 7

#define PINC _SFR_IO8(0x06)
#define PINC0 0
#define PINC1 1
#define PINC2 2
#define PINC3 3
#define PINC4 4
#define PINC5 5
#define PINC6 6
#define PINC7 7

#define PINB _SFR_IO8(0x03)
#define PINB0 0
#define PINB1 1
#define PINB2 2
#define PINB3 3
#define PINB4 4
#define PINB5 5
#define PINB6 6
#define PINB7 7

#define PINA _SFR_IO8(0x0C)
#define PINA0 0
#define PINA1 1
#define PINA2 2
#define PINA3 3