user55350: I need to write a string of number such as 1234567 on a 7 LED display. For example 1 will be lit up on display for 0.5 seconds, it will turn off then 2 will light up and so on. In the end I need to display "-" for 2 seconds. I also added the image of the given task.
The following is the code I wrote.
I would like to ask if it is the correct, and are there any flaws.
  const int LEDa=13;
  const int LEDb=12;
  const int LEDc=11;
  const int LEDd=10;
  const int LEDe=9;
  const int LEDf=8;
  const int LEDg=7;

  int i;
  int a;

  void setup() {

    pinMode(LEDa, OUTPUT);
    pinMode(LEDb, OUTPUT);
    pinMode(LEDc, OUTPUT);
    pinMode(LEDd, OUTPUT);
    pinMode(LEDe, OUTPUT);
    pinMode(LEDf, OUTPUT);
    pinMode(LEDg, OUTPUT);

  }

  void loop() {
    int array[7]= {1,8,0,5,1,0,0};
    for(i=0;i<8;i++){
      a= array[i];
      }
        switch(a){
          case 1:
          digitalWrite(LEDb,HIGH);
          digitalWrite(LEDc,HIGH);
          delay(500);
          digitalWrite(LEDb,LOW);
          digitalWrite(LEDc,LOW);
          break;
          case 2:
          digitalWrite(LEDa,HIGH);
          digitalWrite(LEDb,HIGH);
          digitalWrite(LEDg,HIGH);
          digitalWrite(LEDe,HIGH);
          digitalWrite(LEDd,HIGH);
          delay(500);
          digitalWrite(LEDa,LOW);
          digitalWrite(LEDb,LOW);
          digitalWrite(LEDg,LOW);
          digitalWrite(LEDe,LOW);
          digitalWrite(LEDd,LOW);
          break;
          case 3:
          digitalWrite(LEDa,HIGH);
          digitalWrite(LEDb,HIGH);
          digitalWrite(LEDc,HIGH);
          digitalWrite(LEDg,HIGH);
          digitalWrite(LEDd,HIGH);
          delay(500);
          digitalWrite(LEDa,LOW);
          digitalWrite(LEDb,LOW);
          digitalWrite(LEDc,LOW);
          digitalWrite(LEDg,LOW);
          digitalWrite(LEDd,LOW);
          break;
          case 4:
          digitalWrite(LEDf,HIGH);
          digitalWrite(LEDg,HIGH);
          digitalWrite(LEDb,HIGH);
          digitalWrite(LEDc,HIGH);
          delay(500);
          digitalWrite(LEDf,LOW);
          digitalWrite(LEDg,LOW);
          digitalWrite(LEDb,LOW);
          digitalWrite(LEDc,LOW);
          break;
          case 5:
          digitalWrite(LEDf,HIGH);
          digitalWrite(LEDg,HIGH);
          digitalWrite(LEDd,HIGH);
          digitalWrite(LEDc,HIGH);
          digitalWrite(LEDa,HIGH);
          delay(500);
          digitalWrite(LEDf,LOW);
          digitalWrite(LEDg,LOW);
          digitalWrite(LEDd,LOW);
          digitalWrite(LEDc,LOW);
          digitalWrite(LEDa,LOW);
          break;
          case 6:
          digitalWrite(LEDf,HIGH);
          digitalWrite(LEDg,HIGH);
          digitalWrite(LEDa,HIGH);
          digitalWrite(LEDc,HIGH);
          digitalWrite(LEDe,HIGH);
          digitalWrite(LEDd,HIGH);
          delay(500);
          digitalWrite(LEDf,LOW);
          digitalWrite(LEDg,LOW);
          digitalWrite(LEDa,LOW);
          digitalWrite(LEDc,LOW);
          digitalWrite(LEDe,LOW);
          digitalWrite(LEDd,LOW);
          break;
          case 7:
          digitalWrite(LEDa,HIGH);
          digitalWrite(LEDb,HIGH);
          digitalWrite(LEDc,HIGH);
          delay(500);
          digitalWrite(LEDa,LOW);
          digitalWrite(LEDb,LOW);
          digitalWrite(LEDc,LOW);
          break;
          case 8:
          digitalWrite(LEDa,HIGH);
          digitalWrite(LEDb,HIGH);
          digitalWrite(LEDc,HIGH);
          digitalWrite(LEDd,HIGH);
          digitalWrite(LEDe,HIGH);
          digitalWrite(LEDf,HIGH);
          digitalWrite(LEDg,HIGH);
          delay(500);
          digitalWrite(LEDa,LOW);
          digitalWrite(LEDb,LOW);
          digitalWrite(LEDc,LOW);
          digitalWrite(LEDd,LOW);
          digitalWrite(LEDe,LOW);
          digitalWrite(LEDf,LOW);
          digitalWrite(LEDg,LOW);
          break;
          case 9:
          digitalWrite(LEDa,HIGH);
          digitalWrite(LEDb,HIGH);
          digitalWrite(LEDc,HIGH);
          digitalWrite(LEDd,HIGH);
          digitalWrite(LEDf,HIGH);
          digitalWrite(LEDg,HIGH);
          delay(500);
          digitalWrite(LEDa,LOW);
          digitalWrite(LEDb,LOW);
          digitalWrite(LEDc,LOW);
          digitalWrite(LEDd,LOW);
          digitalWrite(LEDf,LOW);
          digitalWrite(LEDg,LOW);
          break;
          case 0:
          digitalWrite(LEDa,HIGH);
          digitalWrite(LEDb,HIGH);
          digitalWrite(LEDc,HIGH);
          digitalWrite(LEDd,HIGH);
          digitalWrite(LEDe,HIGH);
          delay(500);
          digitalWrite(LEDa,LOW);
          digitalWrite(LEDb,LOW);
          digitalWrite(LEDc,LOW);
          digitalWrite(LEDd,LOW);
          digitalWrite(LEDe,LOW);
          digitalWrite(LEDf,LOW);
          break;
          }
          if(i=8){
            digitalWrite(LEDg,HIGH);
            delay(2000);
            digitalWrite(LEDg,LOW);
            }
          }


