sábado, 22 de março de 2014

Projeto 11: Sensor de temperatura e humidade com o Arduino.


Neste projeto vou mostrar como utilizar, o sensor DHT11, com o Arduino.

O esquema:




O código:

#include <dht.h>

#define dht_dpin A0

dht DHT;

void setup(){
  Serial.begin(9600);
  delay(300);
  Serial.println("Humidade e Temperatura\n\n");
  delay(700);

}

void loop(){

  DHT.read11(dht_dpin);

    Serial.print("Humidade atual = ");
    Serial.print(DHT.humidity);
    Serial.print("%  ");
    Serial.print("Temperature atual = ");
    Serial.print(DHT.temperature);
    Serial.println("C  ");
  delay(800);

}


Referências:

livraria DHT11: http://arduino-info.wikispaces.com/file/view/DHT11.zip/390039522/DHT11.zip


O vídeo:


Espero que gostem!

sábado, 15 de março de 2014

Projeto 10: Controlo de Matriz Led 8x8 + controlador com o Arduino.


Neste projeto vou mostrar como utilizar, a Matriz Led 8x8 e o respetivo controlador, com o Arduino.

O esquema:

As ligações são muito simples:

VCC - 5V
GND - GND
DIN - Pin 4
CLK - Pin5
CS - Pin 5


O código:

