Numeric Counter

Asked

Viewed 169 times

0

I must create a program that commands a hardware in which I have connected on 3 different ports of the Arduino a circuit containing 3 Leds. The program must ascend these leds in binary order.

I have two functions ready, the first function "incrementaDigito" has 4 variables; "base" which refers to the basis on which we are operating, in this example a binary basis, the vector "numero[]", a vector with nths positions that will be used to store the binary value, "k" is the variable used to update the digit of each position of that binary value stored in the vector, and "n" is the number of elements that the vector "número[]" will contain. This function is to update each position of the vector in ascending order, while the "numero" is smaller than the "base" there is an increment in the variable "k" and a false result is returned. Once the "number" is equal to the base, the function assigns zero to "k" and returns a true result.

The function "incrementaNumero" has the variables "base", "numero", "n". the variable is defined "k" as a function of "n", the number of elements in the vector "numero[]". If the previous function returns false nothing happens, when return true there is a decrease in k.

I have to finish the Loop and Setup of my program, and do the documentation, can help me relate these functions so I can finish?

//CÓDIGO

#define DELAY 400
#define NUMERO[0,0,0]
#define BASE 2
# define N 3
bool incrementaDigito (unsigned base, unsigned numero[], unsigned k, unsigned n){
  if (numero < base){
    numero[k]++;
    return false;
  }
  else{
    numero [k] =0;
    return true;
  }
}
void incrementaNumero (unsigned base, unsigned numero[], unsigned n){
  unsigned k = n-1;
  do{
    if(incrementaDigito(base, numero, k, n)==false) break;
    else k--;
  }
  while(k>=0);
}  

int ledPin1 = 11;
int ledPin2 = 12;
int ledPin3 = 13;

void setup()
{
  pinMode(ledPin1 , OUTPUT);
  pinMode(ledPin2 , OUTPUT);
  pinMode(ledPin3 , OUTPUT);
}

void loop()
{
  unsigned numero[];
  for(int i=0;i<2;i++){
    for(int j=0;j<2;j++){
      for(int k=0;k<2;k++){
        if(k==1){
          j++;
          k=0;}
          if(j==1){
            i++;
            j=0;
            k=0;}
              if(i==1){
                i=0;
                j=0;
                k=0;}
      }
    }
  }

  digitalWrite(ledPin1, );
  digitalWrite(ledPin2, );
  digitalWrite(ledPin3, );

  delay(DELAY);
}

Segue imagem do meu circuito

1 answer

0

You don’t need all this paraphernalia to build your accountant!

A simple solution would be to use the bitwise operator & to independently verify the 3 bits less significant of n, for example:

digitalWrite( ledPin1, n & 1 ); /* Bit Menos significativo (LSB) */
digitalWrite( ledPin2, n & 2 );
digitalWrite( ledPin3, n & 4 ); /* Bit Mais significativo (MSB) */

Follow a code capable of implementing a 3-bit binary counter using 3 Leds:

#define DELAY 1000

const int PIN_LED_Q0 = 11;   /* Bit Menos significativo (LSB) */
const int PIN_LED_Q1 = 12;
const int PIN_LED_Q2 = 13;   /* Bit Mais significativo (MSB) */

void setup()
{
  pinMode(PIN_LED_Q0, OUTPUT);
  pinMode(PIN_LED_Q1, OUTPUT);
  pinMode(PIN_LED_Q2, OUTPUT);
}

void exibir( byte n )
{
  digitalWrite( PIN_LED_Q0, n & 1 );
  digitalWrite( PIN_LED_Q1, n & 2 );
  digitalWrite( PIN_LED_Q2, n & 4 );
}

void loop()
{
  for( int i = 0; i < 8; i++ )
  {
    exibir( i );
    delay(DELAY);
  }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.