How to compress certain code

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

help.txt (1.17 KB)

mister there is 7 because A B C D E F G thats 7 letters not six what is this scam

Google "arduino 7 segment display library" and give one of those a try.

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

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.

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

if (keypressed == 55)
{
flash(13,11);
}
// or even simpler
if (keypressed == 56) flash(13,10);
if (keypressed == 57) flash(13,9);
// for the cases where only one LED is flashed then use
if (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.

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

it dont work

More information please

depression:
it dont work

That's the computer equivalent of asking the mechanic to fix my car because "I could not get to Glasgow"

...R

it dont work

Then you have done it incorrectly. please post all of your code so we can see where you have gone wrong.

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

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.

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.

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
  }
}

Perehama:

const byte Segment sLUT[7];
const byte Number sLUT[10]

Have you seen your shrink lately? :slight_smile: :slight_smile:

...R

Robin2:
Have you seen your shrink lately? :slight_smile: :slight_smile:

...R

refers to Look Up Table, but I do enjoy the double entendre.