// Programa : Scrool horizontal com matriz de leds 8x8
 // Baseado no livro Arduino Basico, de Michael McRoberts
 
 #include <pgmspace.h>
 #include <TimerOne.h>
 
 int DataPin = 4;  //Ligar o pino 4 do Arduino ao DIN do modulo
 int ClockPin = 5; //Ligar o pino 5 do Arduino ao CLK do módulo
 int LoadPin = 6;  //Ligar o pino 6 do Arduinio ao pino CS/Load do módulo
 
 byte buffer[8];
 
 static byte font[][8] PROGMEM = {
 
 {B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000},   
 {B00000100, B00000100, B00000100, B00000100, B00000100, B00000100, B00000000, B00000100},   
 {B00001010, B00001010, B00001010, B00000000, B00000000, B00000000, B00000000, B00000000},   
 {B00000000, B00001010, B00011111, B00001010, B00011111, B00001010, B00011111, B00001010},   
 {B00000111, B00001100, B00010100, B00001100, B00000110, B00000101, B00000110, B00011100},   
 {B00011001, B00011010, B00000010, B00000100, B00000100, B00001000, B00001011, B00010011},   
 {B00000110, B00001010, B00010010, B00010100, B00001001, B00010110, B00010110, B00001001},   
 {B00000100, B00000100, B00000100, B00000000, B00000000, B00000000, B00000000, B00000000},   
 {B00000010, B00000100, B00001000, B00001000, B00001000, B00001000, B00000100, B00000010},   
 {B00001000, B00000100, B00000010, B00000010, B00000010, B00000010, B00000100, B00001000},   
 {B00010101, B00001110, B00011111, B00001110, B00010101, B00000000, B00000000, B00000000},   
 {B00000000, B00000000, B00000100, B00000100, B00011111, B00000100, B00000100, B00000000},   
 {B00000000, B00000000, B00000000, B00000000, B00000000, B00000110, B00000100, B00001000},   
 {B00000000, B00000000, B00000000, B00000000, B00001110, B00000000, B00000000, B00000000},   
 {B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000100},   
 {B00000001, B00000010, B00000010, B00000100, B00000100, B00001000, B00001000, B00010000},   
 {B00001110, B00010001, B00010011, B00010001, B00010101, B00010001, B00011001, B00001110},   
 {B00000100, B00001100, B00010100, B00000100, B00000100, B00000100, B00000100, B00011111},   
 {B00001110, B00010001, B00010001, B00000010, B00000100, B00001000, B00010000, B00011111},   
 {B00001110, B00010001, B00000001, B00001110, B00000001, B00000001, B00010001, B00001110},   
 {B00010000, B00010000, B00010100, B00010100, B00011111, B00000100, B00000100, B00000100},   
 {B00011111, B00010000, B00010000, B00011110, B00000001, B00000001, B00000001, B00011110},   
 {B00000111, B00001000, B00010000, B00011110, B00010001, B00010001, B00010001, B00001110},   
 {B00011111, B00000001, B00000001, B00000001, B00000010, B00000100, B00001000, B00010000},   
 {B00001110, B00010001, B00010001, B00001110, B00010001, B00010001, B00010001, B00001110},   
 {B00001110, B00010001, B00010001, B00001111, B00000001, B00000001, B00000001, B00000001},   
 {B00000000, B00000100, B00000100, B00000000, B00000000, B00000100, B00000100, B00000000},   
 {B00000000, B00000100, B00000100, B00000000, B00000000, B00000100, B00000100, B00001000},   
 {B00000001, B00000010, B00000100, B00001000, B00001000, B00000100, B00000010, B00000001},   
 {B00000000, B00000000, B00000000, B00011110, B00000000, B00011110, B00000000, B00000000},   
 {B00010000, B00001000, B00000100, B00000010, B00000010, B00000100, B00001000, B00010000},   
 {B00001110, B00010001, B00010001, B00000010, B00000100, B00000100, B00000000, B00000100},   
 {B00001110, B00010001, B00010001, B00010101, B00010101, B00010001, B00010001, B00011110},   
 {B00001110, B00010001, B00010001, B00010001, B00011111, B00010001, B00010001, B00010001},   
 {B00011110, B00010001, B00010001, B00011110, B00010001, B00010001, B00010001, B00011110},   
 {B00000111, B00001000, B00010000, B00010000, B00010000, B00010000, B00001000, B00000111},   
 {B00011100, B00010010, B00010001, B00010001, B00010001, B00010001, B00010010, B00011100},  
 {B00011111, B00010000, B00010000, B00011110, B00010000, B00010000, B00010000, B00011111},   
 {B00011111, B00010000, B00010000, B00011110, B00010000, B00010000, B00010000, B00010000},   
 {B00001110, B00010001, B00010000, B00010000, B00010111, B00010001, B00010001, B00001110},   
 {B00010001, B00010001, B00010001, B00011111, B00010001, B00010001, B00010001, B00010001},   
 {B00011111, B00000100, B00000100, B00000100, B00000100, B00000100, B00000100, B00011111},   
 {B00011111, B00000100, B00000100, B00000100, B00000100, B00000100, B00010100, B00001000},   
 {B00010001, B00010010, B00010100, B00011000, B00010100, B00010010, B00010001, B00010001},   
 {B00010000, B00010000, B00010000, B00010000, B00010000, B00010000, B00010000, B00011111},   
 {B00010001, B00011011, B00011111, B00010101, B00010001, B00010001, B00010001, B00010001},   
 {B00010001, B00011001, B00011001, B00010101, B00010101, B00010011, B00010011, B00010001},   
 {B00001110, B00010001, B00010001, B00010001, B00010001, B00010001, B00010001, B00001110},   
 {B00011110, B00010001, B00010001, B00011110, B00010000, B00010000, B00010000, B00010000},   
 {B00001110, B00010001, B00010001, B00010001, B00010001, B00010101, B00010011, B00001111},   
 {B00011110, B00010001, B00010001, B00011110, B00010100, B00010010, B00010001, B00010001},   
 {B00001110, B00010001, B00010000, B00001000, B00000110, B00000001, B00010001, B00001110},   
 {B00011111, B00000100, B00000100, B00000100, B00000100, B00000100, B00000100, B00000100},   
 {B00010001, B00010001, B00010001, B00010001, B00010001, B00010001, B00010001, B00001110},   
 {B00010001, B00010001, B00010001, B00010001, B00010001, B00010001, B00001010, B00000100},   
 {B00010001, B00010001, B00010001, B00010001, B00010001, B00010101, B00010101, B00001010},   
 {B00010001, B00010001, B00001010, B00000100, B00000100, B00001010, B00010001, B00010001},   
 {B00010001, B00010001, B00001010, B00000100, B00000100, B00000100, B00000100, B00000100},   
 {B00011111, B00000001, B00000010, B00000100, B00001000, B00010000, B00010000, B00011111},   
 {B00001110, B00001000, B00001000, B00001000, B00001000, B00001000, B00001000, B00001110},   
 {B00010000, B00001000, B00001000, B00000100, B00000100, B00000010, B00000010, B00000001},   
 {B00001110, B00000010, B00000010, B00000010, B00000010, B00000010, B00000010, B00001110},   
 {B00000100, B00001010, B00010001, B00000000, B00000000, B00000000, B00000000, B00000000},   
 {B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00011111},   
 {B00001000, B00000100, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000},   
 {B00000000, B00000000, B00000000, B00001110, B00010010, B00010010, B00010010, B00001111},   
 {B00000000, B00010000, B00010000, B00010000, B00011100, B00010010, B00010010, B00011100},   
 {B00000000, B00000000, B00000000, B00001110, B00010000, B00010000, B00010000, B00001110},   
 {B00000000, B00000001, B00000001, B00000001, B00000111, B00001001, B00001001, B00000111},   
 {B00000000, B00000000, B00000000, B00011100, B00010010, B00011110, B00010000, B00001110},   
 {B00000000, B00000011, B00000100, B00000100, B00000110, B00000100, B00000100, B00000100},   
 {B00000000, B00001110, B00001010, B00001010, B00001110, B00000010, B00000010, B00001100},   
 {B00000000, B00010000, B00010000, B00010000, B00011100, B00010010, B00010010, B00010010},   
 {B00000000, B00000000, B00000100, B00000000, B00000100, B00000100, B00000100, B00000100},   
 {B00000000, B00000010, B00000000, B00000010, B00000010, B00000010, B00000010, B00001100},   
 {B00000000, B00010000, B00010000, B00010100, B00011000, B00011000, B00010100, B00010000},   
 {B00000000, B00010000, B00010000, B00010000, B00010000, B00010000, B00010000, B00001100},   
 {B00000000, B00000000, B00000000, B00001010, B00010101, B00010001, B00010001, B00010001},   
 {B00000000, B00000000, B00000000, B00010100, B00011010, B00010010, B00010010, B00010010},   
 {B00000000, B00000000, B00000000, B00001100, B00010010, B00010010, B00010010, B00001100},   
 {B00000000, B00011100, B00010010, B00010010, B00011100, B00010000, B00010000, B00010000},   
 {B00000000, B00001110, B00010010, B00010010, B00001110, B00000010, B00000010, B00000001},   
 {B00000000, B00000000, B00000000, B00001010, B00001100, B00001000, B00001000, B00001000},   
 {B00000000, B00000000, B00001110, B00010000, B00001000, B00000100, B00000010, B00011110},   
 {B00000000, B00010000, B00010000, B00011100, B00010000, B00010000, B00010000, B00001100},   
 {B00000000, B00000000, B00000000, B00010010, B00010010, B00010010, B00010010, B00001100},   
 {B00000000, B00000000, B00000000, B00010001, B00010001, B00010001, B00001010, B00000100},   
 {B00000000, B00000000, B00000000, B00010001, B00010001, B00010001, B00010101, B00001010},   
 {B00000000, B00000000, B00000000, B00010001, B00001010, B00000100, B00001010, B00010001},   
 {B00000000, B00000000, B00010001, B00001010, B00000100, B00001000, B00001000, B00010000},   
 {B00000000, B00000000, B00000000, B00011111, B00000010, B00000100, B00001000, B00011111},   
 {B00000010, B00000100, B00000100, B00000100, B00001000, B00000100, B00000100, B00000010},   
 {B00000100, B00000100, B00000100, B00000100, B00000100, B00000100, B00000100, B00000100},   
 {B00001000, B00000100, B00000100, B00000100, B00000010, B00000100, B00000100, B00001000},   
 {B00000000, B00000000, B00000000, B00001010, B00011110, B00010100, B00000000, B00000000}   
 };  
 
 void clearDisplay() {
     for (byte x=0; x<8; x++) {
         buffer[x] = B00000000;
     }
     screenUpdate();
 }
 
 void initMAX7219() {
     pinMode(DataPin, OUTPUT);
     pinMode(LoadPin, OUTPUT);
     pinMode(ClockPin, OUTPUT);
     clearDisplay();
     writeData(B00001011, B00000111);
     writeData(B00001001, B00000000);
     writeData(B00001100, B00000001);
     intensity(1);
 }
 
 void intensity(int intensity) {
     writeData(B00001010, intensity);
     }
 
 void writeData(byte MSB, byte LSB) {
     byte mask;
     digitalWrite(LoadPin, LOW);
     
     for (mask = B10000000; mask>0; mask >>= 1) {
         digitalWrite(ClockPin, LOW);
         if (MSB & mask){
             digitalWrite(DataPin,HIGH);
         }
         else{
             digitalWrite(DataPin,LOW);
         }
         digitalWrite(ClockPin, HIGH);
     }
     
      for (mask = B10000000; mask>0; mask >>= 1) {
         digitalWrite(ClockPin, LOW);
         if (LSB & mask){
             digitalWrite(DataPin,HIGH);
         }
         else{
             digitalWrite(DataPin,LOW);
         }
         digitalWrite(ClockPin, HIGH);
     }
     digitalWrite(LoadPin, HIGH);
     digitalWrite(ClockPin, LOW);
 }
 
 
 void scroll(char myString[], int speed) {
     byte firstChrRow, secondChrRow;
     byte ledOutput;
     byte chrPointer = 0;
     byte Char1, Char2;
     byte scrollBit = 0;
     byte strLength = 0;
     unsigned long time;
     unsigned long counter;
 
     
     while (myString[strLength]) {strLength++;}
 
     counter = millis();
      while (chrPointer < (strLength-1)) {
         time = millis();
         if (time > (counter + speed)) {
             Char1 = myString[chrPointer];
             Char2 = myString[chrPointer+1];
             for (byte y= 0; y<8; y++) {
                 firstChrRow = pgm_read_byte(&font[Char1 - 32][y]);
                 secondChrRow = (pgm_read_byte(&font[Char2 - 32][y])) << 1;
                 ledOutput = (firstChrRow << scrollBit) | (secondChrRow >>
  (8 - scrollBit) );
                 buffer[y] = ledOutput;
             }
             scrollBit++;
             if (scrollBit > 6) {
             scrollBit = 0;
             chrPointer++;
             }
             counter = millis();
         }
     }
 }
 
 void screenUpdate() {
     for (byte row = 0; row < 8; row++) {
         writeData(row+1, buffer[row]);
     }
 }
 
 void setup() {
     initMAX7219();
     Timer1.initialize(10000);    
     Timer1.attachInterrupt(screenUpdate);
 }
 
 void loop() {
     clearDisplay();  
     scroll(" Jacinto, Joao e Cristina ", 100); //Exibe a mensagem, com intervalo de scroll de 100
     scroll(" 15 - 03 - 2014 ", 100);        //Adicione mais linhas para mostrar outros caracteres
 }


