Adafruit shield Arduino

Shield de arduino

Objetivo
El objetivo es poder realizar un programa en el que echemos a andar el shield capacitivo de arduino, para poder demostras y comprobar la cantidad de resistencia que generar diferentes artículos, ya sean masillas o salinidad dentro del agua.

Material
-          1 Adafruit 12x Capacitive Touch shield for arduino
-          1 Tarjeta de Arduino Uno
-          6 Caimanes


Descripción del shield de arduino
Shield táctil capacitiva para trabajos de detección, puede detectar cuando una persona (o animal) ha tocado uno de los electrodos del sensor. La detección táctil capacitiva es usada para cosas como las tabletas y los teléfonos de contacto, así como paneles de control para los aparatos. Este escudo le permite crear productos electrónicos que pueden reaccionar al tacto humano, con hasta 12 sensores individuales.
Programa
Programa 1

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

Adafruit_MPR121 cap = Adafruit_MPR121();

uint16_t lasttouched = 0;
uint16_t currtouched = 0;

void setup() {
  while (!Serial);       

  Serial.begin(9600);
  Serial.println("Adafruit MPR121 Capacitive Touch sensor test"); 

  if (!cap.begin(0x5A)) {
    Serial.println("MPR121 not found, check wiring?");
    while (1);
  }
  Serial.println("MPR121 found!");
}

void loop() {
  currtouched = cap.touched();
  
  for (uint8_t i=0; i<12; i++) {
    if ((currtouched & _BV(i)) && !(lasttouched & _BV(i)) ) {
      Serial.print(i); Serial.println(" touched");
    }
    if (!(currtouched & _BV(i)) && (lasttouched & _BV(i)) ) {
      Serial.print(i); Serial.println(" released");
    }
  }

  lasttouched = currtouched;

  return;
  
  Serial.print("\t\t\t\t\t\t\t\t\t\t\t\t\t 0x"); Serial.println(cap.touched(), HEX);
  Serial.print("Filt: ");
  for (uint8_t i=0; i<12; i++) {
    Serial.print(cap.filteredData(i)); Serial.print("\t");
  }
  Serial.println();
  Serial.print("Base: ");
  for (uint8_t i=0; i<12; i++) {
    Serial.print(cap.baselineData(i)); Serial.print("\t");
  }
  Serial.println();

  delay(100);
}
Programa 2

Programa 3

Evidencias

Conclusión

Softpot lineal

Potenciómetro Softpot lineal

Objetivo
El objetivo de esta práctica es observar, el funcionamiento del potenciómetro softpot lineal, el cual tiene muchas aplicaciones dentro de los dispositivos que utilizamos.
Material
-          1 Potenciómetro softpot Lineal
-          1 resistencia de 10 KΩ
-          Tarjeta Arduino uno

Descripción del potenciómetro
Es un potenciómetro variable muy delgado, el cual si es presionado hacia abajo en varias partes de la tira, la resistencia cambia linealmente desde 100 ohms a 10,000 ohmios y este te permite calcular con gran precisión la posición relativa en la tira, estos potenciómetros gracias a que trabajan muy bien con el dedo son utilizados en dispositivos como iPod Classic, algunos reproductores mp4, blue ray, televisiones, entre otros más.
Circuito
Circuito en el que podemos observar la distribución de los pines del potenciómetro lineal.
Programas
Programa 1
int pot = 0;
int val = 0;
void setup(){
  Serial.begin(9600);
}
void loop(){
  val = analogRead(pot);
  Serial.println(val);
  delay(500);
}

Programa 2
int sec = 0;
int AnalogPin = 0
int valor = 0; 
 void setup() {
  Serial.begin(9600);
}
 void loop() {
  valor = analogRead(AnalogPin);
  while (valor > 600) {
    valor = analogRead(AnalogPin);
  }
    for (sec=0;sec<6;sec++) {
      valor = analogRead(AnalogPin);
      if (valor > 1024 && valor< 924)      {
                  Serial.write(0);
}
if (valor > 1024 && valor< 924)      {
                  Serial.write(1);
}
if (valor > 924 && valor< 824)      {
                  Serial.write(2);
}
if (valor > 824 && valor< 724)      {
                  Serial.write(3);
}
if (valor > 724 && valor< 624)      {
                  Serial.write(4);
}
if (valor > 624 && valor< 524)      {
                  Serial.write(5);
}
if (valor > 524 && valor< 424)      {
                  Serial.write(6);
}
if (valor > 424 && valor< 324)      {
                  Serial.write(7);
}
if (valor > 324 && valor< 224)      {
                  Serial.write(8);
}
if (valor > 224 && valor< 124)      {
                  Serial.write(9);
}
if (valor > 124 && valor<0)      {
                  Serial.write(10);
}
}
  sec=0;
  delay(1000);
}