user42015: I see 3 errors:

You define the array to have 7 elements, but in the for loop, you count 8 elements, by using i<8 (Array indizes start at 0, so 0 will be the first and 6 will be the last element). Currently you try to access an array element, that does not exist.
The if statement is also not useful here. In your current code you check for i being equal to 8, but it will never be 8 inside of the for loop, because you used the condition i<8. To display the last - after the number, you should put the code after the for loop.
In the same if statement you write i=8, but a single equality sign means assignment. The value 8 will be assigned to the variable i and this assignment is true everytime. If you want to test equality use == (double equality signs)


Tip: You can make your code smaller by defining functions for codes parts, that get repeated often (for example the code to turn off all LEDs).
Tip2: If you want the code to be very efficient, you can change the pins to use one single port. Then you can change all the pins of the port at once, by assigning a single byte to it. You can save these bytes in an array. This will make your code way shorter, but you also need a deeper understanding about port manipulations. You can choose, what you want to do.

55350: <p>I need to write a string of number such as 1234567 on a 7 LED display. For example 1 will be lit up on display for 0.5 seconds, it will turn off then 2 will light up and so on. In the end I need to display "-" for 2 seconds. I also added the image of the given task.<a href="https://i.stack.imgur.com/BwQyI.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BwQyI.jpg" alt="Given Task"></a></p>

<p>The following is the code I wrote.
I would like to ask if it is the correct, and are there any flaws.</p>