Referências:
- pgmspace.h já vem instalada com o Arduino
- TimerOne.h download em http://www.pjrc.com/teensy/arduino_libraries/TimerOne.zip


O vídeo:



Espero que gostem!

sábado, 8 de março de 2014

Projeto 9: Controlo de Servo Motor com Bluetooth e o Arduino.


Neste projeto vou mostrar como utilizar, o Bluetooth num telemóvel Android, para controlar um Servo Motor, com o Arduino.

O esquema:


Resistências: 220 ohms (à esquerda da imagem) e 330 ohms (a da direita da imagem).

O código:

//Programa : Controle de servo pelo Android utilizando Bluetooth  
   
 #include <Servo.h>  
   
 Servo myservo;  
   
 int val;  
   
 void setup()  
 {  
  //Pino de dados do servo ligado na porta 4  
  myservo.attach(4);  
  Serial.begin(9600);  
 }  
    
 void loop()  
 {  
   if(Serial.available()>=2)  
   {  
    int key=Serial.read();  
    int val=Serial.read();  
    myservo.write(val);  
   }  
 }


Após efetuar a montagem das componentes, tal como descrito no esquema, carregue o programa para o Arduino. No entanto, antes desconecte o VCC do módulo Bluetooth, para evitar qualquer conflito no momento da transmissão pelo computador.

