sexta-feira, 26 de fevereiro de 2016

SD Card Shield DFR0165 - Arquivo 017

Referência:
http://www.dfrobot.com/wiki/index.php/Mega_IO_Expansion_Shield_V2.3_(SKU:DFR0165)

Lista de Materiais:

IDE Arduino 1.0.6
1x  Board Arduino MEGA 2560
1x  Cabo USB-A male to USB-B male 
1x  Mega IO Expansion Shield V.2.3
1x  Mini SD Card

Montagem:




Firmware:
#include <SPI.h>
#include <SD.h>

const int chipSelect = 4;


void setup(){

  Serial.begin(9600);
  while (!Serial) {
    ;
  }


  Serial.print("Initializing SD card...");


  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    return;
  }
  Serial.println("card initialized.");
}

void loop(){

  String dataString = "";

  for (int analogPin = 0; analogPin < 3; analogPin++) {
    int sensor = analogRead(analogPin);
    dataString += String(sensor);
    if (analogPin < 2) {
      dataString += ",";
    }
  }

  File dataFile = SD.open("datalog.txt", FILE_WRITE);

  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    Serial.println(dataString);
  }
  else {
    Serial.println("error opening datalog.txt");
  }
}

Download: não necessário

Button I2C PCF8547 - Arquivo 016

Referência: 
http://www.dfrobot.com/

Montagem:






Firmware:

#include <Wire.h>

int NewX=0, OldX=0, y=0;

int ExitReadButton = 1,
    oldstate1 = 0,
    oldstate2 = 0,
    oldstate3 = 0,
    oldstate4 = 0,
    oldstate5 = 0,
    oldstate6 = 0,
    oldstate7 = 0,
    oldstate8 = 0;

const int Addr_Button = 0x38;


void setup(){

  Wire.begin();
  Serial.begin(9600);
}

void loop(){

  Wire.requestFrom(Addr_Button,1);    
  ReadButton();
  Serial.println(y);                                            
}

void ReadButton(){

  byte x;
  do{
   Wire.requestFrom (Addr_Button,1);
   if(Wire.available()) x = Wire.read();
   
   if (x == 254 && oldstate1 == 0) oldstate1 = 1;
   if (x == 255 && oldstate1 == 1){
     y = 0;
     oldstate1 = 0;
     ExitReadButton = 0;
   }
   
   if (x == 253 && oldstate2 == 0) oldstate2 = 1;
   if (x == 255 && oldstate2 == 1){
     y = 1;
     oldstate2 = 0;
     ExitReadButton = 0;
   }
   
   if (x == 251 && oldstate3 == 0) oldstate3 = 1; 
   if (x == 255 && oldstate3 == 1){
     y = 2;
     oldstate3 = 0;
     ExitReadButton = 0;
   }
   
   if (x == 247 && oldstate4 == 0) oldstate4 = 1; 
   if (x == 255 && oldstate4 == 1){
     y = 3;
     oldstate4 = 0;
     ExitReadButton = 0;
   }
   
   if (x == 239 && oldstate5 == 0) oldstate5 = 1; 
   if (x == 255 && oldstate5 == 1){
     y = 4;
     oldstate5 = 0;
     ExitReadButton = 0;
   }
   
   if (x == 223 && oldstate6 == 0) oldstate6 = 1; 
   if (x == 255 && oldstate6 == 1){
     y = 5;
     oldstate6 = 0;
     ExitReadButton = 0;
   }
   
   if (x == 191 && oldstate7 == 0) oldstate7 = 1; 
   if (x == 255 && oldstate7 == 1){
     y = 6;
     oldstate7 = 0;
     ExitReadButton = 0;
   }
   if (x == 127 && oldstate8 == 0) oldstate8 = 1; 
   if (x == 255 && oldstate8 == 1){
     y = 7;
     oldstate8 = 0;
     ExitReadButton = 0;
   }
  }while(ExitReadButton);
  delay(200);
  ExitReadButton = 1;
}

