Source

arduino_hack_day / senha.pde

Full commit
/*
 * Copyright (C) 2010 Álvaro Justen <alvaro@justen.eng.br>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 dated June, 1991.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 * 
 * If you find any bugs or have any suggestions, please email me.
 *
 * This software was created on Arduino Hack Days (on 2010-03-28 and 2010-04-01)
 * See more details and authors at: 
 * http://blog.justen.eng.br/2010/03/arduino-hack-n-beer-sunday.html
 * http://blog.justen.eng.br/2010/03/arduino-hack-n-beer-sunday-foi.html
 * http://blog.justen.eng.br/2010/03/arduino-hack-n-beer-day-reloaded.html
 * 
 */


//1 -> VSS
//2 -> VCC
const int RS = 3;
const int RW = 4;
const int E = 5;
const int D[] = { 6, 7, 8, 9, 10, 11, 12, 13 };
const int LED = 13;
//15 - > LED+
//16 - > LED-

int fila = 0;
int pinInputIncrement = 1;
int pinInputRepeat = 2;
int pinSound = 0;

int delay_enable = 5;

void enable() {
  delay(delay_enable);
  digitalWrite(E, 0);
  delay(delay_enable);
  digitalWrite(E, 1);
  delay(delay_enable);
}

void blink_LED(int t) {
  digitalWrite(LED, 1);
  delay(t);
  digitalWrite(LED, 0);
}

void print_pins(char c) {
  digitalWrite(D[7], ((int)c & 0x128) >> 7);
  digitalWrite(D[6], ((int)c & 0x64) >> 6);
  digitalWrite(D[5], ((int)c & 0x32) >> 5);
  digitalWrite(D[4], ((int)c & 0x16) >> 4);
  digitalWrite(D[3], ((int)c & 0x8) >> 3);
  digitalWrite(D[2], ((int)c & 0x4) >> 2);
  digitalWrite(D[1], ((int)c & 0x2) >> 1);
  digitalWrite(D[0], (int)c & 0x1);
}

void setup_lcd() {
  digitalWrite(RW, 0);
  digitalWrite(RS, 0);
  digitalWrite(E, 1);
  
  /* clean up the display */
  print_pins(0x0C); /* 0x0F = 15 = 00001111 */
  enable();
  
  /* enable two lines */
  print_pins(0x3F); /* 0x3F = 63 = 00111111 */
  enable();

  /* enable two lines */
  print_pins(128); /* move cursor to the 1st line */
  enable();
}

void lcd_print(char *text) {
  digitalWrite(RS, 1);
  int size = 0, i;
  while (text[size] != '\0') {
    size++;
  }
  for (i = 0; i < size; i++) {
    if (text[i] == '\n') {
      digitalWrite(RS, 0);
      print_pins(128 + 49 + 7); /* move cursor to the 2nd line */
      enable();
      digitalWrite(RS, 1);
      delay(delay_enable);
    }
    else {
      print_pins(text[i]);
      enable();
    }
  }
}


void piscar_lcd(char *text){
  setup_lcd();
  lcd_print(text);
  delay(100);
  setup_lcd();
  lcd_print("   ");
  delay(50);
  setup_lcd();
  lcd_print(text);
}


void toca_nota(int freq, float time){
  float delay_time = 1000000.0 / (freq);
  float delay_time1 = 0.9 * delay_time;
  float delay_time2 = 0.1 * delay_time;
  for (int i = 0; i < freq * time; i++){
      digitalWrite(pinSound, HIGH);
      delayMicroseconds(delay_time2);
      digitalWrite(pinSound, LOW);
      delayMicroseconds(delay_time1);
  }
}


void vem_ca_filho_da_puta(int senha){
    char senha_str[10];
    sprintf(senha_str, "%d", senha);
    piscar_lcd(senha_str);
    toca_nota(1720, 1);
    toca_nota(880, 1);
}

void setup() {
  // Define o botão de input para o próximo número 
  // como input e coloca 5v nele (por causa do pullup
  // resistor
  pinMode(pinInputIncrement, INPUT);
  digitalWrite(pinInputIncrement, HIGH);
  
  // Faz o mesmo para o pino de repetição do número
  pinMode(pinInputRepeat, INPUT);
  digitalWrite(pinInputRepeat, HIGH);
  
  pinMode(pinSound, OUTPUT);

  // Prepara o LCD
  int i;
  blink_LED(100);
  pinMode(RS, OUTPUT);
  pinMode(RW, OUTPUT);
  pinMode(E, OUTPUT);
  for (i = 0; i < 8; i++) {
    pinMode(D[i], OUTPUT);
  }
  pinMode(LED, OUTPUT);
  
  // Faz uma pequena apresentação
  delay(1000);
  setup_lcd();
  lcd_print("Arduino Hackday");
  delay(1000);
  setup_lcd();
  lcd_print("               ");
}



void loop() {
  if (digitalRead(pinInputIncrement) == LOW){
    fila++;
    vem_ca_filho_da_puta(fila);
  } else if (digitalRead(pinInputRepeat) == LOW) {
    vem_ca_filho_da_puta(fila);
  }
}