Após o processo estar concluído volte a ligar o VCC.


Configurar o Android:

Com o Android vamos criar uma aplicação, com um botão deslizante (como se fosse um potenciómetro) para o controlo do servo motor.

1º - baixar e instalar, do Google Play, o aplicativo Microcontroller BT.
2º - Execute o Microcontroller BT. Clique no botão Menu do telemóvel e escolha a opção New Layout.

 3º - Dê um nome ao novo Layout (por exemplo: Arduino Servo).
 4º - Surgirá o nome do layout do lado esquerdo.
 5º - Clique novamente no Menu do telemóvel e seguidamente em Edit Mode.
 6º - O modo de edição é caracterizado pela cor vermelha. Clique no botão deslizante (ver a seta):
 7º - Configure as propriedades do botão deslizante: "Analog Input e 5" e "Max = 180" (máx. graus de rotação).
 8º - "Done" para finalizar o modo de edição.
 9º - Clique uma vez mais no Menu do telemóvel e depois em Connect.
10º - Escolha o dispositivo correspondente ao módulo Bluetooth (no meu caso é HC06). A password por defeito é "1234".

11º - E agora é só pressionar o botão deslizante no Android e observar o servo motor!


O vídeo:

Espero que gostem!


terça-feira, 4 de março de 2014

