Go Down

### Topic: Efficient Programming (Read 878 times)previous topic - next topic

#### nz-tdm

##### Aug 16, 2012, 06:57 am
I believe the technical term for having efficient programming is to NOT have it like below:

Code: [Select]
`int one = 2;int two = 3;int four = 4;int eight = 5;int button = 8;int pressed;int pressed2;int current = 0;void setup() {  pinMode(one, OUTPUT);  pinMode(two, OUTPUT);  pinMode(four, OUTPUT);  pinMode(eight, OUTPUT);  pinMode(button, INPUT);}void loop() {  pressed = digitalRead(button);  delay(10);  pressed2 = digitalRead(button);  if (pressed == 1) {    if (pressed == pressed2) {      do        pressed = digitalRead(button);      while (pressed == 1);      delay(10);      if (current == 9) {        current = 0;      } else {        current = current + 1;      }    }  }if (current == 0) {  digitalWrite(one, LOW);  digitalWrite(two, LOW);  digitalWrite(four, LOW);  digitalWrite(eight, LOW);} else if (current == 1) {  digitalWrite(one, HIGH);  digitalWrite(two, LOW);  digitalWrite(four, LOW);  digitalWrite(eight, LOW);} else if (current == 2) {  digitalWrite(one, LOW);  digitalWrite(two, HIGH);  digitalWrite(four, LOW);  digitalWrite(eight, LOW);} else if (current == 3) {  digitalWrite(one, HIGH);  digitalWrite(two, HIGH);  digitalWrite(four, LOW);  digitalWrite(eight, LOW);} else if (current == 4) {  digitalWrite(one, LOW);  digitalWrite(two, LOW);  digitalWrite(four, HIGH);  digitalWrite(eight, LOW);} else if (current == 5) {  digitalWrite(one, HIGH);  digitalWrite(two, LOW);  digitalWrite(four, HIGH);  digitalWrite(eight, LOW);} else if (current == 6) {  digitalWrite(one, LOW);  digitalWrite(two, HIGH);  digitalWrite(four, HIGH);  digitalWrite(eight, LOW);} else if (current == 7) {  digitalWrite(one, HIGH);  digitalWrite(two, HIGH);  digitalWrite(four, HIGH);  digitalWrite(eight, LOW);} else if (current == 8) {  digitalWrite(one, LOW);  digitalWrite(two, LOW);  digitalWrite(four, LOW);  digitalWrite(eight, HIGH);} else if (current == 9) {  digitalWrite(one, HIGH);  digitalWrite(two, LOW);  digitalWrite(four, LOW);  digitalWrite(eight, HIGH);}}`

The above code works PERFECTLY fine. It is just an early start at making a binary clock but that is irrelevant at the moment.

What I want to do, is make this part of code SMALLER. This part of code is an example of INEFFICIENT programming.

Code: [Select]
`if (current == 0) {  digitalWrite(one, LOW);  digitalWrite(two, LOW);  digitalWrite(four, LOW);  digitalWrite(eight, LOW);} else if (current == 1) {  digitalWrite(one, HIGH);  digitalWrite(two, LOW);  digitalWrite(four, LOW);  digitalWrite(eight, LOW);} else if (current == 2) {  digitalWrite(one, LOW);  digitalWrite(two, HIGH);  digitalWrite(four, LOW);  digitalWrite(eight, LOW);} else if (current == 3) {  digitalWrite(one, HIGH);  digitalWrite(two, HIGH);  digitalWrite(four, LOW);  digitalWrite(eight, LOW);} else if (current == 4) {  digitalWrite(one, LOW);  digitalWrite(two, LOW);  digitalWrite(four, HIGH);  digitalWrite(eight, LOW);} else if (current == 5) {  digitalWrite(one, HIGH);  digitalWrite(two, LOW);  digitalWrite(four, HIGH);  digitalWrite(eight, LOW);} else if (current == 6) {  digitalWrite(one, LOW);  digitalWrite(two, HIGH);  digitalWrite(four, HIGH);  digitalWrite(eight, LOW);} else if (current == 7) {  digitalWrite(one, HIGH);  digitalWrite(two, HIGH);  digitalWrite(four, HIGH);  digitalWrite(eight, LOW);} else if (current == 8) {  digitalWrite(one, LOW);  digitalWrite(two, LOW);  digitalWrite(four, LOW);  digitalWrite(eight, HIGH);} else if (current == 9) {  digitalWrite(one, HIGH);  digitalWrite(two, LOW);  digitalWrite(four, LOW);  digitalWrite(eight, HIGH);}`

I just want to output a number from 0 to 9 onto 4 LEDs for Binary-Coded-Decimal. What fancy programming trick will make this like 1/10th of its size.

#### Osgeld

#1
##### Aug 16, 2012, 07:12 amLast Edit: Aug 16, 2012, 07:20 am by Osgeld Reason: 1
your looking for arrays and loops

while it really does the exact same thing your letting the computer deal with it

the real answer is outside of the scope of arduino, which kind of pads thing to be more friendly to beginners, but if you get everything on the same AVR port can be set in one call

if you look at http://arduino.cc/it/Hacking/PinMapping168

you will see what pins are assigned to what ports, if you changed pin 8 to pin 7 they would all be on port D and you could do something like

if(current == 7)
{
PORTD = B11100000;
}

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

but that can be bad in the arduino world

also look at the case switch functions, they can be faster than if, if you dont go nutty with them
http://arduino.cc/en/Reference/SwitchCase

so theres a lot to digest, arrays, loops, direct AVR functions, and case functions

#### Arrch

#2
##### Aug 16, 2012, 07:16 am
Arrays and loops are good for situations where you have a lot of similar pins doing similar things.

In this case, it would also benefit you to use bitwise operations to determine which LEDs need to go on for a given number.

#### GoForSmoke

#3
##### Aug 16, 2012, 08:50 am
If you have 2 of the same general thing, use arrays and loops because once coded that way it's not a big deal or mess to go to more than 2.

And once you do that, there's more basics (a few book chapters worth) to learn that can save you a whole lot of time, effort and hair. The time invested pays back many fold.

Have you bookmarked the Arduino Reference page? And the Tutorials page?

This example has the for loop and an array:
http://arduino.cc/en/Tutorial/Array

.... setting up some globals, but they don't -have- to be globals
byte  pin[ 4 ] = { 2,3,4,5 }; // pin numbers in pin[ 0 ] thru pin[ 3 ]
byte  ledOn; // the bits of ledOn can be accessed using bitRead() and bitWrite()

.... setup() here

.... and inside loop()

// I want for example to light up binary 6
ledOn = 6;

for ( int i = 0; i < 4; i++ )
{
digitalWrite( pin[ i ], bitRead( ledOn, i ));
}

.... just feed the loop numbers (0-15 or for BCD, 0-9) in ledOn
.... or maybe you make the for loop into a function, it's up to you
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

#### AWOL

#4
##### Aug 16, 2012, 08:59 am
Quote
I just want to output a number from 0 to 9 onto 4 LEDs for Binary-Coded-Decimal.

bitRead is a very handy tool if you're not comfortable doing bit-twiddling yourself.
Code: [Select]
`for (int i = 0; i < 4; ++i) {  digitalWrite (digitPin [i], bitRead (current, i));}`
Where "digitPin" is an array of pin numbers for your binary digit LEDs.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

Go Up