无法识别处理中的mouseClicked

时间:2014-04-11 19:33:02

标签: processing mouseclick-event arduino-uno

我有8个简单的模拟土壤传感器连接到一个16通道模拟多路复用器,用于花园项目。传感器通过Arduino Uno读取。 Arduino还附有温度和光线传感器,每个传感器使用1个模拟引脚。我也在Arduino上运行Firmata,如果这有任何区别的话。

过去只有4个传感器连接到Arduino,但我必须扩展并加倍数字(因此多路复用器)。 mouseClicked功能适用于4个传感器,但不再增加4个传感器。添加了4个额外的传感器是否会影响Arduino的内存(即过载),或者是什么?我不确定问题是什么;任何帮助将不胜感激。

import processing.serial.*;
import java.awt.event.KeyEvent;
import javax.swing.JOptionPane;
import processing.video.*;
import cc.arduino.*;
Capture cam;
Arduino arduino;
Serial tinyg;
PImage bg;
PImage img; //green plant
PImage img2; //yellow sun
PImage img3; //brown plant
PImage img4; //gray sun
PImage img5; //plant petals
PFont font;
Serial port = null;
String portname = null;
boolean streaming = false;
float speed = 0.001;
String[] gcode;
int i = 0;
int lightReading=0;
int tempReading=0;
int toggle=0;
float voltage=0.0;
float tempc=0.0;
color off = color(4, 79, 111);
color on = color(84, 145, 158);
color brown = color(100, 59, 15);
color green = color(153, 204, 0, 0); //last number is alpha value for transparency
color black = color(000, 000, 000);
color transparentblack = color(000, 000, 000, 0);
float moisture_ref = 910.0; //change based on wet resistance value

int oldTime = 0;
int interval = 250;

int enable = 2;
int chan[][] = {
  {
    0, 0, 0, 0
  }
  , 
  {
    1, 0, 0, 0
  }
  , 
  {
    0, 1, 0, 0
  }
  , 
  {
    1, 1, 0, 0
  }
  , 
  {
    0, 0, 1, 0
  }
  , 
  {
    1, 0, 1, 0
  }
  , 
  {
    0, 1, 1, 0
  }
  , 
  {
    1, 1, 1, 0
  }
  , /*
  {
   0, 0, 0, 1
   }
   , 
   {
   1, 0, 0, 1
   }
   , 
   {
   0, 1, 0, 1
   }
   , 
   {
   1, 1, 0, 1
   }
   , 
   {
   0, 0, 1, 1
   }
   , 
   {
   1, 0, 1, 1
   }
   , 
   {
   0, 1, 1, 1
   }
   , 
   {
   1, 1, 1, 1
   } */
};

int sel[]= {
  3, 4, 5, 6
};

//array to hold soil values
int[] soilReading = new int[8];


void selectArduinoPort()
{
  if (port != null) port.stop();
  String result = (String) JOptionPane.showInputDialog(this, 
  "Select the serial port that corresponds to your Arduino board.", 
  "Select serial port", 
  JOptionPane.PLAIN_MESSAGE, 
  null, 
  Serial.list(), 
  0);

  if (result != null) {
    portname = result;
    arduino = new Arduino(this, portname, 57600);
  }
}

void selectTinygPort()
{
  if (port != null) port.stop();
  String result = (String) JOptionPane.showInputDialog(this, 
  "Select the serial port that corresponds to your TinyG board.", 
  "Select serial port", 
  JOptionPane.PLAIN_MESSAGE, 
  null, 
  Serial.list(), 
  0);

  if (result != null) {
    portname = result;
    tinyg = new Serial(this, portname, 115200);
  }
}

void setup() {
  size(800, 650);
  img = loadImage("plant5.png");
  img2 = loadImage("sun2.png");
  img3 = loadImage("plant8.png");
  img4 = loadImage("sun4.png");
  img5 = loadImage("plant9negative.png");

  String[] cameras = Capture.list();

  if (cameras == null) {
    println("Failed to retrieve the list of available cameras, will try the default...");
    cam = new Capture(this, 320, 240);
  } 
  if (cameras.length == 0) {
    println("There are no cameras available for capture.");
    exit();
  } 
  else {
    println("Available cameras:");
    for (int i = 0; i < cameras.length; i++) {
      println(cameras[i]);
    }
    cam = new Capture(this, cameras[8]);
    cam.start();
  }
  println(Arduino.list());
  font=loadFont("Helvetica-24.vlw");
  textFont(font);
  selectArduinoPort();
  selectTinygPort();
  //openSerialPort();
}