Projeto 8: O teclado matricial (membrana 4x4) com o Arduino.


Neste projeto vou mostrar como utilizar, de forma muito simples, um teclado matricial 4x4, do tipo membrana adesiva, com o Arduino.



O esquema:



Ligações (pela ordem da imagem de cima) no Arduino (Mega)

2, 3, 4, 5    6, 7, 8, 9


O vídeo:



O código:

#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {2, 3, 4, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {6, 7, 8, 9}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

void setup(){
  Serial.begin(57600);
}
 
void loop(){
  char customKey = customKeypad.getKey();
 
  if (customKey){
    Serial.println(customKey);
  }
}

Espero que gostem!
Projeto 7: O módulo de relays com o Arduino.

Neste projeto vou mostrar como utilizar, de forma muito simples, um módulo de relés, para o controlo de uma lâmpada com o Arduino.
O esquema:




O vídeo:


O código:

// Basic 4 Realy board connection
// Each relay is turned on for 2 seconds and then off.
// You can here them click as there state changes from off to on and on to
// off.
// You will also see the corresponding Red LED on the 4 Relay board
// light up when the relay is on.

 //  define names for the 4 Digital pins On the Arduino 7,8,9,10
 //  These data pins link to 4 Relay board pins IN1, IN2, IN3, IN4

#define RELAY1  6                      
#define RELAY2  7                      
#define RELAY3  8                      
#define RELAY4  9

