Numeric Counter

0

I must create a program that commands a hardware in which I have connected in 3 different ports of the arduino a circuit containing 3 leds. The program should raise these LEDs in binary order.

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

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

I have to finish my program's Loop and Setup, and do the documentation, can you help me relate these functions so that 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);
}

    
asked by anonymous 24.08.2018 / 00:30

1 answer

0

You do not need all this paraphernalia to build your accountant!

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

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

A code capable of implementing a 3-bit binary counter using 3 LEDs follows:

#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);
  }
}
    
28.08.2018 / 13:18