Arduino and Processing Interact.

by maker_studio in Circuits > Arduino

5559 Views, 44 Favorites, 0 Comments

Arduino and Processing Interact.

schematic.png
Arduino and processing interact to turn pages

Arduino, processing and ultrasonic sensors to make a pages turner. It makes us can turn pages by waving hand.Ultrasonic page tunner is used to turn page on the computer.It is consist of 3 ultrasonic sensors and an Arduino UNO. It can help you turn page by wave your hand in front of the ultrasonic sensor.

Materials

零件.png

what you need:

Arduino Uno R3 x1;

ultrasonic sensor HC-SR04 x3;

ProtoBoard 7cm * 10cm - 2.54mm x1;

330 ohm ressitor x3;

LED x3;

Some dupont lines;

Schematic

schematic.png
实物图.jpg

Connect the circiut on bread board and test the fountions.

Put the Pictures in Suitable Situation.

文件.png

You must put some images (JPEG, GIF, or PNG) into the sketch’s data before you test the circuit.

Loading the Arduino Program and Processing to Test the Fountions

代码.png

Here is the Arduino code:

copy and paste these code to Arduino IDE and load it to UNO


int slide = 0;
boolean left=false; boolean center=false; boolean right=false; int trignPin1=4; int trignPin2=7; int trignPin3=10; int echoPin1=3; int echoPin2=6; int echoPin3=9; int ledPin1=5; int ledPin2=8; int ledPin3=11; int maxD = 20; long int lastTouch = -1; // ms int resetAfter = 2000; // ms int afterSlideDelay = 50; //ms; all slides ignored after successful slide int afterSlideOppositeDelay = 500; // left slides ignored after successful right slide int SLIDELEFT_BEGIN = -1; // Motion was detected from right int SLIDELEFT_TO_CENTER = -2; // Motion was detected from right to center int SLIDENONE = 0; // No motion detected int SLIDERIGHT_BEGIN = 1; // Motion was detected from left int SLIDERIGHT_TO_CENTER = 2; // Motion was detected from left to center
void setup() {
  Serial.begin(9600); // bit/s
  pinMode(ledPin1, OUTPUT); 
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3,OUTPUT);
}
void loop() {
  left = echo(trignPin1,echoPin1, ledPin1);
  center = echo(trignPin2,echoPin2, ledPin2);
  right = echo(trignPin3,echoPin3,ledPin3);
  if (left || center || right) { 
    lastTouch=millis(); 
  }
  if ((millis() - lastTouch)> resetAfter) { 
    slide=0; 
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW);
    // Serial.println("Reset slide and timer. ");
  }
  if (slide >= SLIDENONE) { // only if we are not already in opposite move 
    if ((left) && (!right)) 
      slide = SLIDERIGHT_BEGIN;
    if (center && (slide == SLIDERIGHT_BEGIN)) 
      slide = SLIDERIGHT_TO_CENTER;
    if (right && (slide == SLIDERIGHT_TO_CENTER))
      slideNow('R'); 
  }
  if (slide <= SLIDENONE) {    
    if (right && (!left))
      slide = SLIDELEFT_BEGIN;
    if (center && slide == SLIDELEFT_BEGIN)
      slide = SLIDELEFT_TO_CENTER;
    if (left && slide == SLIDELEFT_TO_CENTER) {
      slideNow('L');
    }
  }
  delay(50);
}
boolean echo(int trignPin, int echoPin, int ledPin) 
{
  int d = getDistance(trignPin, echoPin); // cm 
  boolean pinActivated = false;
 Serial.print(d); 
  if (d < maxD) {  
    digitalWrite(ledPin, HIGH);
    pinActivated = true;
  } else {
    digitalWrite(ledPin, LOW);
    pinActivated = false;
  }
  return pinActivated;
}
int getDistance(int trignPin,int echoPin)
{
  long duration, inches, cm;
  pinMode(trignPin, OUTPUT);
  digitalWrite(trignPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trignPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(trignPin, LOW);
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);  
  return(cm); // You could also return inches
}
void slideNow(char direction) {
  if ('R' == direction) 
     Serial.println("F");
     digitalWrite(ledPin1,HIGH);
  if ('L' == direction)
     Serial.println("B");
  digitalWrite(ledPin3, HIGH);
  delay(afterSlideDelay); 
  slide = SLIDENONE;
}
long microsecondsToInches(long microseconds) {
  return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds) {
  return microseconds / 29 / 2;
}

