What is the best way to simplify this code

Hi.

I have a certain code for LCD 16x2 on Arduino UNO. I want to simplify the code by loop but I am confused.

digitalWrite(D7, LOW);
digitalWrite(D6, HIGH);
digitalWrite(D5, LOW);	
digitalWrite(D4, LOW);
digitalWrite(D3, HIGH);
digitalWrite(D2, HIGH);
digitalWrite(D1, LOW);
digitalWrite(D0, HIGH);

I wanted to make for loop such as

void send_cmd(unsigned char wCmd)
for (int i = 0; i < 8; i++)
	{
		digitalWrite(how to set the pin here?, how to correspond to each bit(LOW or HIGH));
        }

So if the user send “10010101” or send_cmd(10010101), the function will write the first bit to D7 and so on.

Secondly, if the user sends the cmd in HEX(or DEC, OCT, any other format), how will the function handle it?

What Arduino are you using? Most Arduinos do NOT use a D to designate digital pins.

   for(byte b=0; b<8; b++)
   {
      digitalWrite(b, someValue);
   }

someValue could be bitRead(someInteger, b), but it is unlikely that you someInteger = 10010101 will produce the results you expect.

Hi, Can you post your complete code, using code tags please. Are you using a LCD library to control the LCD display? What are D0 thru D7 connected to?

D0 and D1 are used for programming the UNO so you would be advised to avoid using them.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :)

With direct port manipulation on the Uno that would be just

PORTD = 0x4D ;

(although that's simultaneous, not quite the same thing).

If your pins had well chosen names the code you started with might be sensible as it is, I don't know if those pins have related or separate functions, for instance.

In general if you are updating a group of related pins, there should be a function to do it (with a well chosen name).

You could use parallel arrays or a struct. With parallel arrays, it might look like:

// Do not use 0 or 1 as they are RX/TX for Serial object

int pin[] = {  D2,  D3,   D4,   D5,  D6,  D7,   D8,  D9};  
int val[] = {HIGH, LOW, HIGH, HIGH, LOW, LOW, HIGH, LOW};

for (int i = 0; i < (sizeof(pin) / sizeof(pin[0])); i++) {
   digitalWrite(pin[i], val[i]);
}

Damn it. I posted this in hurry that I left the main point. Forgive me.

@TomGeorge & PaulS No no no, they are actually LCD pins.

Okay first here's the thing. I am trying to write the LCD library purely in C style of Arduino. No classes, objects, constructors, etc. Before anyone tells me to stop reinventing the wheel, no I am not. I just want to learn to write my own library and specifically in C. That's all to it.

I know there might be certain people that won't like my idea but hey I am just trying to learn here. And because I am only familiar in C, why not?

Next, the code

#define RS 12
#define EN 11
#define D0 10
#define D1 9
#define D2 8
#define D3 7
#define D4 6
#define D5 5
#define D6 4
#define D7 3

void setup()
{
 pinMode(RS, OUTPUT);
 pinMode(EN, OUTPUT);
 pinMode(D0, OUTPUT);
 pinMode(D1, OUTPUT);
 pinMode(D2, OUTPUT);
 pinMode(D3, OUTPUT);
 pinMode(D4, OUTPUT);
 pinMode(D5, OUTPUT);
 pinMode(D6, OUTPUT);
 pinMode(D7, OUTPUT);

 digitalWrite(RS, LOW);
 digitalWrite(EN, LOW);
 digitalWrite(D0, LOW);
 digitalWrite(D1, LOW);
 digitalWrite(D2, LOW);
 digitalWrite(D3, LOW);
 digitalWrite(D4, LOW);
 digitalWrite(D5, LOW);
 digitalWrite(D6, LOW);
 digitalWrite(D7, LOW);

 delay(500);

 //init LCD 2 lines, 5x7
 digitalWrite(D7, LOW);
 digitalWrite(D6, LOW);
 digitalWrite(D5, HIGH);
 digitalWrite(D4, HIGH);
 digitalWrite(D3, HIGH);
 digitalWrite(D2, LOW);
 digitalWrite(D1, LOW);
 digitalWrite(D0, LOW);

 digitalWrite(RS, LOW);
 digitalWrite(EN, HIGH);
 delay(10);
 digitalWrite(EN, LOW);

 delay(300);

 //display on, cursor on
 digitalWrite(D7, LOW);
 digitalWrite(D6, LOW);
 digitalWrite(D5, LOW);
 digitalWrite(D4, LOW);
 digitalWrite(D3, HIGH);
 digitalWrite(D2, HIGH);
 digitalWrite(D1, HIGH);
 digitalWrite(D0, LOW);

 digitalWrite(RS, LOW);
 digitalWrite(EN, HIGH);
 delay(10);
 digitalWrite(EN, LOW);

        delay(30);

 //clear LCD
 digitalWrite(D7, LOW);
 digitalWrite(D6, LOW);
 digitalWrite(D5, LOW);
 digitalWrite(D4, LOW);
 digitalWrite(D3, LOW);
 digitalWrite(D2, LOW);
 digitalWrite(D1, LOW);
 digitalWrite(D0, HIGH);

 digitalWrite(RS, LOW);
 digitalWrite(EN, HIGH);
 delay(10);
 digitalWrite(EN, LOW);

        delay(30);

 //shift cursor right
 digitalWrite(D7, LOW);
 digitalWrite(D6, LOW);
 digitalWrite(D5, LOW);
 digitalWrite(D4, LOW);
 digitalWrite(D3, LOW);
 digitalWrite(D2, HIGH);
 digitalWrite(D1, HIGH);
 digitalWrite(D0, LOW);

 digitalWrite(RS, LOW);
 digitalWrite(EN, HIGH);
 delay(10);
 digitalWrite(EN, LOW);

        delay(30);

 //line 1, position 6
 digitalWrite(D7, HIGH);
 digitalWrite(D6, LOW);
 digitalWrite(D5, LOW);
 digitalWrite(D4, LOW);
 digitalWrite(D3, LOW);
 digitalWrite(D2, HIGH);
 digitalWrite(D1, HIGH);
 digitalWrite(D0, LOW);

 digitalWrite(RS, LOW);
 digitalWrite(EN, HIGH);
 delay(10);
 digitalWrite(EN, LOW);

        delay(30);

 //write 'M'
 digitalWrite(D7, LOW);
 digitalWrite(D6, HIGH);
 digitalWrite(D5, LOW);
 digitalWrite(D4, LOW);
 digitalWrite(D3, HIGH);
 digitalWrite(D2, HIGH);
 digitalWrite(D1, LOW);
 digitalWrite(D0, HIGH);

 digitalWrite(RS, LOW);
 digitalWrite(EN, HIGH);
 delay(10);
 digitalWrite(EN, LOW);

}

void loop()
{
}

As you can see, the code above has redundant of digitalWrite which can be simplified. The code was written to test if I could write it bit by bit. I wrote that based on Hitachi HD44780 datasheet. The delay however is not exactly as what the datasheet specified. I purposely gave extra delay to what the datasheet said.

@MarkT, Does that mean I can use, i.e PORTD = 10101011 and the first bit will correspond to the MSB, in this case PD7(atmega328P pin)? If so, then that's almost equivalent to writing bits on PIC16/18.

@econjack I was thinking of using arrays too. Thanks for that!