void draw() {
  tint(255, 235);
  background(0);

  stroke(255);
  fill(0);
  rect(20, 50, 50, 50, 3, 3, 3, 3);
  rect(20, 120, 50, 50, 3, 3, 3, 3);
  rect(20, 190, 50, 50, 3, 3, 3, 3);
  rect(20, 260, 50, 50, 3, 3, 3, 3);

  stroke(255);
  fill(0);
  rect(265, 55, 330, 250, 6, 6, 6, 6); //frame around the video feed

    font = loadFont("Helvetica-24.vlw");
  textFont(font, 32);
  fill(255); //font color
  text("Collaborators", width/5-50, 35); //text and location
  text("Aaron", width/5-20, 85);
  text("Angelika", width/5-20, 155);
  text("Alex", width/5-20, 225);
  text("Brett", width/5-20, 295);

  for (int i = 1; i <= 4; i++) {
    image(img, width/5*i-75, height/2+5);
  } //this is the top row of plant icons

  for (int i = 1; i <= 4; i++) {
    image(img, width/5*i-75, height/2+165);
  } //this is the bottom row of plant icons

  if (cam.available() == true) {
    cam.read();
  }
  image(cam, width/2-130, height/2-265);
  stroke(off);
  ellipseMode(CENTER);

  //////
  for (int i=0; i<8; i++)
  {
    arduino.digitalWrite(enable, 1);
    for (int j=0; j<4; j++)
    {
      arduino.digitalWrite(sel[j], chan[i][j]);
    }
    arduino.digitalWrite(enable, 0);
    delay(25);
    soilReading[i]=arduino.analogRead(0);
  }

  if ((millis()-oldTime)>interval)
  {
    //top row
    for (int i = 1; i <= 4; i++) {
      //print("sensor"+1+": "+arduino.analogRead(1)+" ");
      //println("");
      color col=lerpColor(brown, green, soilReading[i-1]/moisture_ref);//reads analog A0, interpolates color
      fill(col);
      stroke(col);
      ellipse(width/5*i-15, height/2+65, 120, 120); //width/5*i, height/2+300, 175, 175
      font = loadFont("Helvetica-24.vlw");
      textFont(font, 32);
      textAlign(CENTER);
      fill(255); //previously 0
      text(ceil(soilReading[i-1]/1024.0*100)+"%", width/5*i-10, height/2+154); //values below top row of plant icons
      //delay(50);
    }
    //bottom row
    for (int i = 1; i <= 4; i++) {
      //print("sensor"+1+": "+arduino.analogRead(1)+" ");
      //println("");
      color col=lerpColor(brown, green, soilReading[i*2-1]/moisture_ref);//reads analog A0, interpolates color
      fill(col);
      stroke(col);
      ellipse(width/5*i-15, height/2+225, 120, 120); //width/5*i, height/2+300, 175, 175
      font = loadFont("Helvetica-24.vlw");
      textFont(font, 32);
      textAlign(CENTER);
      fill(255); //previously 0
      text(ceil(soilReading[i*2-1]/1024.0*100)+"%", width/5*i-10, height/2+315); //values below bottom row of plant icons
      //delay(50);
    }
    oldTime = millis();
  }

  delay(50);
  //////


  for (int i = 1; i <= 4; i++) {
    image(img5, width/5*i-75, height/2+5);
  } //this is the overlay for the top row of plant icons

  for (int i = 1; i <= 4; i++) {
    image(img5, width/5*i-75, height/2+165);
  } //this is the overlay for the bottom row of plant icons


  //read the light sensor on pin A5
  lightReading=arduino.analogRead(5);
  delay(10);
  lightReading=arduino.analogRead(5);
  print("light: "+lightReading);
  delay(10);

  //read the temp sensor on pin A1
  tempReading=arduino.analogRead(1);
  delay(10);
  tempReading=arduino.analogRead(1);
  delay(10);

  //convert temp reading to degrees centigrade  
  voltage=tempReading*(5000/1024.0);
  tempc=ceil((voltage-500)/10.0);
  println(" temp: "+tempc);
  text(tempc+"\u00B0C", width/2+295, 270);

  image(img2, width/2+220, 80, 150, 150);

  // sun
  color col2=lerpColor(black, transparentblack, lightReading/900.0);//reads analog A05, interpolates color
  fill(col2);
  stroke(col2);
  ellipse(width/2+295, 155, 150, 150); //width/5*i, height/2+300, 175, 175
  fill(255); //previously 0
}

