Starter code not working

Hello everyone, someone tell me what I'm doing wrong for this program not to work.

IDE Arduino 2.1.1 - Arduino UNO


//Input

#define Buton1 PINB0
#define Buton2 PINB1


//Output
#define RL1 PORTD2
#define RL2 PORTD3



void setup() {
  // put your setup code here, to run once:
  DDRB = 0b00000000;
  PORTB = 0b00000011;
  DDRC = 0b00000000;
   DDRD = 0b00011100;
 }

void loop() {
  // put your main code here, to run repeatedly:
  if (digitalRead(Buton1)==1) {
    digitalWrite(RL1, HIGH);
 } else {
   digitalWrite(RL1, LOW);
 }

  if (digitalRead(Buton2)==1) {
    digitalWrite(RL2, HIGH);
 } else {
   digitalWrite(RL2, LOW);
 }
}

Okay, may be possible. If you tell us in what way it doesn't work...

  DDRB = 0b00000000;
  PORTB = 0b00000011;
  DDRC = 0b00000000;
   DDRD = 0b00011100;

Is this undocumented code your own? What does it do? What is its purpose?

DDRB = 0b00000000; // Configure all Port B pins as inputs
PORTB = 0b00111111; // Enable internal pull-ups for pins PB0 to PB5
DDRC = 0b00000000; // Configure all Port C pins as inputs
DDRD = 0b00001100; // Configures Port D pins PD2, PD3 as outputs

void setup() {
  // put your setup code here, to run once:
  DDRB = 0b00000000;
  PORTB = 0b00000011;
  DDRC = 0b00000000;
   DDRD = 0b00011100;
 }

void loop() {
  // put your main code here, to run repeatedly:
  if (digitalRead(8)==1) {
    digitalWrite(2, HIGH);
 } else {
   digitalWrite(2, LOW);
 }

  if (digitalRead(9)==1) {
    digitalWrite(3, HIGH);
 } else {
   digitalWrite(3, LOW);
 }
}

Does this work?

PINB0 is 0 and PINB1 is 1. They are the bit positions in the register for the associated pins. They are NOT the pin numbers that the digitalWrite API expects.

It works the way you envy me.

Now if I define

#define RL1 PORTD2
#define RL2 PD3

And swapping 2 for RL1 and 3 for RL2 also works

Now no it doesn't work if I set the digitalRead

/Input

#define Buton1 8
#define Buton2 9


//Output
#define RL1 2
#define RL2 3



void setup() {
  // put your setup code here, to run once:
  DDRB = 0b00000000;
  PORTB = 0b00000011;
  DDRC = 0b00000000;
   DDRD = 0b00011100;
 }

void loop() {
  // put your main code here, to run repeatedly:
  if (digitalRead(Buton1)==1) {
    digitalWrite(RL1, HIGH);
 } else {
   digitalWrite(RL1, LOW);
 }

  if (digitalRead(Buton2)==1) {
    digitalWrite(RL2, HIGH);
 } else {
   digitalWrite(RL2, LOW);
 }
}

Does this not work? Again, the nomenclature you are trying to use is not appropriate for the API you are trying to use. You have to either use pin numbers like digitalWrite and digitalRead expect or don't use those functions.

/Input

#define Buton1 8
#define Buton2 9


//Output
#define RL1 2
#define RL2 3



void setup() {
  // put your setup code here, to run once:
  pinMode(Buton1, INPUT);
  pinMode(Buton2, INPUT);
  pinMode(RL1, OUTPUT);
  pinMode(RL2, OUTPUT);
 }

void loop() {
  // put your main code here, to run repeatedly:
  if (digitalRead(Buton1)==1) {
    digitalWrite(RL1, HIGH);
 } else {
   digitalWrite(RL1, LOW);
 }

  if (digitalRead(Buton2)==1) {
    digitalWrite(RL2, HIGH);
 } else {
   digitalWrite(RL2, LOW);
 }
}

Is there a particular reason you don't want to use pinMode like normal?

I'm a beginner in the arduino, I made some assembly programs for the atmega328 and I'm more comfortable saying that the led is on PB5 than saying that the led is on 13.

It confused me at times.

I would like to define that the led is on PB5 and that a button is on PB4 and be able to write

digitalRead(PB4) to read the button state and not have to use digitalRead(12)

I would like to be able to play basketball outside in the rain and not get wet.

That's not how digitalWrite and digitalRead work. If you want a function that works that way then you will have to write one.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.