Download: Schematic Mega Shield

quarta-feira, 24 de fevereiro de 2016

Como Scannear dispositivos no barramento I2C - Arquivo 015

Referêcia:
http://www.arduinoecia.com.br/2015/04/arduino-lcd-16x2-modulo-i2c-rtc-ds1307.html

Lista de Materiais:
IDE Arduino 1.0.6
1x  Board Arduino UNO
1x  Protoboard 830 furos
1x  Cabo USB-A male to USB-B male 
1x  Conj. de fios jumper p/ protoboard
1x  Sensor de temperatura MLX90615
1x  C.I. DS1307 RTC
1x  Display I2C / Drive PCF8574AP

Montagem:

Esse programa varre o barramento I2C e mostra no serial monitor o endereço dos dispositivos conectados.



Firmware:
#include <Wire.h>
//****************************************************************************
void setup(){
  Wire.begin();
  Serial.begin(9600);
  Serial.println("\nI2C Scanner");
}
//****************************************************************************
void loop(){
  byte error, address;
  int nDevices;
  Serial.println("Scanning...");
  nDevices = 0;
  for(address = 1; address < 127; address++ ) {
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    Wire.beginTransmission(address);
    error = Wire.endTransmission();

    if (error == 0){

      Serial.print("I2C device found at address 0x");
      if (address<16) 
        Serial.print("0");
      Serial.print(address,HEX);
      Serial.println("  !");
      nDevices++;
    }
    else if (error==4) {
      Serial.print("Unknow error at address 0x");
      if (address<16) 
        Serial.print("0");
      Serial.println(address,HEX);
    }    
  }
  if (nDevices == 0)
    Serial.println("No I2C devices found\n");
  else
    Serial.println("done\n");
  delay(5000);         
}

Download: não necessário

sábado, 6 de fevereiro de 2016

Sensor Monóxido de Carbono MQ-7 - Arquivo 014

Referência:
http://arduino.rechena.com/?p=313

Lista de Materiais:
IDE Arduino 1.0.6
1x  Board Arduino NANO
1x  Protoboard 830 furos
1x  Cabo USB-A male to USB-Mini male 
1x  Conj. de fios jumper p/ protoboard
1x  Sensor monóxido de carbono MQ-7

Montagem:



Firmware:
int sensorValue; 
//*********************************************************************** 
void setup(){
  Serial.begin(9600);                           // sets the serial port to 9600
}
//*********************************************************************** 
void loop(){
  sensorValue = analogRead(0);         // read analog input pin 0
  Serial.print("Value: ");
  Serial.println(sensorValue, DEC);   // prints the value read
  delay(250);                                       // wait 250ms for next reading
}

Download: não necessário

Acelerômetro ADXL330 - Arquivo 013

Referência:
http://playground.arduino.cc/Portugues/LearningADXL3xx

Lista de Materiais:
IDE Arduino 1.0.6
1x  Board Arduino UNO
1x  Protoboard 830 furos
1x  Cabo USB-A male to USB-B male 
1x  Acelerômetro ADXL330

Montagem:





Firmware:
int groundPin = 18;             // pino de entrada analógica 4
int powerPin = 19;              // pino de entrada analógica 5
int xpin = 3;                   // eixo x do acelerômetro
int ypin = 2;                   // eixo y
int zpin = 1;                   // eixo z (disponível só em modelos triaxiais)

void setup()
{
  Serial.begin(9600);

  // Fornecer terra e energia mediante o uso das entradas analógicas
  // como pinos digitais comuns. Isso permite conectar a placa de
  // prototipagem diretamente no Arduino. No caso de se usar os pinos
  // normais GND e 5V do Arduino, pode-se remover as quatro linhas
  // abaixo.
  pinMode(groundPin, OUTPUT);
  pinMode(powerPin, OUTPUT);
  digitalWrite(groundPin, LOW); 
  digitalWrite(powerPin, HIGH);
}