<pre><code>  const int LEDa=13;
  const int LEDb=12;
  const int LEDc=11;
  const int LEDd=10;
  const int LEDe=9;
  const int LEDf=8;
  const int LEDg=7;

  int i;
  int a;

  void setup() {

    pinMode(LEDa, OUTPUT);
    pinMode(LEDb, OUTPUT);
    pinMode(LEDc, OUTPUT);
    pinMode(LEDd, OUTPUT);
    pinMode(LEDe, OUTPUT);
    pinMode(LEDf, OUTPUT);
    pinMode(LEDg, OUTPUT);

  }

  void loop() {
    int array[7]= {1,8,0,5,1,0,0};
    for(i=0;i&lt;8;i++){
      a= array[i];
      }
        switch(a){
          case 1:
          digitalWrite(LEDb,HIGH);
          digitalWrite(LEDc,HIGH);
          delay(500);
          digitalWrite(LEDb,LOW);
          digitalWrite(LEDc,LOW);
          break;
          case 2:
          digitalWrite(LEDa,HIGH);
          digitalWrite(LEDb,HIGH);
          digitalWrite(LEDg,HIGH);
          digitalWrite(LEDe,HIGH);
          digitalWrite(LEDd,HIGH);
          delay(500);
          digitalWrite(LEDa,LOW);
          digitalWrite(LEDb,LOW);
          digitalWrite(LEDg,LOW);
          digitalWrite(LEDe,LOW);
          digitalWrite(LEDd,LOW);
          break;
          case 3:
          digitalWrite(LEDa,HIGH);
          digitalWrite(LEDb,HIGH);
          digitalWrite(LEDc,HIGH);
          digitalWrite(LEDg,HIGH);
          digitalWrite(LEDd,HIGH);
          delay(500);
          digitalWrite(LEDa,LOW);
          digitalWrite(LEDb,LOW);
          digitalWrite(LEDc,LOW);
          digitalWrite(LEDg,LOW);
          digitalWrite(LEDd,LOW);
          break;
          case 4:
          digitalWrite(LEDf,HIGH);
          digitalWrite(LEDg,HIGH);
          digitalWrite(LEDb,HIGH);
          digitalWrite(LEDc,HIGH);
          delay(500);
          digitalWrite(LEDf,LOW);
          digitalWrite(LEDg,LOW);
          digitalWrite(LEDb,LOW);
          digitalWrite(LEDc,LOW);
          break;
          case 5:
          digitalWrite(LEDf,HIGH);
          digitalWrite(LEDg,HIGH);
          digitalWrite(LEDd,HIGH);
          digitalWrite(LEDc,HIGH);
          digitalWrite(LEDa,HIGH);
          delay(500);
          digitalWrite(LEDf,LOW);
          digitalWrite(LEDg,LOW);
          digitalWrite(LEDd,LOW);
          digitalWrite(LEDc,LOW);
          digitalWrite(LEDa,LOW);
          break;
          case 6:
          digitalWrite(LEDf,HIGH);
          digitalWrite(LEDg,HIGH);
          digitalWrite(LEDa,HIGH);
          digitalWrite(LEDc,HIGH);
          digitalWrite(LEDe,HIGH);
          digitalWrite(LEDd,HIGH);
          delay(500);
          digitalWrite(LEDf,LOW);
          digitalWrite(LEDg,LOW);
          digitalWrite(LEDa,LOW);
          digitalWrite(LEDc,LOW);
          digitalWrite(LEDe,LOW);
          digitalWrite(LEDd,LOW);
          break;
          case 7:
          digitalWrite(LEDa,HIGH);
          digitalWrite(LEDb,HIGH);
          digitalWrite(LEDc,HIGH);
          delay(500);
          digitalWrite(LEDa,LOW);
          digitalWrite(LEDb,LOW);
          digitalWrite(LEDc,LOW);
          break;
          case 8:
          digitalWrite(LEDa,HIGH);
          digitalWrite(LEDb,HIGH);
          digitalWrite(LEDc,HIGH);
          digitalWrite(LEDd,HIGH);
          digitalWrite(LEDe,HIGH);
          digitalWrite(LEDf,HIGH);
          digitalWrite(LEDg,HIGH);
          delay(500);
          digitalWrite(LEDa,LOW);
          digitalWrite(LEDb,LOW);
          digitalWrite(LEDc,LOW);
          digitalWrite(LEDd,LOW);
          digitalWrite(LEDe,LOW);
          digitalWrite(LEDf,LOW);
          digitalWrite(LEDg,LOW);
          break;
          case 9:
          digitalWrite(LEDa,HIGH);
          digitalWrite(LEDb,HIGH);
          digitalWrite(LEDc,HIGH);
          digitalWrite(LEDd,HIGH);
          digitalWrite(LEDf,HIGH);
          digitalWrite(LEDg,HIGH);
          delay(500);
          digitalWrite(LEDa,LOW);
          digitalWrite(LEDb,LOW);
          digitalWrite(LEDc,LOW);
          digitalWrite(LEDd,LOW);
          digitalWrite(LEDf,LOW);
          digitalWrite(LEDg,LOW);
          break;
          case 0:
          digitalWrite(LEDa,HIGH);
          digitalWrite(LEDb,HIGH);
          digitalWrite(LEDc,HIGH);
          digitalWrite(LEDd,HIGH);
          digitalWrite(LEDe,HIGH);
          delay(500);
          digitalWrite(LEDa,LOW);
          digitalWrite(LEDb,LOW);
          digitalWrite(LEDc,LOW);
          digitalWrite(LEDd,LOW);
          digitalWrite(LEDe,LOW);
          digitalWrite(LEDf,LOW);
          break;
          }
          if(i=8){
            digitalWrite(LEDg,HIGH);
            delay(2000);
            digitalWrite(LEDg,LOW);
            }
          }
</code></pre>
 42015: <p>I see 3 errors:</p>

<ol>
<li><p>You define the array to have 7 elements, but in the <code>for</code> loop, you count 8 elements, by using <code>i&lt;8</code> (Array indizes start at 0, so 0 will be the first and 6 will be the last element). Currently you try to access an array element, that does not exist.</p></li>
<li><p>The <code>if</code> statement is also not useful here. In your current code you check for <code>i</code> being equal to 8, but it will never be 8 inside of the <code>for</code> loop, because you used the condition <code>i&lt;8</code>. To display the last <code>-</code> after the number, you should put the code after the <code>for</code> loop.</p></li>
<li><p>In the same <code>if</code> statement you write <code>i=8</code>, but a single equality sign means assignment. The value 8 will be assigned to the variable <code>i</code> and this assignment is true everytime. If you want to test equality use <code>==</code> (double equality signs)</p></li>
</ol>

<hr>

<p>Tip: You can make your code smaller by defining functions for codes parts, that get repeated often (for example the code to turn off all LEDs).</p>

<p>Tip2: If you want the code to be very efficient, you can change the pins to use one single port. Then you can change all the pins of the port at once, by assigning a single byte to it. You can save these bytes in an array. This will make your code way shorter, but you also need a deeper understanding about port manipulations. You can choose, what you want to do.</p>
