#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int map( int input) {
  return (input*255)/1023;
}

int main() {
  while(1){

  srand(time(NULL));
  int input = rand()%1023;
  printf("Analog input: %d\n", input);
  int A0 = map(input);
  printf("Mapped value: %d\n", A0);  


  int i, tmp;
  /* This loop takes the integer in bit form, shifts them n places to the right, performs a bitwise AND on the bit in position 0, and writes out the result */
  printf("Binary value: ");
  for (i = 7; i >= 0; i--)
    {
      tmp = A0 >> i;
      
      if (tmp & 1)
  	{
  	  printf("1");
  	}
      else
  	{
  	  printf("0");
  	}
    }
  printf("\n");
  
  printf("VU value: ");
  if (A0 < 31)
    printf("Led 1\n");
  
  else if (A0 <= 63)
    printf("Led 1 - 2\n");

  else if (A0 <= 95)
    printf("Led 1 - 3\n");

  else if (A0 <= 127)
    printf("Led 1 - 4\n");

  else if (A0 <= 159)
    printf("Led 1 - 5\n");

  else if (A0 <= 191)
    printf("Led 1 - 6\n");

  else if (A0 <= 223)
    printf("Led 1 - 7\n");

  else if (A0 <= 255)
    printf("Led 1 - 8\n");

  else
    printf("This shouldn't happen...");

  printf("\n");


  usleep(1000000);
  }

}



  




/* void  */
/* light_leds(int num) */
/* { */
/*   int i; */
/*   for (i = 0; i < num; i++) */
/*     { */
/*       /\* led[1] = HIGH *\/ */
/*     } */
/* } */