void setup()
{  
// Initialise the Arduino data pins for OUTPUT
  pinMode(RELAY1, OUTPUT);      
  pinMode(RELAY2, OUTPUT);
  pinMode(RELAY3, OUTPUT);
  pinMode(RELAY4, OUTPUT);
}

 void loop()
{
   digitalWrite(RELAY1,LOW);           // Turns ON Relays 1
   delay(2000);                                      // Wait 2 seconds
   digitalWrite(RELAY1,HIGH);          // Turns Relay Off

   digitalWrite(RELAY2,LOW);           // Turns ON Relays 2
   delay(2000);                                      // Wait 2 seconds
   digitalWrite(RELAY2,HIGH);          // Turns Relay Off

   digitalWrite(RELAY3,LOW);           // Turns ON Relays 3
   delay(2000);                                      // Wait 2 seconds
   digitalWrite(RELAY3,HIGH);          // Turns Relay Off

   digitalWrite(RELAY4,LOW);           // Turns ON Relays 4
   delay(2000);                                      // Wait 2 seconds
   digitalWrite(RELAY4,HIGH);          // Turns Relay Off      
 }



Espero que gostem!

segunda-feira, 3 de março de 2014

Projeto 6: O Arduino e o módulo de Relógio RTC. 

Neste projeto vou mostrar como utilizar, de forma muito simples, um módulo de relógio RTC DS1307, com o Arduino.
Módulo RTC DS1307

O esquema:





O vídeo:


O código:

// Date and time functions using a DS1307 RTC connected via I2C and Wire lib

#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 rtc;

void setup () {
  Serial.begin(9600);
#ifdef AVR
  Wire.begin();
#else
  Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
#endif
  rtc.begin();

  if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(__DATE__, __TIME__));
  }
}

void loop () {
    DateTime now = rtc.now();
 
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
 
    Serial.print(" since midnight 1/1/1970 = ");
    Serial.print(now.unixtime());
    Serial.print("s = ");
    Serial.print(now.unixtime() / 86400L);
    Serial.println("d");
 
    // calculate a date which is 7 days and 30 seconds into the future
    DateTime future (now.unixtime() + 7 * 86400L + 30);
 
    Serial.print(" now + 7d + 30s: ");
    Serial.print(future.year(), DEC);
    Serial.print('/');
    Serial.print(future.month(), DEC);
    Serial.print('/');
    Serial.print(future.day(), DEC);
    Serial.print(' ');
    Serial.print(future.hour(), DEC);
    Serial.print(':');
    Serial.print(future.minute(), DEC);
    Serial.print(':');
    Serial.print(future.second(), DEC);
    Serial.println();
 
    Serial.println();
    delay(3000);
}


Referências:

Livraria: https://github.com/adafruit/RTClib

Espero que gostem!
Projeto 5: Controlo de um Servo Motor com o Arduino 

Neste projeto vou mostrar como utilizar, de forma muito simples, um Servo Motor, com o Arduino.


Para este projeto, além do Servo Motor, utilizei também um potenciómetro 100KA.


O esquema:



O vídeo:


O código:

// Projeto 5

#include <Servo.h>
Servo servo1; // Create a servo object

void setup()
{
servo1.attach(5); // Attaches the servo on Pin 5 to the servo object
}

void loop()
{
int angle = analogRead(0); // Read the pot value
angle=map(angle, 0, 1023, 0, 180); // Map the values from 0 to 180 degrees
servo1.write(angle); // Write the angle to the servo
delay(15); // Delay of 15ms to allow servo to reach position
}


Espero que gostem!
Projeto 4: Controlo do Buzzer com o Arduino 

Neste projeto vou mostrar como utilizar, de forma muito simples, o Buzzer (5V), com o Arduino. Para tal, vou recorrer a um tema muito conhecido do cinema, associado ao agente James Bond 007.

Buzzer 5V

Buzzer 5V

O esquema:






O vídeo:


O código:

#include "pitches.h"
#define NO_SOUND 0 // make the rests in music

