Pages: [1]   Go Down
Author Topic: Efficient Programming  (Read 329 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I believe the technical term for having efficient programming is to NOT have it like below:

Code:
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:
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.
Logged

SE USA
Offline Offline
Faraday Member
**
Karma: 41
Posts: 3783
@ssh0le
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: August 16, 2012, 12:20:21 am by Osgeld » Logged


California
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3442
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4813
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Note that arrays start with element zero.

As for your code:

.... 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
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26509
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
Logged

"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.

Pages: [1]   Go Up
Jump to: