I NEED HELP TRANSLATING THIS CODE

Hi guys,

im so sorry to bother you all but i need to translate this code into registry like PORTD=0b0000000
for example cuz im not really good at it and the code is very simple.

and i have a problem i cant make the LEDs fade in and out i dont know how to do it.

it would be great if anyone could help :slight_smile: :slight_smile:

#include <IRremote.h>

int receiver_pin = 8;
int first_light_pin = 7;
int second_light_pin = 6;
int third_light_pin = 5;
int fourth_light_pin = 4;
int led_case[] = {0,0,0,0,0};
IRrecv receiver(receiver_pin);
decode_results output;

#define code1 48703
#define code2 58359
#define code3 6375
#define code4 25979

void setup()
{
Serial.begin(9600);
receiver.enableIRIn();
pinMode(first_light_pin, OUTPUT);
pinMode(second_light_pin, OUTPUT);
pinMode(third_light_pin, OUTPUT);
pinMode(fourth_light_pin, OUTPUT);
}

void loop()
{
if (receiver.decode(&output))
{
unsigned int value = output.value;
switch(value)
{
case code1:
if(led_case[1] == 1)
{
digitalWrite(first_light_pin, LOW);
led_case[1] = 0;
}
else
{
digitalWrite(first_light_pin, HIGH);
led_case[1] = 1;
}
break;
case code2:
if(led_case[2] == 1)
{
digitalWrite(second_light_pin, LOW);
led_case[2] = 0;
}
else
{
digitalWrite(second_light_pin, HIGH);
led_case[2] = 1;
}
break;
case code3:
if(led_case[3] == 1)
{
digitalWrite(third_light_pin, LOW);
led_case[3] = 0;
}
else
{
digitalWrite(third_light_pin, HIGH);
led_case[3] = 1;
}
break;
case code4:
if(led_case[4] == 1)
{
digitalWrite(fourth_light_pin, LOW);
led_case[4] = 0;
}
else
{
digitalWrite(fourth_light_pin, HIGH);
led_case[4] = 1;
}
break;
}
Serial.println(value);
receiver.resume();
}
}

mundokhalaf:
i need to translate this code into registry like PORTD=0b0000000

Why?

If it is just to get more speed I suggest you first try the digitalWriteFast library. It is a great deal faster than the standard digitalWrite().

...R

PS ... To make it easy for people to help you please modify your post and use the code button </>
codeButton.png

so your code looks like this and is easy to copy to a text editor. See How to use the Forum

Note that you never use led_case[0]. You have allocated 5 elements in your array, so accessing led_case[4] is fine, but you are losing memory for nothing (and given you use only 0 and 1, you don’t need that array to be of type int, a boolean with true or false or even better a bit mask on a byte (since you have only 4 of them) should be enough).

this is a project im working on at school and they insist that we dont use digitalwrite()
they want us to use the register thats why i dont understand it and i need help :frowning:

#include <IRremote.h>

int receiver_pin = 8;
int first_light_pin = 7;
int second_light_pin = 6;
int third_light_pin = 5;
int fourth_light_pin = 4;
int led_case[] = {0,0,0,0,0};
IRrecv receiver(receiver_pin);
decode_results output;

#define code1  48703
#define code2  58359
#define code3  6375
#define code4  25979

void setup()
{
  Serial.begin(9600);
  receiver.enableIRIn();
  pinMode(first_light_pin, OUTPUT);
  pinMode(second_light_pin, OUTPUT);
  pinMode(third_light_pin, OUTPUT);
  pinMode(fourth_light_pin, OUTPUT);
}

void loop()
{
  if (receiver.decode(&output))
  {
    unsigned int value = output.value;
    switch(value)
    {
      case code1:
      if(led_case[1] == 1) 
      {
        digitalWrite(first_light_pin, LOW);
        led_case[1] = 0;
      } 
      else 
      {
        digitalWrite(first_light_pin, HIGH);
        led_case[1] = 1;
      }
      break;
      case code2:
      if(led_case[2] == 1) 
      {
        digitalWrite(second_light_pin, LOW);
        led_case[2] = 0;
      } 
      else 
      {
        digitalWrite(second_light_pin, HIGH);
        led_case[2] = 1;
      }
      break;
      case code3:
      if(led_case[3] == 1) 
      {
        digitalWrite(third_light_pin, LOW);
        led_case[3] = 0;
      } 
      else 
      {
        digitalWrite(third_light_pin, HIGH);
        led_case[3] = 1;
      }
      break;
      case code4:
      if(led_case[4] == 1) 
      {
        digitalWrite(fourth_light_pin, LOW);
        led_case[4] = 0;
      } 
      else 
      {
        digitalWrite(fourth_light_pin, HIGH);
        led_case[4] = 1;
      }
      break;
    }
    Serial.println(value);
    receiver.resume();
  }
}

mundokhalaf:
this is a project im working on at school and they insist that we dont use digitalwrite()
they want us to use the register

Sounds like you weren't paying attention when your teacher explained about port manipulation.

Why not ask your teacher for help? That's what s/he is paid for.

...R

Have you read the reference page on port manipulation?