//array of notes
int melody[] = {
  /*NOTE_G4,NOTE_G4,NO_SOUND,NOTE_G4,NOTE_G4,NO_SOUND,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_G4,
   NOTE_B3,NOTE_G3,NOTE_C4,NOTE_G3,NOTE_CS4,NOTE_G3,NOTE_C4,NOTE_G3,NOTE_B3,NOTE_G3,NOTE_C4,NOTE_G3,NOTE_CS4,NOTE_G3,NOTE_C4,NOTE_G3,
   NOTE_E4,NOTE_F4,NOTE_F4,NOTE_F4,NOTE_F4,NOTE_E4,NOTE_E4,NOTE_E4,
   NOTE_E4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_E4,NOTE_E4,NOTE_E4,*/
   //Introduction
  NOTE_E4,NOTE_F4,NOTE_F4,NOTE_F4,NOTE_F4,NOTE_E4,NOTE_E4,NOTE_E4,
  NOTE_E4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_E4,NOTE_E4,NOTE_E4,
  NOTE_E4,NOTE_F4,NOTE_F4,NOTE_F4,NOTE_F4,NOTE_E4,NOTE_E4,NOTE_E4,
  NOTE_E4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_E4,NOTE_E4,NOTE_E4,
  NOTE_DS5,NOTE_D5,NOTE_B4,NOTE_A4,NOTE_B4,
  NOTE_E4,NOTE_G4,NOTE_DS5,NOTE_D5,NOTE_G4,NOTE_B4,
  NOTE_B4,NOTE_FS5,NOTE_F5,NOTE_B4,NOTE_D5,NOTE_AS5,
  NOTE_A5,NOTE_F5,NOTE_A5,NOTE_DS6,NOTE_D6,NO_SOUND
};

// note duration: 1 = whole note, 2 = half note, 4 = quarter note, 8 = eighth note, etc.
int noteDurations[] = {
  /*8,8,2,8,8,2,16,8,16,8,8,2,4,2,4,2,4,2,4,2,4,2,4,2,4,2,4,8,16,16,8,4,8,8,8,
   8,16,16,8,4,8,8,8,*/

  8,16,16,8,4,8,8,8,
  8,16,16,8,4,8,8,8,
  8,16,16,8,4,8,8,8,
  8,16,16,8,4,8,8,8,
  8,2,8,8,1,
  8,4,8,4,8,8,
  8,8,4,8,4,8,
  4,8,4,8,3
};

int pace = 1450; // change pace of music("speedy")
void setup() {
  for (int Note = 0; Note <54; Note++) {//counter of Notes (54 limit the array)
    int duration = pace/noteDurations[Note];//Adjust duration with the pace of music
    tone(8, melody[Note],duration); //Play note

// to distinguish the notes, set a minimum time between them.
    delay(duration*1.2);
  }
}

void loop() {
  //to repeat song, push reset button.
}
//


Referências:

Crie um novo separador com a designação "pitches.h" e copie o seguinte código:



/*************************************************

* Public Constants

*************************************************/


#define NOTE_B0 31

#define NOTE_C1 33

#define NOTE_CS1 35

#define NOTE_D1 37

#define NOTE_DS1 39

#define NOTE_E1 41

#define NOTE_F1 44

#define NOTE_FS1 46

#define NOTE_G1 49

#define NOTE_GS1 52

#define NOTE_A1 55

#define NOTE_AS1 58

#define NOTE_B1 62

#define NOTE_C2 65

#define NOTE_CS2 69

#define NOTE_D2 73

#define NOTE_DS2 78

#define NOTE_E2 82

#define NOTE_F2 87

#define NOTE_FS2 93

#define NOTE_G2 98

#define NOTE_GS2 104

#define NOTE_A2 110

#define NOTE_AS2 117

#define NOTE_B2 123

#define NOTE_C3 131

#define NOTE_CS3 139

#define NOTE_D3 147

#define NOTE_DS3 156

#define NOTE_E3 165

#define NOTE_F3 175

#define NOTE_FS3 185