Here is the processing code:
copy and paste these code to Processing IDE run when UNO connecting with computer.

import processing.serial.*;
int slideStep = 75; // how many pixels to slide in/out // The current image and the next image to display PImage currentImage, nextImage;
// The index of the current image.
int imgIndex = 0; 
// Keeps track of the horizontal slide position. A negative number
// indicates sliding in from the left.
int slideOffset; 
// All the image files found in this sketch’s data/ directory.
String[] fileList; 
// A serial port that we use to talk to Arduino.
Serial myPort;
// This class is used to filter the list of files in the data directory
// so that the list includes only images.
class FilterImages implements java.io.FilenameFilter { 
  public boolean accept(File dir, String fname) {
    String[] extensions = {".png", ".jpeg", ".gif", ".tga", ".jpg"};
    
    // Don’t accept a file unless it has one of the specified extensions
    for (int i = 0; i < extensions.length; i++) { 
      if (fname.toLowerCase().endsWith( extensions[i])) {
        return true;
      }
    } 
    return false;
  }
}
// This loads the filenames into the fileList
void loadFileNames() { 
  java.io.File dir = new java.io.File(dataPath(""));
  fileList = dir.list(new FilterImages());
}
// The Processing setup method that’s run once
void setup() {
  size(displayWidth, displayHeight); // Go fullscreen
  loadFileNames();   // Load the filenames
  /* This centers images on the screen. To work correctly with
   this mode, we'll be using image coordinates from the center
   of the screen (1/2 of the screen height and width) .
   */
  imageMode(CENTER); 
  // Load the current image and resize it.
  println(fileList[0]);
  currentImage = loadImage(dataPath("") + "\\" + fileList[0]); 
  currentImage.resize(0, height);
  println(Serial.list()); 
  myPort = new Serial(this, Serial.list()[0], 9600); 
}
// Go to the next image
void advanceSlide() { 
  imgIndex++; // go to the next image
  if (imgIndex >= fileList.length) { // make sure we're within bounds
    imgIndex = 0;
  }
  slideOffset = width; // Start sliding in from the right
}
void reverseSlide() {
  imgIndex--; // go to the previous image
  if (imgIndex < 0) { // make sure we're within bounds
    imgIndex = fileList.length - 1;
  }
  slideOffset = width * - 1; // Start sliding in from the left
}
void draw() {
  // Put up a black background and display the current image.
  background(0);
  image(currentImage, width/2, height/2); 
  // Is the image supposed to be sliding?
  if (slideOffset != 0) { 
    // Load the next image at the specified offset.
    image(nextImage, slideOffset + width/2, height/2);
    if (slideOffset > 0) { // Slide from the right (next) 
      slideOffset -= slideStep;
      if (slideOffset < 0) {
        slideOffset = 0;
      }
    }
    if (slideOffset < 0) { // Slide from the left (previous)
      slideOffset += slideStep;
      if (slideOffset > 0) {
        slideOffset = 0;
      }
    }
    if (slideOffset == 0) { 
      currentImage = nextImage;
    }
  } 
  else {
    // If we're not sliding, read the serial port.
    if (myPort.available() > 0) {
      char inByte = myPort.readChar();
      print(inByte); // Displays the character that was read
      if (inByte == 'F') { // Forward
        advanceSlide(); 
      }
      if (inByte == 'B') { // Backward
        reverseSlide();
      }
      // Load and resize the next image
      nextImage = loadImage(dataPath("") + "\\" + fileList[imgIndex]); 
      nextImage.resize(0, height);
    }
  }
}

Design and Assemble the Shape

整体4.png
整体6.png

My design is not looking so good. I will design a better shape next time.
Made by Xie weifang from i-element.org