mercredi 26 décembre 2018

How to i make a program in arduino to change a variable from 0 to 9, then from 9 to 0 and so on?

I'm currently building a seven segments display and i want it to display all the numbers from 0 to 9, and the viceversa. I've been able to display all the numbers between 0 and 9, but i'm stuck trying to display the ones between 9 and 0. Once it gets to 9, it simply restarts from 0. Can you help me? Thank you so much in advance <3

int G = 3; // G equals to the lower segment
int H = 2; // H equals to the lower-left segment
int x = 0;
int Delay = 500;


void setup() {
pinMode(A, OUTPUT);
pinMode(B, OUTPUT);
pinMode(C, OUTPUT);
pinMode(D, OUTPUT);
pinMode(E, OUTPUT);
pinMode(F, OUTPUT);
pinMode(G, OUTPUT);
pinMode(H, OUTPUT);
}


void loop() {
    if (x < 10) {
        x = x+1;           // This is the part of the code where i'm stuck
        delay(Delay);

   }
    if (x == 10) {                 // in this part of the loop i simply dispaly a certain number on the display based on the value of x
        x = x-10; 
  }
    if(x == 0)  {
        digitalWrite(A, LOW);
        digitalWrite(B, HIGH);  
        digitalWrite(C, HIGH);
        digitalWrite(D, HIGH);
        digitalWrite(E, LOW);
        digitalWrite(F, HIGH);
        digitalWrite(G, HIGH);
        digitalWrite(H, HIGH);
  } 

    if(x == 1)  {
        digitalWrite(A, LOW);
        digitalWrite(B, LOW);
        digitalWrite(C, HIGH);
        digitalWrite(D, LOW);
        digitalWrite(E, LOW);
        digitalWrite(F, HIGH);
        digitalWrite(G, LOW);
        digitalWrite(H, LOW); 
  }

      if(x == 2)  {
        digitalWrite(A, HIGH);
        digitalWrite(B, LOW);
        digitalWrite(C, HIGH);
        digitalWrite(D, HIGH);
        digitalWrite(E, LOW);
        digitalWrite(F, LOW);
        digitalWrite(G, HIGH);
        digitalWrite(H, HIGH);
  }

    if(x == 3)  {
        digitalWrite(A, HIGH);
        digitalWrite(B, LOW);
        digitalWrite(C, HIGH);
        digitalWrite(D, HIGH);
        digitalWrite(E, LOW);
        digitalWrite(F, HIGH);
        digitalWrite(G, HIGH);
        digitalWrite(H, LOW);
  }
    if(x == 4)  {
        digitalWrite(A, HIGH);
        digitalWrite(B, HIGH);
        digitalWrite(C, HIGH);
        digitalWrite(D, LOW);
        digitalWrite(E, LOW);
        digitalWrite(F, HIGH);
        digitalWrite(G, LOW);
        digitalWrite(H, LOW);
  }
    if(x == 5)  {
        digitalWrite(A, HIGH);
        digitalWrite(B, HIGH);
        digitalWrite(C, LOW);
        digitalWrite(D, HIGH);
        digitalWrite(E, LOW);
        digitalWrite(F, HIGH);
        digitalWrite(G, HIGH);
        digitalWrite(H, LOW);
  }
    if(x == 6)  {
        digitalWrite(A, HIGH);
        digitalWrite(B, HIGH);
        digitalWrite(C, LOW);
        digitalWrite(D, HIGH);
        digitalWrite(E, LOW);
        digitalWrite(F, HIGH);
        digitalWrite(G, HIGH);
        digitalWrite(H, HIGH);
  }
    if(x == 7)  {
        digitalWrite(A, LOW);
        digitalWrite(B, LOW);
        digitalWrite(C, HIGH);
        digitalWrite(D, HIGH);
        digitalWrite(E, LOW);
        digitalWrite(F, HIGH);
        digitalWrite(G, LOW);
        digitalWrite(H, LOW);
  }
    if(x == 8)  {
        digitalWrite(A, HIGH);
        digitalWrite(B, HIGH);
        digitalWrite(C, HIGH);
        digitalWrite(D, HIGH);
        digitalWrite(E, LOW);
        digitalWrite(F, HIGH);
        digitalWrite(G, HIGH);
        digitalWrite(H, HIGH);
  }
    if(x == 9)  {
        digitalWrite(A, HIGH);
        digitalWrite(B, HIGH);
        digitalWrite(C, HIGH);
        digitalWrite(D, HIGH);
        digitalWrite(E, LOW);
        digitalWrite(F, HIGH);
        digitalWrite(G, HIGH);
        digitalWrite(H, LOW);
  }
}

Aucun commentaire:

Enregistrer un commentaire