Pages: [1]   Go Down
Author Topic: Byte Digital Output  (Read 455 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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, smiley-cool);

   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);
 
}
 
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I tryed that, but still doesn´t works.

It blinks 30, 33, and 36.
Logged

Offline Offline
Edison Member
*
Karma: 48
Posts: 1615
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pete
Logged

Where are the Nick Gammons of yesteryear?

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Perfect,

Thank you so much
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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);
 
}
Logged

Pages: [1]   Go Up
Jump to: