Go Down

Topic: Byte Digital Output (Read 724 times) previous topic - next topic

elvitena

Hi,

I need to put a Byte in an 8 bits port; I made the follow code but it´s not working as it should for example in pins 30, 31 and 32 I should have a High output (Blinking), but I have high only in 30 and 31.

Please, could somebody help me?
I´m using the Arduino mega 2560

THE CODE

byte b = 00000111;


void setup()
{
  pinMode(30, OUTPUT);
  pinMode(31, OUTPUT);
  pinMode(32, OUTPUT);
  pinMode(33, OUTPUT);
  pinMode(34, OUTPUT);
  pinMode(35, OUTPUT);
  pinMode(36, OUTPUT);
  pinMode(37, OUTPUT);
 
}

void loop()
{
  delay(200);

   digitalWrite(30, bitRead (b, 1));
   digitalWrite(31, bitRead (b, 2));
   digitalWrite(32, bitRead (b, 3));
   digitalWrite(33, bitRead (b, 4));
   digitalWrite(34, bitRead (b, 5));
   digitalWrite(35, bitRead (b, 6));
   digitalWrite(36, bitRead (b, 7));
   digitalWrite(37, bitRead (b, 8));

   delay(200);
   
   digitalWrite(30, LOW);
   digitalWrite(31, LOW);
   digitalWrite(32, LOW);
   digitalWrite(33, LOW);
   digitalWrite(34, LOW);
   digitalWrite(35, LOW);
   digitalWrite(36, LOW);
   digitalWrite(37, LOW);
 
}
 

Fors

The parameter for which bit to read starts at 0 for the least-significant bit, so you have simply shifted everything one bit. The correct code would be:

Code: [Select]
   digitalWrite(30, bitRead (b, 0));
   digitalWrite(31, bitRead (b, 1));
   digitalWrite(32, bitRead (b, 2));
   digitalWrite(33, bitRead (b, 3));
   digitalWrite(34, bitRead (b, 4));
   digitalWrite(35, bitRead (b, 5));
   digitalWrite(36, bitRead (b, 6));
   digitalWrite(37, bitRead (b, 7));

elvitena

I tryed that, but still doesn´t works.

It blinks 30, 33, and 36.

el_supremo

Code: [Select]
byte b = 00000111;
This sets b to the octal value 0111 which is the bit pattern 01001001. What I think you wanted was:
Code: [Select]
byte b = B00000111;

Pete

elvitena

Perfect,

Thank you so much

elvitena

Hi, again.

I´m trying to change the code in order to manipulate ports, I put the following line codes, and comment the code that was working well, and now it doesn´t make anything.


void setup()
{
   DDRD = B11111111;
// pinMode(0, OUTPUT);
// pinMode(1, OUTPUT);
// pinMode(2, OUTPUT);
  //pinMode(3, OUTPUT);
  //pinMode(4, OUTPUT);
// pinMode(5, OUTPUT);
// pinMode(6, OUTPUT);
  //pinMode(7, OUTPUT);
 
}

void loop()
{
  delay(200);
 
   PORTD = B10011100;

  // digitalWrite(0, bitRead (salida, 0));
  // digitalWrite(1, bitRead (salida, 1));
  // digitalWrite(2, bitRead (salida, 2));
  // digitalWrite(3, bitRead (salida, 3));
  // digitalWrite(4, bitRead (salida, 4));
  // digitalWrite(5, bitRead (salida, 5));
  // digitalWrite(6, bitRead (salida, 6));
  // digitalWrite(7, bitRead (salida, 7));

   delay(200);
   
   PORTD = 0;
  // digitalWrite(0, LOW);
  // digitalWrite(1, LOW);
  // digitalWrite(2, LOW);
  // digitalWrite(3, LOW);
  // digitalWrite(4, LOW);
  // digitalWrite(5, LOW);
  // digitalWrite(6, LOW);
  // digitalWrite(7, LOW);
 
}

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy