Go Down

Topic: Processing analogWrite/analogRead conflict (Read 1 time) previous topic - next topic

DamienK

Nov 03, 2014, 06:11 am Last Edit: Nov 03, 2014, 06:12 am by DamienK
Hi guys,

I'm using firmata to send data between the arduino and my processing, and sending and receiving work fine independently, but when I'm running them together it doesn't work.

I can have analogRead give me data from the arduino or I can have analogWrite tell the arduino to do something, but if I have both of them going at the same time one analogRead just repeats the last set of values it sent.

The arduino code I am using is a minor modification of AllInputFirmata.

Processing Code:
Code: [Select]
import controlP5.*;
import org.firmata.*;
import cc.arduino.*;
import processing.serial.*;
import cc.arduino.*;

Arduino arduino;

float diffVal = 1000;
int diffInt = int(diffVal);

int recLength = 1000;

int[][] data = new int[1000][3];
int[][]compData = new int[1000][3];
int currIndex = 0;
int compareStore[] = new int[3];
int diffCount = 0;

color off = color(4, 79, 111);
color on = color(84, 145, 158);

int reservedPinTone= 6;
int reservedPinVibe = 9;

ControlP5 gui;

int recCount = 0;
boolean recPressed = false;
int playCount = 0;
boolean playPressed = false;

int diffX = 0;
int diffY = 0;
int diffZ = 0;
int totalDiff = 0;

int playIteration = 0;

float intensity = 0;
int intensityVal = 0;

int originalTotal = 0;
int newTotal = 0;



void setup() {

  size(500, 500, P3D);
  rectMode(CENTER);

  // Prints out the available serial ports.
  println(Arduino.list());

  // Modify this line, by changing the "0" to the index of the serial
  // port corresponding to your Arduino board (as it appears in the list
  // printed by the line above).
  arduino = new Arduino(this, Arduino.list()[0], 57600);

  // Alternatively, use the name of the serial port corresponding to your
  // Arduino (in double-quotes), as in the following line.
  //arduino = new Arduino(this, "/dev/tty.usbmodem621", 57600);

  // Set the Arduino digital pins as inputs.
  //make sure to exclude the reserved pins
  for (int i = 0; i <= reservedPinTone; i++){
    arduino.pinMode(i, Arduino.INPUT);
  }
  arduino.pinMode(reservedPinTone, Arduino.OUTPUT);
  arduino.pinMode(reservedPinVibe, Arduino.OUTPUT);
  for (int i = reservedPinVibe; i <= 13; i++){
    arduino.pinMode(i, Arduino.INPUT);
  }
 
  //Create the new GUI
  gui = new ControlP5(this);
  //Add a Button
  gui.addButton("Record")
    //Set the position of the button : (X,Y)
    .setPosition(50, 50)
    //Set the size of the button : (X,Y)
    .setSize(100, 100)
     //Set the pre-defined Value of the button : (int)
    .setValue(0)
    //set the way it is activated : RELEASE the mouseboutton or PRESS it
    .activateBy(ControlP5.RELEASE);
  ;
 
  gui.addButton("Play")
    //Set the position of the button : (X,Y)
    .setPosition(150, 50)
    //Set the size of the button : (X,Y)
    .setSize(100, 100)
     //Set the pre-defined Value of the button : (int)
    .setValue(100)
    //set the way it is activated : RELEASE the mouseboutton or PRESS it
    //.activateBy(ControlP5.RELEASE);
    ;
}

public void Record(int value) {
  // This is the place for the code, that is activated by the buttonb
  if(recCount != 0){
    println("Record pressed");
    recPressed = true;
    /*if (currIndex < 1000) {
    saveData();
    }*/
  }
  recCount += 1;
}

public void Play(int value) {
  // This is the place for the code, that is activated by the buttonb
  if(playCount != 0){
    println("Play pressed");
    if (currIndex >= 1000) {
      playPressed = true;
    } else if (currIndex < 1000){
      println("Recording not complete");
    }
  }
  playCount += 1;
}