void loop()
{
  Serial.print(analogRead(xpin));
  Serial.print(" ");
  Serial.print(analogRead(ypin));
  Serial.print(" ");
  Serial.print(analogRead(zpin));
  Serial.println();
  delay(1000);
}

Download: não necessário

segunda-feira, 1 de fevereiro de 2016

Controlando Led RGB com Arduino e Processing - Arquivo 012

Referência:
http://blog.bsoares.com.br/processing/controlling-rgb-led-with-arduino-and-processing

Lista de Materiais:
IDE Arduino 1.0.6
IDE Processing 3 3.0b6 Alpha
1x  Board Arduino NANO
1x  Protoboard 830 furos
1x  Cabo USB-A male to USB-mini male 

1x  Conj. de fios jumper p/ protoboard
1x  Bolinha de ping-pong
1x  Led RGB - Catodo comum
2x  Resistor 90R 1/4w
1x  Resistor 150R 1/4w

Montagem:








Firmware - Arduino:
#define START_COLOR_CHAR '^'
#define END_COLOR_CHAR '$'
#define COLOR_SIZE 8
#define PIN_RED 9
#define PIN_GREEN 11
#define PIN_BLUE 10
//******************************************************************************* char serialMessage[COLOR_SIZE];
unsigned int readChar;
unsigned int count;
unsigned long color;
unsigned int r;
unsigned int g;
unsigned int b;
boolean readingSerial;
//******************************************************************************* void setup() {
  Serial.begin(9600);
  readingSerial = false;
}
//******************************************************************************* void loop() {
  if (Serial.available() > 0 && !readingSerial) {
    if (Serial.read() == START_COLOR_CHAR) {
      serialReadColor();
    }
  }
}
//******************************************************************************* void serialReadColor() {
  readingSerial = true;
  count = 0;
  
  iniReading:
  if (Serial.available() > 0) {
    readChar = Serial.read();
    if (readChar == END_COLOR_CHAR || count == COLOR_SIZE) {
      goto endReading;
    } else {
      serialMessage[count++] = readChar;
      goto iniReading;
    }
  }
  goto iniReading;
  
  endReading:
  readingSerial = false;
  serialMessage[count] = '\0';
  
  setColor(serialMessage);
}
//******************************************************************************* void setColor(char* value)
{
  // Convert Char* to Long
  color = atol(value);
  
  // Extract RGB
  r = color >> 16 & 0xFF;
  g = color >>  8 & 0xFF;
  b = color >>  0 & 0xFF;
  
  // Send values to analog pins
  analogWrite(PIN_RED, r);
  analogWrite(PIN_GREEN, g);
  analogWrite(PIN_BLUE, b);

}

Firmware - Processing:
import processing.serial.*;

Serial port;
//******************************************************************************* void setup() {
  size(100, 150);
  noStroke();
  // Background
  colorMode(HSB, 100);
  for (int i = 0; i < 100; i++) {
    for (int j = 0; j < 100; j++) {
      stroke(i, j, 100);
      point(i, j);
    }
  }
  // Select port
  println(Serial.list());
  //port = new Serial(this, Serial.list()[1], 9600);
  port = new Serial(this, "COM16", 9600);        
}
//******************************************************************************* void draw() {
  // Only to enable the method mouseDragged
}
//******************************************************************************* void mouseClicked() {
  processColor();
}
//******************************************************************************* void mouseDragged() {
  processColor();
}
//*******************************************************************************
void processColor() {
  color c = get(mouseX, mouseY);
  noStroke();
  fill(c);
  rect(0, 100, 100, 50);
  sendColorToSerial(c);
}
//******************************************************************************* void sendColorToSerial(color colour) {
  // Get HEX
  String hexColor = hex(colour, 6);
  
  // Convert HEC to Number
  long numColor = unhex(hexColor);
  
  // Send color number to serial port
  port.write("^" + numColor + "$");
}

Download: https://processing.org