arduino -processing

edited April 2016 in Arduino

Can someone help me?

I'm new to processing.i'm trying to write a code that will receive arduino serial data and take a webcam photo if a value goes high.I've written the code but its giving an error "unexpected token :void". here's the code. please help.

import java.awt.Robot; import java.awt.event.KeyEvent; import java.awt.AWTException; Robot robot; import processing.serial.*; import processing.video.*; Serial myPort; int val; int number = 0; Capture cam; float value; int read; void setup() { try{ robot=new Robot(); } catch (AWTException e){ e.printStackTrace(); exit(); } size(640, 480); String portName=Serial.list()[0]; myPort=new Serial(this,portName,9600);

String[] cameras = Capture.list();

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]); }

// The camera can be initialized directly using an 
// element from the array returned by list():
cam = new Capture(this, cameras[0]);
cam.start();     

}
}

void draw() { if (cam.available() == true) { cam.read(); } image(cam, 0, 0); // The following does the same, and is faster when just drawing the image // without any additional resizing, transformations, or tint.

val=myPort.read(); if(val>120){ robot.keyPress(KeyEvent.VK_SPACE); //If we want a delay here, that gets a little bit more complicated... robot.keyRelease(KeyEvent.VK_SPACE); } void keyPressed() { //Detect space key presses (to show that it works) if(key == ' ') { save(screen.jpg); } }

Tagged:

Answers

  • Please make your code readable by hitting ctrl+t in processing then when pasting here, highlight it and click the C button (ctrl+o). Make sure there is one white line before and after the code.

    https://forum.processing.org/two/discussion/32/how-to-format-text-and-code-on-the-new-forum/p1

    Your error is usually a syntax error. Try looking for missing ";" or too little or too much {}. Ctrl+t helps you find the error.

  • import java.awt.Robot;
    import java.awt.event.KeyEvent;
    import java.io.IOException;
    import java.awt.AWTException;
    Robot robot;
    import processing.serial.*;
    import processing.video.*;
    Serial myPort;
    int val;
    float state=0;
    int number = 0;
    Capture cam;
    int sensor=0;
    float value;
    int read;
    void setup() {
      try {
        robot=new Robot();
      }
      catch (AWTException e) {
        e.printStackTrace();
        exit();
      }
      size(640, 480);
      String portName=Serial.list()[0];
      myPort=new Serial(this, portName, 9600);
    
    
    
      String[] cameras = Capture.list();
    
      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[0]);
        cam.start();
      }
    }
    
    void draw() {
      if (cam.available() == true) {
        cam.read();
      }
      image(cam, 0, 0);
    
      val=myPort.read();
      if (val>120) {
        robot.keyPress(KeyEvent.VK_SPACE);
            robot.keyRelease(KeyEvent.VK_SPACE);
      }
      void keyPressed() {   
        if (key==' ') {
          String s = "screen" + nf(number, 4) +".jpg";
          save(s);
          number++;
          println("Done saving.");
        }
      }
    
  • Missing } after line 55.

  • edited April 2016

    i've edited the code . i want to add a command that will read the arduino input "val" through serial and take a photo through the webcam if 'val' goes higher than 120. how can i do that?

        import processing.serial.*;
        import processing.video.*;
        Serial myPort;
        int val;
        float state=0;
        int number = 0;
        Capture cam;
        int sensor=0;
        float value;
        int read;
        void setup() {
    
          size(640, 480);
          String portName=Serial.list()[0];
          myPort=new Serial(this, portName, 9600);
    
    
    
          String[] cameras = Capture.list();
    
          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[0]);
            cam.start();
          }
        }
    
        void draw() {
          if (cam.available() == true) {
            cam.read();
          }
          image(cam, 0, 0);
        }
    
  • What do you want to do with the photo after you've taken it? Just display it on the screen?

    Can you write code so that you print some message to the console when val is above 120?

  • its a security system.I want to save the photo when the 'val' from arduino goes above 120.

    i am sending 'val' from arduino to processing through serial but i am unable to save the photo when 'val' goes above 120.

  • So you want to save the photo to disc?

    This one compiles but you'll have to check and debug yourself:

    import processing.serial.*;
    import processing.video.*;
    
    Serial myPort;
    Capture cam;
    
    
    int val;
    float state=0;
    int number = 0;
    
    int sensor=0;
    float value;
    int read;
    
    boolean check = false;
    boolean saveImage = false;
    
    void setup() {
      size(640, 480);
      String portName=Serial.list()[0];
      myPort=new Serial(this, portName, 9600);
    
      String[] cameras = Capture.list();
    
      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[0]);
        cam.start();
      }
    }
    
    
    
    
    
    void draw()
    {
      if (check != saveImage && saveImage)
      {
        if (cam.available())
        {
          cam.read();
          background(0);
          image(cam, 0, 0);
          cam.save("screen" + nf(number++, 4) +".jpg");
          saveImage = false;
        }
        check = saveImage;
      }
    }
    
    
    
    
    void serialEvent(Serial port)
    {
      int inByte = port.read();
      if (inByte > 120) saveImage = true;
      else saveImage = false;
    }
    
  • edited April 2016

    It works ! thanks a lot.

Sign In or Register to comment.