public void controlEvent(ControlEvent theEvent) {
  //Is called whenever a GUI Event happened
}

void draw() {
  //background(off);
  //stroke(on);

  for (int i = 0; i <= 2; i++) {
        println(arduino.analogRead(i));
      }

    //arduino.analogWrite(reservedPinTone, 5); 

  // Draw a circle whose size corresponds to the value of an analog input.
  //noFill();
  if (currIndex >= 1000 && playPressed == true) {
    //println("Play Iteration");
      compareData();
  } else if (currIndex < 1000 && recPressed == true) {
    saveData();
    println("Progress: " + currIndex/10.0 + "%");
  }
 
  //test
    //vibration
      //arduino.analogWrite(reservedPinVibe, 255);
    //audio
      //arduino.analogWrite(reservedPinTone, 255);
 
}

void saveData() {
  //println("Saving Data");
      for (int i = 0; i <= 2; i++) {
        data[currIndex][i] = arduino.analogRead(i);
        println(data[currIndex][i]);
        //ellipse(280 + i * 30, 240, arduino.analogRead(i) / 16, arduino.analogRead(i) / 16);
    }
    //println(currIndex);
    currIndex += 1;
}

void compareData(){
  //println("Comparing Data");
  if (playIteration < 1000){
   println("Compare Slot: " + playIteration);
   
 
  for (int k = 0; k <= 2; k++){
    println("Recording: " + arduino.analogRead(k));
  }

  //for (int i = 0; i < 1000; i++) {
    for (int j = 0; j <= 2; j++) {
      compareStore[j] = arduino.analogRead(j);
      //println(compareStore[j]);
    }
    originalTotal = data[playIteration][0] + data[playIteration][1] + data[playIteration][2];
    newTotal = compareStore[0] + compareStore[1] + compareStore[2];
   
    diffX = abs(data[playIteration][0] - compareStore[0]);
    diffY = abs(data[playIteration][1] - compareStore[1]);
    diffZ = abs(data[playIteration][2] - compareStore[2]);
    //println(diffX +", " + diffY +", " + diffZ);
    totalDiff = diffX + diffY + diffZ;
    //println("Difference = " + totalDiff);
   
    //???????!!!!!
      if (totalDiff >= diffInt){
        totalDiff = diffInt;
      }
     
      if (totalDiff >= 0){
        //diffCount += 1;
        //println("DIFFERENCE DETECTED = " + totalDiff);
        intensity = 255*(totalDiff/diffVal);
        intensityVal = int(intensity);
         println("Difference = " + totalDiff);
        println("Intensity = " + intensityVal);
        sendData(intensityVal);
      }
      else {
          //no vibration or audio
          println("None Detected");
          arduino.analogWrite(reservedPinVibe, 0);
          arduino.analogWrite(reservedPinTone, 0);
      }
    //}
    currIndex += 1;
    playIteration += 1;
    }
  }
 
void sendData(int intensityVal){
  //vibration
    //arduino.analogWrite(reservedPinVibe, intensityVal);
  //audio
    //arduino.analogWrite(reservedPinTone, intensityVal);
}

void drawData() {
  fill(20);

  for (int i = 0; i < 1000; i++) {
    translate(data[i][0], data[i][1], data[i][2]);
    rect(0, 0, 10, 10);
  }
}


Has anyone encountered this before/know a solution?

PaulS

Code: [Select]
  //make sure to exclude the reserved pins
  for (int i = 0; i <= reservedPinTone; i++){
    arduino.pinMode(i, Arduino.INPUT);
  }

"the reserved pins" would include the hardware serial pins (0 and 1).

What is connected to the analog pins on the Arduino? What is connected to the reservedPinVibe pin on the Arduino?
The art of getting good answers lies in asking good questions.

Go Up