6
\$\begingroup\$

I am new here..

I have two IR sensors that give digital output (0 when there is something in front of it and 1 when there isn't).

I am using a ATxMEGA128 to run motors using these sensors but I am unable to read them simultaneously.

The way I am reading 1 pin is :

if(PORTD.IN & (1<<3))

this works perfectly fine but I do not know how to accept two inputs, For instance if my two sensors are on bit 1 and bit 3?

Can anyone please tell me the syntax?

\$\endgroup\$
  • \$\begingroup\$ Do you want to tell if either of them are on, or if both of them are on? \$\endgroup\$ – Joe Baker Feb 24 '13 at 0:39
  • \$\begingroup\$ i am writing a obstacle avoidance program, so I want to run my motors for different cases, for instance if left has an obstacle, then go right, if both have obstacle then go back and so on.. (Actually I am using 3 sensors, but I just need to know how can I use the outputs from these sensors together) \$\endgroup\$ – Ron Feb 24 '13 at 1:14
10
\$\begingroup\$

Hassan's answer is not quite accurate. If you really want to read both inputs at the same time, you can do that so long as they both reside on the same port.

uint8_t sample = PORTD.IN;
uint8_t bit3   = (sample >> 3) & 1;
uint8_t bit7   = (sample >> 7) & 1;

Now you can be asured that bit3 and bit7 are from the same time sample. If you use PORTD.IN in multiple places, they are all distinct readings from the pins. Read the PORT.IN into a temporary variable and base your decisions on that sample.

\$\endgroup\$
  • \$\begingroup\$ ahhh... good point ;) \$\endgroup\$ – hassan789 Feb 24 '13 at 3:16
  • \$\begingroup\$ what does >> actually mean? So in this case, how would my if statement go? If(1<<bit3 && 1<<bit7) { ... } \$\endgroup\$ – Ron Feb 24 '13 at 23:03
  • \$\begingroup\$ Got it working, Thanks so much! But it would be great if you could tell me what does (sample >> 3) & 1 mean? \$\endgroup\$ – Ron Feb 25 '13 at 0:02
  • \$\begingroup\$ @Ron (x >> 3) means "shift sample to the right by three bits. (y & 1) means "clear all but the least significant bit". In effect this code extracts bit 3 from sample. \$\endgroup\$ – vicatcu Feb 25 '13 at 2:33
2
\$\begingroup\$

I would mask the bits I wanted from the port, and test for the possible combinations in a case statement:

SensorMask    : constant unsigned_8 := 16#88#;
Right_Blocked : constant unsigned_8 := 16#80#;
Left_Blocked  : constant unsigned_8 := 16#08#;
...
case PORTD.IN and SensorMask is
   when SensorMask    => reverse;  
   when Right_Blocked => goLeft;
   when Left_Blocked  => goRight;
   when others        => -- carry on
end case;

In C and presumably the Arduino's own dialect it's called a switch statement and works slightly differently, but it's the usual way of testing several bits at once.

\$\endgroup\$
0
\$\begingroup\$

Never programmed an arduino... but if portd.in is an 8-bit register starting from bit-0 to bit-7 here is your code:

//    Im assuming bit-0 is the least significant bit.. ie:   "((PORTD.IN) & 0x01)"

if (   ((PORTD.IN >> 3) & 0x01) && ((PORTD.IN >> 1) & 0x01)   ) {
     // printf("both bit-3 and bit-1 are TRUE");
}
if (   (PORTD.IN >> 7) & 0x01   ) {
     // printf("bit-7 is TRUE");
}

if (   PORTD.IN & 0x01   ) {
     // printf("bit-0 is TRUE");
}
\$\endgroup\$
  • \$\begingroup\$ no problem, your welcome. have fun! \$\endgroup\$ – hassan789 Feb 24 '13 at 2:57
  • 1
    \$\begingroup\$ These are not necessarily being read at the same time. Most likely the ports are marked volatile so the compiler knows to never assume the value at any given time. Since you use PORTD.IN twice, the compiler will generate two separate reads of the port.Look at the generated assembly, it's most likely not what he wants. \$\endgroup\$ – Jonathon Reinhart Feb 24 '13 at 18:43
  • \$\begingroup\$ actually you are right, using this, its not AND-ing the two inputs but OR-ing the two, i.e. if any one of the inputs goes true, the output goes true! \$\endgroup\$ – Ron Feb 24 '13 at 20:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.