Programa processing
import processing.serial.*;

Serial myPort;
int val;   
PImage img;

void setup()
{
  size(640, 500);
  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);
}

void draw()
{
  if ( myPort.available() > 0) {
    val = myPort.read();        
  }
  background(255);           
  if (val == 0) {            
    img = loadImage("Barcharge0");                  
  }
  else if (val == 1) {            
    img = loadImage("Barcharge1");                  
  }
  else if (val == 2) {            
    img = loadImage("Barcharge2");                  
  }
  else if (val == 3) {            
    img = loadImage("Barcharge3");                  
  }
  else if (val == 4) {            
    img = loadImage("Barcharge4");                  
  }
  else if (val == 5) {            
    img = loadImage("Barcharge5");                  
  }
  else if (val == 6) {            
    img = loadImage("Barcharge6");                  
  }
  else if (val == 7) {            
    img = loadImage("Barcharge7");                  
  }
  else if (val == 8) {            
    img = loadImage("Barcharge8");                  
  }
  else if (val == 9) {            
    img = loadImage("Barcharge9");                   
  }
  else if (val == 10) {            
    img = loadImage("Barcharge10");                  
  }
  rect(50, 50, 100, 100);
}

Programa 3 (Este es un programa sacado de http://ee.walfas.org/2010/12/05/arduino-softpot-potentiometer-game/ es un juego que me pareció interesante mientras investigaba)
int analogPin = 2;

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

void loop() {
  Serial.println(analogRead(analogPin));
  delay(100);
}


Programa Processing
import processing.serial.*;
 
///////////////
// Constants //
///////////////
 
// High score data file
PrintWriter file;
String FILE_NAME = "score.dat";
 
// Serial input setup
String PORT_NAME = "COM3";   // Change this
int BAUD_RATE = 9600;
int SERIAL_IN_MAX = 1023;
 
// Font setup
PFont font;
String FONT_NAME = "Arial";
int FONT_SIZE = 20;
 
// Graphics setup constants
int FPS = 60;
int SCREEN_SIZE[] = {550,400};
int BG_COLOR = #000000;
int LINE_COLOR = #00FF00;
int TEXT_COLOR = #FFFFFF;
int CENTER_X = SCREEN_SIZE[0]/2;
int CENTER_Y = SCREEN_SIZE[1]/2;
 
// Player information constants
int PLAYER_SIZE = 6;
int HALF_PLAYER_SIZE = PLAYER_SIZE/2;
 
// Difficulty constants
int VERTICAL_RATE_RANGE[] = {2, 7};
int VERTICAL_RATE_CHANGE = 500;
int GAP_SIZE_RANGE[] = {25, 50};
int GAP_SIZE_CHANGE = 500;
 
int OBSTACLE_UPPER_BOUND = 100;
int OBSTACLE_LOWER_BOUND = SCREEN_SIZE[1]-100;
 
// Circular buffer constants
int SCROLL_SPEED = 2;
int BUFFER_SIZE = SCREEN_SIZE[0]/SCROLL_SPEED;
 
///////////////
// Variables //
///////////////
 
Serial arduino;
 
String serialString = "";
char byteReceived = 0;
int serialValue = 512;
int serialValueBuffer = serialValue;
 
float playerY = CENTER_Y;
int playerX = SCREEN_SIZE[0]/10;
int score, highScore, verticalRate, gapSize = 0;
 
int[] buffer = new int[BUFFER_SIZE];
int bufferWritePos = 0;
int bufferWritePosPrev = BUFFER_SIZE-1;
int bufferReadPos = 0;
int bufferCheckIndex = 0;
 
//////////////////////////
// Function definitions //
//////////////////////////
 
void setup() {
  size(SCREEN_SIZE[0],SCREEN_SIZE[1]);
 
  font = createFont(FONT_NAME, FONT_SIZE);  // Initialize font
  textFont(font);
 
  arduino = new Serial(this, PORT_NAME, BAUD_RATE);
 
  rectMode(CENTER);
  frameRate(FPS);
 
  reset();
 
  highScore = int(loadStrings(FILE_NAME)[0]);
  file = createWriter(FILE_NAME);
}
 
void draw() {
  if (arduino.available() > 0) {
    getData();
  }
  clearScreen();
  updateBackground();
  drawPlayer();
  scoreUpdate();
  difficultyCheck();
  checkDeath();
}
 
void reset() {
  score = 0;
 
  for (int i = 0; i < BUFFER_SIZE; i++) {
    buffer[i] = CENTER_Y;
  }
}
 
void checkDeath() {
  bufferCheckIndex = (bufferWritePos+playerX/SCROLL_SPEED)%BUFFER_SIZE;
  if (playerY + HALF_PLAYER_SIZE > buffer[bufferCheckIndex]+gapSize ||
      playerY - HALF_PLAYER_SIZE < buffer[bufferCheckIndex]-gapSize) {
    textAlign(CENTER, BOTTOM);
    text("GAME OVER",CENTER_X,CENTER_Y);
    text("Move in between the lines to try again",CENTER_X,CENTER_Y+FONT_SIZE);
    reset();
  }
}
 
void scoreUpdate() {
  score ++;
  if (score > highScore) {
    highScore = score;
  }
 
  fill(LINE_COLOR);
  stroke(TEXT_COLOR);
  textAlign(RIGHT, TOP);
  text("High Score:", SCREEN_SIZE[0]-70, 0);
  text("Score:", SCREEN_SIZE[0]-70, FONT_SIZE);
 
  text(nfc(highScore), SCREEN_SIZE[0], 0);
  text(nfc(score), SCREEN_SIZE[0], FONT_SIZE);
}
 
void difficultyCheck() {
  score ++;
  gapSize = max(GAP_SIZE_RANGE[1]-score/GAP_SIZE_CHANGE, GAP_SIZE_RANGE[0]);
  verticalRate = min(VERTICAL_RATE_RANGE[0]+score/VERTICAL_RATE_CHANGE, VERTICAL_RATE_RANGE[1]);
}
 
void clearScreen() {
  background(BG_COLOR);
}
 
void drawPlayer() {
  playerY = map(serialValue, 0, 1023, 0, SCREEN_SIZE[1]);
  fill(LINE_COLOR);
  ellipse(playerX, playerY, PLAYER_SIZE, PLAYER_SIZE);
}
 
void getData() {
  byteReceived = char(arduino.read());
  if (byteReceived != '\n') {
    serialString += byteReceived;
  } else {
    serialString = serialString.substring(0,serialString.length()-1);
 
    if (int(serialString) != 0) {
      serialValueBuffer = int(serialString);
    }
    serialValue = (3*serialValue+serialValueBuffer)/4;
    serialString = "";
  }
}
 
void updateBackground() {
  buffer[bufferWritePos] = buffer[bufferWritePosPrev] + int(random(-verticalRate,verticalRate));
 
  if (buffer[bufferWritePos] > OBSTACLE_LOWER_BOUND) {
    buffer[bufferWritePos] = OBSTACLE_LOWER_BOUND;
  }
  if (buffer[bufferWritePos] < OBSTACLE_UPPER_BOUND) {
    buffer[bufferWritePos] = OBSTACLE_UPPER_BOUND;
  }
 
  bufferWritePosPrev = bufferWritePos;
  bufferWritePos ++;
  bufferWritePos %= BUFFER_SIZE;
 
  drawObstacles(-gapSize);
  drawObstacles(gapSize);
}
 
void drawObstacles(int offset) {
  bufferReadPos = bufferWritePos;
 
  stroke(LINE_COLOR);
  noFill();
 
  int x, y;
  beginShape();
  for (int i = 0; i < BUFFER_SIZE; i++) {
    x = i*SCROLL_SPEED;
    y = buffer[bufferReadPos]+offset;
    vertex(x,y);
 
    bufferReadPos ++;
    bufferReadPos %= BUFFER_SIZE;
  }
  endShape();
}
 
void stop() {
  file.println(highScore);
  file.flush();
  file.close();
  exit();
}

Evidencias
Conclusión

Este sensor es similar al anterior, su funcionamiento es practicante igual que el de rueda, pero este sensor es más utilizado para dispositivos móviles, ya que por el posicionamiento y la precisión en la que se presiona da la lectura de la resistividad dada.