Commits

Anonymous committed a003e29

Adicionado sketch do 2o AHD: projeto senha-display-buzzer

Comments (0)

Files changed (1)

+# Este código foi escrito no II Arduino Hack Day
+# Está licenciado sob GPLv3:
+# Mais detalhes em: 
+
+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;
+
+int fila = 0;
+int pinInputIncrement = 1;
+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 incrementa_fila(){
+  fila++;
+}
+
+void toca_nota(int freq, float time){
+  float delay_time = 1.0 / (freq * 2);
+  for (int i = 0; i < freq * time; i++){
+    digitalWrite(pinSound, HIGH);
+    delay(delay_time);
+    digitalWrite(pinSound, LOW);
+    delay(delay_time);
+  }
+}
+
+
+void setup() {
+  //
+  pinMode(pinInputIncrement, INPUT);
+  pinMode(pinSound, OUTPUT);
+  digitalWrite(pinInputIncrement, HIGH);
+
+  // 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);
+  
+  delay(1000);
+  setup_lcd();
+  lcd_print("Arduino Hackday");
+  delay(1000);
+  setup_lcd();
+  lcd_print("               ");
+}
+
+
+char fila_str[10];
+
+void loop() {
+  if (digitalRead(pinInputIncrement) == LOW){
+    incrementa_fila();
+    sprintf(fila_str, "%d", fila);
+    piscar_lcd(fila_str);
+    toca_nota(440, 1);
+  }
+}
+