Go Down

Topic: I NEED HELP TRANSLATING THIS CODE  (Read 328 times) previous topic - next topic

mundokhalaf

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 :) :)


#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();
  }
}

Robin2

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 </>


Code: [Select]
so your code looks like this and is easy to copy to a text editor. See How to use the Forum
Two or three hours spent thinking and reading documentation solves most programming problems.

J-M-L

#2
Apr 19, 2019, 03:49 am Last Edit: Apr 19, 2019, 03:50 am by J-M-L
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).

Hello - Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums.
Bonjour Pas de messages privés SVP, postez dans le forum directement pour que ça profite à tous

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 thats why i dont understand it and i need help :(






Code: [Select]
#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();
  }
}

Robin2

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
Two or three hours spent thinking and reading documentation solves most programming problems.

david_2018

#5
Apr 19, 2019, 01:17 pm Last Edit: Apr 19, 2019, 01:28 pm by david_2018
Have you read the reference page on port manipulation?

https://www.arduino.cc/en/Reference/PortManipulation

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.

mundokhalaf

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

J-M-L

Read the doc in the link provided by david_2018 in post #5
Hello - Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums.
Bonjour Pas de messages privés SVP, postez dans le forum directement pour que ça profite à tous

Robin2

#8
Apr 20, 2019, 05:39 pm Last Edit: Apr 20, 2019, 05:43 pm by Robin2
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
Two or three hours spent thinking and reading documentation solves most programming problems.

J-M-L

#9
Apr 20, 2019, 06:46 pm Last Edit: Apr 20, 2019, 06:51 pm by J-M-L
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:


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:
Code: [Select]
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 |
Code: [Select]
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 &
Code: [Select]
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


Hello - Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums.
Bonjour Pas de messages privés SVP, postez dans le forum directement pour que ça profite à tous

MorganS

Easy solution: use digitalWriteFast().
"The problem is in the code you didn't post."

Robin2

Easy solution: use digitalWriteFast().
I suggested that but it seems that it is not allowed. See Reply #3

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

mundokhalaf


STILL CANT GET IT TO WORK PLEASE HELP ME

Code: [Select]
#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]

david_2018

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.




Go Up