//manual motion

//this set is for the top row
void mouseClicked() {
  if (mouseY>80 && mouseY<200 && mouseX>325 && mouseX<445 && toggle==0) {
    tinyg.write("G91G1X250F5000\n");
    tinyg.write("G91G1Y70F5000\n");
    toggle=1;
    return;
  }
  if (mouseY>80 && mouseY<200 && mouseX>325 && mouseX<445 && toggle==1) {
    tinyg.write("G91G1X0F5000\n");
    tinyg.write("G91G1Y0F5000\n");
    toggle=0;
    return;
  }
  if (mouseY>240 && mouseY<390 && mouseX>325 && mouseX<445 && toggle==0) {
    tinyg.write("G91G1X250F5000\n");
    tinyg.write("G91G1Y275F5000\n");
    toggle=1;
    return;
  }
  if (mouseY>240 && mouseY<390 && mouseX>325 && mouseX<445 && toggle==1) {
    tinyg.write("G91G1X0F5000\n");
    tinyg.write("G91G1Y0F5000\n");
    toggle=0;
    return;
  }
  if (mouseY>405 && mouseY<555 && mouseX>325 && mouseX<445 && toggle==0) {
    tinyg.write("G91G1X250F5000\n");
    tinyg.write("G91G1Y500F5000\n");
    toggle=1;
    return;
  }
  if (mouseY>405 && mouseY<555 && mouseX>325 && mouseX<445 && toggle==1) {
    tinyg.write("G91G1X0F5000\n");
    tinyg.write("G91G1Y0F5000\n");
    toggle=0;
    return;
  }
  if (mouseY>570 && mouseY<720 && mouseX>325 && mouseX<445 && toggle==0) {
    tinyg.write("G91G1X250F5000\n");
    tinyg.write("G91G1Y725F5000\n");
    toggle=1;
    return;
  }
  if (mouseY>570 && mouseY<720 && mouseX>325 && mouseX<445 && toggle==1) {
    tinyg.write("G91G1X0F5000\n");
    tinyg.write("G91G1Y0F5000\n");
    toggle=0;
    return;
  }

  //this set is for the bottom row
  if (mouseY>80 && mouseY<200 && mouseX>485 && mouseX<605 && toggle==0) {
    tinyg.write("G91G1X350F5000\n");
    tinyg.write("G91G1Y70F5000\n");
    toggle=1;
    return;
  }
  if (mouseY>80 && mouseY<200 && mouseX>485 && mouseX<605 && toggle==1) {
    tinyg.write("G91G1X0F5000\n");
    tinyg.write("G91G1Y0F5000\n");
    toggle=0;
    return;
  }
  if (mouseY>240 && mouseY<390 && mouseX>485 && mouseX<605 && toggle==0) {
    tinyg.write("G91G1X350F5000\n");
    tinyg.write("G91G1Y275F5000\n");
    toggle=1;
    return;
  }
  if (mouseY>240 && mouseY<390 && mouseX>485 && mouseX<605 && toggle==1) {
    tinyg.write("G91G1X0F5000\n");
    tinyg.write("G91G1Y0F5000\n");
    toggle=0;
    return;
  }
  if (mouseY>405 && mouseY<555 && mouseX>485 && mouseX<605 && toggle==0) {
    tinyg.write("G91G1X350F5000\n");
    tinyg.write("G91G1Y500F5000\n");
    toggle=1;
    return;
  }
  if (mouseY>405 && mouseY<555 && mouseX>485 && mouseX<605 && toggle==1) {
    tinyg.write("G91G1X0F5000\n");
    tinyg.write("G91G1Y0F5000\n");
    toggle=0;
    return;
  }
  if (mouseY>570 && mouseY<720 && mouseX>485 && mouseX<605 && toggle==0) {
    tinyg.write("G91G1X350F5000\n");
    tinyg.write("G91G1Y725F5000\n");
    toggle=1;
    return;
  }
  if (mouseY>570 && mouseY<720 && mouseX>485 && mouseX<605 && toggle==1) {
    tinyg.write("G91G1X0F5000\n");
    tinyg.write("G91G1Y0F5000\n");
    toggle=0;
    return;
  }
}

//turn water pump on/off

void keyPressed() {
  arduino.digitalWrite(2, Arduino.HIGH);
}

void keyReleased() {
  arduino.digitalWrite(2, Arduino.LOW);
}

0 个答案:

没有答案