#define NOTE_G3 196

#define NOTE_GS3 208

#define NOTE_A3 220

#define NOTE_AS3 233

#define NOTE_B3 247

#define NOTE_C4 262

#define NOTE_CS4 277

#define NOTE_D4 294

#define NOTE_DS4 311

#define NOTE_E4 330

#define NOTE_F4 349

#define NOTE_FS4 370

#define NOTE_G4 392

#define NOTE_GS4 415

#define NOTE_A4 440

#define NOTE_AS4 466

#define NOTE_B4 494

#define NOTE_C5 523

#define NOTE_CS5 554

#define NOTE_D5 587

#define NOTE_DS5 622

#define NOTE_E5 659

#define NOTE_F5 698

#define NOTE_FS5 740

#define NOTE_G5 784

#define NOTE_GS5 831

#define NOTE_A5 880

#define NOTE_AS5 932

#define NOTE_B5 988

#define NOTE_C6 1047

#define NOTE_CS6 1109

#define NOTE_D6 1175

#define NOTE_DS6 1245

#define NOTE_E6 1319

#define NOTE_F6 1397

#define NOTE_FS6 1480

#define NOTE_G6 1568

#define NOTE_GS6 1661

#define NOTE_A6 1760

#define NOTE_AS6 1865

#define NOTE_B6 1976

#define NOTE_C7 2093

#define NOTE_CS7 2217

#define NOTE_D7 2349

#define NOTE_DS7 2489

#define NOTE_E7 2637

#define NOTE_F7 2794

#define NOTE_FS7 2960

#define NOTE_G7 3136

#define NOTE_GS7 3322

#define NOTE_A7 3520

#define NOTE_AS7 3729

#define NOTE_B7 3951

#define NOTE_C8 4186

#define NOTE_CS8 4435

#define NOTE_D8 4699

#define NOTE_DS8 4978


Espero que gostem!

domingo, 2 de março de 2014

Projeto 3: Sensor Ultrasonic HC-SR04 

Neste projeto vou mostrar como utilizar, de forma muito simples, o Sensor Ultrasonic HC-SR04.


Sensor Ultrasonic HC-SR04

HC-SR04

O esquema:




O vídeo:



O código:

#include "Ultrasonic.h"
#define echoPin 13 //Pino 13 recebe o pulso do echo
#define trigPin 12 //Pino 12 envia o pulso para gerar o echo
//iniciando a função e passando os pinos
Ultrasonic ultrasonic(12,13);

void setup()
{
   Serial.begin(9600); //inicia a porta serial
   pinMode(echoPin, INPUT); // define o pino 13 como entrada (recebe)
   pinMode(trigPin, OUTPUT); // define o pino 12 como saida (envia)
}

void loop()
{
  //seta o pino 12 com um pulso baixo "LOW" ou desligado ou ainda 0
    digitalWrite(trigPin, LOW);
  // delay de 2 microssegundos
    delayMicroseconds(2);
  //seta o pino 12 com pulso alto "HIGH" ou ligado ou ainda 1
    digitalWrite(trigPin, HIGH);
  //delay de 10 microssegundos
    delayMicroseconds(10);
  //seta o pino 12 com pulso baixo novamente
    digitalWrite(trigPin, LOW);
  // função Ranging, faz a conversão do tempo de
  //resposta do echo em centimetros, e armazena
  //na variavel distancia
    int distancia = (ultrasonic.Ranging(CM));

Serial.print("Distancia em Cm: ");
Serial.println(distancia);
delay(1000); //espera 1 segundo para fazer a leitura novamente
}


Referências:

Livraria: ftp://imall.iteadstudio.com/Modules/IM120628012_HC_SR04/Lib_IM120628012_HC_SR04.zip
Datasheet: ftp://imall.iteadstudio.com/Modules/IM120628012_HC_SR04/DS_IM120628012_HC_SR04.pdf

Espero que gostem!