Potentiometer nearly working, don't know what I'm missing?
in
Integration and Hardware
•
4 months ago
I have my Arduino sending Potentiometer and RFID values, the RFID loads the image from the data folder and the potentiometer is there to scroll left and right. Now the RFID is loading the images, but the Potentiometer is moving the image, it did slightly before. I just don't know what I'm missing to make it work, Any suggestions ?
- import processing.serial.*;
- import ddf.minim.*;//declare
- AudioPlayer player;
- Minim minim;
- int x, moveSpeed = 10;
- Serial portOne; // the first serial port
- Serial portTwo; // the second serial port
- boolean showImage; // whether or not to show the image
- // the list of names
- String[] tags = {
- "5100C2F495F2","5100C2EB6F17","59003206E68B","59003219B7C5","590031BDCE1B","590031C48B4"};
- String tag="";
- String sensorReading = "";
- float sensorFloat;
- // the list of people
- PImage thisImage;
- PImage bg;
- int maxPics =1;
- int pictureNumber = 1;
- void setup() {
- size(1000, 664);
- showImage = false;
- // list the serial ports
- println(Serial.list());
- // open the serial ports:
- portOne = new Serial(this, Serial.list()[16], 9600);
- // set both ports to buffer information until you get 0x03:
- portOne.bufferUntil('\n');
- minim=new Minim(this);
- bg = loadImage("backimage.jpg");
- }
- void draw() {
- background(bg);
- if(tag!="")
- {
- getImage(tag,pictureNumber);
- image(thisImage, 0, 0);
- }
- // if there's an image to show, show it:
- if (showImage) {
- image(thisImage, 0, 0);
- float potentiometer = sensorFloat;
- float movement = map(potentiometer, 0, 1023, -moveSpeed, moveSpeed); // map from range (0, 1023) to (-moveSpeed, moveSpeed)
- x += movement; // add movement to location
- x = constrain(x, -(thisImage.width-width), 0); // constrain image by 0 (at the beginning) and -600 (aka a remaining visible image width of 1000 equal to the canvas width).
- image(thisImage, x, 0); // draw the image
- fill(0);
- frame.setTitle(x + ""); // see the x in the top-left
- }
- }
- void serialEvent(Serial thisPort) {
- // read the incoming serial data:
- String potString = thisPort.readStringUntil(',');
- String rfidString = thisPort.readStringUntil('\n');
- if(potString != null && potString.length()>2)
- {
- potString = trim(potString);
- sensorFloat = float(potString);
- }
- // if the string is not empty, do stuff with it:
- if (rfidString != null && rfidString.length()>2) {
- // if the string came from serial port one:
- if (thisPort == portOne) {
- print ("Data from port one: ");
- }
- // if the string came from serial port two:
- if (thisPort == portTwo) {
- print ("Data from port two: ");
- }
- // print the string:
- println(rfidString);
- println(rfidString.substring(1,13)+".jpg");
- // images[i] = loadImage(rfidString.substring(1,13)+ "/" + i + ".jpg" );
- println(rfidString.substring(1,13)+"/music.mp3");
- if(player!=null)
- {
- player.pause();
- player.close();
- }
- player=minim.loadFile(rfidString.substring(1,13)+"/music.mp3");//locate file
- println(player);
- player.play();//when opened play this
- // the tag ID is only bytes 1 through 13. Get it:
- tag = rfidString.substring(1, 13);
- }
- }
- // Get random image from the folder filelist array
- void getImage(String tag,int picture) {
- String filenames[];
- try{
- // open the directory:
- File directory = new File(sketchPath("data")+"/"+tag);
- // list the files in the directory:
- filenames = directory.list();
- int picCounter=0;
- for(int thisFile = 0; thisFile<filenames.length; thisFile++){
- // if the file is a .jpg:
- if (filenames[thisFile].endsWith(".jpg")) {
- picCounter++;
- if(picCounter==picture)
- {
- // get the filename without the .jpg extension:
- String thisName = filenames[thisFile].substring(0, filenames[thisFile].length() -4);
- //if the filename matches the name of the person:
- // load the image from the file:
- thisImage = loadImage("data/"+tag+"/"+filenames[thisFile]);
- // show it:
- showImage = true;
- }
- }
- }
- }
- catch(Exception f){
- // if there was a problem reading the directory:
- println("Image import error - missing thisFile or folder");
- }
- }
1