It appears you are simply toggling an output when the correct code is received, you really don't need an array for that, simply read the current output state and write the inverse state.

You mentioned having the LEDs fade in and our, neither digitalwrite nor direct port manipulation will do that without a good deal more code to modulate the output.

Robin2:
Sounds like you weren't paying attention when your teacher explained about port manipulation.

Why not ask your teacher for help? That's what s/he is paid for.

...R

asked them for help yet i still couldnt figure it out

Read the doc in the link provided by david_2018 in post #5

mundokhalaf:
asked them for help yet i still couldnt figure it out

Then tell your teacher you don't understand and ask for more assistance. Remember, the teacher is paid to work for YOU.

If there is some specific point about the teacher's advice that you don't understand then tell us and we will try to help. However IMHO direct discussion with your teacher will be a lot more effective than asking questions on a Forum.

...R

Let me give it a try an (overly simplified) way for your Arduino UNO board:

  • You have a set of pins you can use in your code

  • Pins are organized in groups, referred to with a 1 letter name.

  • digital pins 0 to 7 are in group D

  • digital pins 8 to 13 are in group B

  • Analog pins A0 to A5 are in group C

  • At every point in time, a pin is in a given state. it can be an INPUT or an OUPUT, it can be HIGH or LOW.

  • On your Arduino UNO board, information about pin state is described in memory (registers) associated to groups of pins. Those registers have 8 bits, each bit of these registers corresponds to a single pin and we have 3 of them known by their names: DDR, PORT and PIN

The DDR register, determines whether the pin is an INPUT or OUTPUT.
The PORT register controls whether the pin is HIGH or LOW,
The PIN register reads the state of INPUT pins set to input with pinMode().

As you have 3 groups, you'll find three registers per group and to not confuse the names, we add the group name at the end.

So in group D, for example you will have DDRD, PORTD and PIND.

Pin association to bits is simple and relate to the position. the digital pin 7 is the most significant bit ("left" side) and pin 0 corresponds to the least significant bit ("right" side) of the register.

DDR and PORT registers may be both written to, and read. PIN registers correspond to the state of inputs and may only be read (more on writing to PIN is advanced level).

So that's what it looks like for group D:
PORTD.pdf (1.61 MB)

Now, if you want to set Arduino pin 0 as input and pins 1 to 7 as outputs, you just have to write 1 and 0 in the right place:

DDRD = 0b11111110;

If you want to set pins 3,5 and 7 HIGH and not touch the others, you can use the OR bitwise operator represented by |

PORTD |= 0b10101000; // sets digital pins 3,5 and 7 HIGH

if you want to read in one go the state of the full group D, you use PIND. if you want to know the state of one of the pins, you can use the AND bitwise operator represented by &

bool pin7_in_Group_D_is_LOW = ((PIND & 0b10000000) == 0); // masking bit 7

That's about it. play with it.

PS: read the doc for not messing around with pin 0 and 1 which are used for Serial Communication

Easy solution: use digitalWriteFast().

MorganS:
Easy solution: use digitalWriteFast().

I suggested that but it seems that it is not allowed. See Reply #3

...R

STILL CANT GET IT TO WORK PLEASE HELP ME

#include <IRremote.h>

int led_case[] = {0, 0, 0, 0, 0};

decode_results output;

#define code1  26775
#define code2  39015
#define code3  45135
#define code4  12495

void setup()
{
  Serial.begin(9600);
  
  IRrecv PORTB=0b00000001;
  DDRD = 0b11110000;
  DDRB = 0b00000000;
  PORTB = 0b00000001;
}



void loop()
{
  if (IRrecv.decode(&output))
  {
    unsigned int value = output.value;
    switch (value)
    {
      case code1:
        if (led_case[1] == 1)
        {
          PORTD = 0b00000000;
          led_case[1] = 0;
        }
        else
        {
          PORTD = 0b10000000;
          led_case[1] = 1;
        }
        break;
      case code2:
        if (led_case[2] == 1)
        {
          PORTD = 0b00000000;
          led_case[2] = 0;
        }
        else
        {
          PORTD = 0b01000000;
          led_case[2] = 1;
        }
        break;
      case code3:
        if (led_case[3] == 1)
        {
          PORTD = 0b00000000;
          led_case[3] = 0;
        }
        else
        {
          PORTD = 0b00100000;
          led_case[3] = 1;
        }
        break;
      case code4:
        if (led_case[4] == 1)
        {
          PORTD = 0b00000000;
          led_case[4] = 0;
        }
        else
        {
          PORTD = 0b00010000;
          led_case[4] = 1;
        }
        break;
    }
    Serial.println(value);
    PORTB.resume();
  }
}[code]

Don't mess with the code for the IR decoder, presumably that worked in your original code, nothing in that needs to change in order to use direct port manipulation for the LEDs. You don't need to be messing with PORTB or DDRB, the IRremote library expects a pin number, and will handle the rest itself.

Once you get the code back to where it will actually compile and run, then you can work on the code for the LEDs. What you have will at least turn LEDs on and off, but not in the way you are expecting, but get it working at least that far then come back if you have more questions.