Go Down

### Topic: How to compress certain code (Read 371 times)previous topic - next topic

#### depression

##### Oct 16, 2018, 01:57 am
Alright so, say you got pins 2 - 8 being used for a 7 segment display, (2=A, 3=B...) Since im new to this stuff, I use the raw coding way which consists of like if input = 1 { digitalWrite(1, HIGH) digitalWrite(3, HIGH) } and so forth, is there a way to compress this?

this is my raw code attached

#1

#### depression

#2
##### Oct 16, 2018, 02:14 am
mister there is 7 because A B C D E F G thats 7 letters not six what is this scam

#### econjack

#3
##### Oct 16, 2018, 02:25 am
Google "arduino 7 segment display library" and give one of those a try.

#### Grumpy_Mike

#4
##### Oct 16, 2018, 08:13 amLast Edit: Oct 16, 2018, 08:17 am by Grumpy_Mike
When you find yourself writing nearly the same thing over and over it is a sign that you are doing something wrong.
This piece of code for example
Code: [Select]
`if (keypressed == 55){digitalWrite(13, HIGH);digitalWrite(11, HIGH); delay(300);  digitalWrite(13, LOW);digitalWrite(11, LOW);}`
Seems to be repeated again and again without changing anything but the pins you use. If you write your own function you can simply pass the pins you want to use into that function making the code a lot cleaner.

Code: [Select]
`void flash(byte pin1, byte pin2){digitalWrite(pin1, HIGH);digitalWrite(pin2, HIGH); delay(300);  digitalWrite(pin1, LOW);digitalWrite(pin2, LOW);}`

Then that code segment becomes:-
Code: [Select]
`if (keypressed == 55){flash(13,11);}// or even simplerif (keypressed == 56) flash(13,10);if (keypressed == 57) flash(13,9);// for the cases where only one LED is flashed then useif (keypressed == 49) flash(13,13);`
The whole code is a lot shorter.

You can even shorten this by using arrays but this is a good start to optimising your code.

#### depression

#5
##### Oct 16, 2018, 03:44 pmLast Edit: Oct 16, 2018, 03:45 pm by depression
if (keypressed == 49) flash(13,13);
if (keypressed == 50) flash(12, 12);
if (keypressed == 51) flash(11,11);
if (keypressed == 52) flash(10,10);
if (keypressed == 53) flash(9,9);
if (keypressed == 54) flash(12,13);
if (keypressed == 55) flash(13,11);
if (keypressed == 56) flash(13,10);
if (keypressed == 57) flash(13,9);
it dont work

#### UKHeliBob

#6
##### Oct 16, 2018, 04:13 pm
Quote
it dont work
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

#### Robin2

#7
##### Oct 16, 2018, 04:21 pm
it dont work
That's the computer equivalent of asking the mechanic to fix my car because "I could not get to Glasgow"

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

#### Grumpy_Mike

#8
##### Oct 16, 2018, 04:53 pm
Quote
it dont work
Then you have done it incorrectly. please post all of your code so we can see where you have gone wrong.

#### Perehama

#9
##### Oct 16, 2018, 08:27 pmLast Edit: Oct 16, 2018, 08:32 pm by Perehama
Alright so, say you got pins 2 - 8 being used for a 7 segment display, (2=A, 3=B...) Since im new to this stuff, I use the raw coding way which consists of like if input = 1 { digitalWrite(1, HIGH) digitalWrite(3, HIGH) } and so forth, is there a way to compress this?
First, you make an array (look up table) with your segment pins.
Code: [Select]
`const byte SegmentsLUT[7] = {2, 3, 4, 5, 6, 7, 8};`
Next, you make an array of bit masks where the binary value corresponds to the on/off state of each segment for a numeral 0-9 with 2^7 representing segment A, 2^1 representing segment G and 2^0 always 0. I have written these in binary notation so the segment value is evident.
Code: [Select]
`const byte NumbersLUT[10] = {B11111100, B01100000, B11011010, B11110010, B01100110, B10110110, B10111110, B11100000, B11111110, B11110110};`
Last, you write a function which when called and passed a value from 0-9 will iterate over the pin array using the bit mask from the number array.
Code: [Select]
`void displayRefresh(byte numeral) {    digitalWrite(CommonPin, LOW);// HIGH for common cathode    for (int i = 0; i < 7; i++) digitalWrite(SegmentsLUT[i], ((NumbersLUT[numeral] & (0x01 << i)) ? LOW : HIGH));// swap HIGH and LOW for common cathode    digitalWrite(CommonPin, HIGH);// LOW for common cathode  }}`
The slender snake hissed as she slithered slowly over the rocks, grass and leaves.

#### Robin2

#10
##### Oct 16, 2018, 09:38 pmLast Edit: Oct 16, 2018, 09:38 pm by Robin2
Code: [Select]
`const byte Segment sLUT[7];`

Code: [Select]
`const byte Number sLUT[10]`
Have you seen your shrink lately?

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

#### Perehama

#11
##### Oct 16, 2018, 10:59 pm
Have you seen your shrink lately?

...R
refers to Look Up Table, but I do enjoy the double entendre.
The slender snake hissed as she slithered slowly over the rocks, grass and leaves.

Go Up