Reading multiple values in processing > Help needed!

edited May 2017 in Arduino

Hello,

For a school project i’m making an interactive installation where you can make a digital ball go higher with a sensor. I’m trying to make this work with processing and Arduino. Several things are working, but for some reason i can’t combine everything to one smooth proces. I think this is because i try to read two different sensor values.

The steps that i want: - First image is TRUE - When the photoresistor value is between 2 values, First image is FALSE - Then a ball is displayed - When the ball is on top of the screen, a second image is TRUE - When you click a push button a servo turns and the second image is FALSE. After that the first image becomes TRUE again

All of this is working, except for the photoresistor and push button part. For now it works when you press DOWN or ENTER.

I really hope someone can help me with this. I can provide additional information if needed.

The scripts i’m using now:

PROCESSING: import processing.serial.*; // import the Processing serial library

        Serial FirstArduino;
        float yTrim, gFill;
        Ball ball1;

        PImage a;
        PImage b;
        PImage c;
        boolean welcome = true;
        boolean corner = false;

        void setup() {
          fullScreen();
          smooth();
          noStroke();
          background(0);
          a = loadImage("ball.png");
          b = loadImage("1scannen.png");
          c = loadImage("2balweg.png");

          ball1 = new Ball (600, 0);
          ball1.initialiseBall();
          FirstArduino = new Serial(this, "/dev/tty.usbmodem1421",9600);
          // read incoming bytes to a buffer
          // until you get a linefeed (ASCII 10):
          FirstArduino.bufferUntil('\n');
        }

        void draw() {
          fill(0, 0, 0, height);
          rect(0, 0, width, height);
          ball1.run();
          if (welcome) image(b, 0, 0, width, height);
          if (corner) image(c, 0, 0, width, height);

        }

        void serialEvent(Serial myPort) {
          // read the serial buffer:
          String myString = FirstArduino.readStringUntil('\n');
          if (myString != null) {
            myString = trim(myString);

            // split the string at the commas
            // and convert the sections into integers:
            int sensors[] = int(split(myString, ','));
            //Next, print out those three integers using a for() loop, like so:

            for (int sensorNum = 1; sensorNum < sensors.length; sensorNum++) {
              // print("Sensor " + sensorNum + ": " + sensors[sensorNum] + "\t");
            }

            // add a linefeed at the end:
            //println();
            // make sure you've got all three values:
            if (sensors.length > 1) {
              gFill= sensors[1];
              yTrim = 750-sensors[1]*8.5;
              yTrim += (0);
            }

              if (keyCode == DOWN) {
              welcome = false;

            }

            if (v1.y < 0) {
              corner = true;
            }

                if (keyCode == ENTER) {
              corner = false;
              welcome = true;
            }
          }

        }

ARDUINO:

    #include <Servo.h>
                Servo myservo;  // create servo object to control a servo
                // twelve servo objects can be created on most boards

                int pos = 0;    // variable to store the servo position
                int sensorPin = A0;
                int servoPin = 9; 
                int pushPin = 2;   // choose the input pin (for a pushbutton)
                int lowThreshold = 450;
                int highThreshold = 150;
                int sensorValue = 0;
                int val;    // variable to read the value from the analog pin
                int ldrValue;
                int ldrPin = 3;      // photoresistor pin

                void setup() {
                   Serial.begin(9600);            // Start serial communication at 9600 bps
                   pinMode(sensorPin, INPUT);
                   pinMode(servoPin, OUTPUT);
                   pinMode(pushPin, INPUT);    // declare pushbutton as input
                   digitalWrite(sensorPin, LOW);
                   digitalWrite(servoPin, LOW);
                   myservo.attach(9);  // attaches the servo on pin 9 to the servo object
                }

                void loop() {
                  for (int thisSensor = 0; thisSensor < 2; thisSensor++) {
                    int sensorValue = analogRead(thisSensor) / 3;
                    Serial.print(sensorValue);
                    // if you're on the last sensor value, end with a println()
                    // otherwise, print a comma
                    if (thisSensor == 1) {
                    Serial.println();
                  } else {
                    Serial.print(",");
                      }
                    }

                    myservo.write(0);
                    val = digitalRead(pushPin);  // read input value
                    if (val == HIGH) {         // check if the input is HIGH (button released)
                      delay(500);
                      for (pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
                      { // in steps of 1 degree
                        myservo.write(180);              // tell servo to go to position in variable 'pos'
                        delay(1);                       // waits 15ms for the servo to reach the position
                      }
                      delay(300);
                      for (pos = 180; pos >= 1; pos -= 1) // goes from 180 degrees to 0 degrees
                      {
                        delay(1);                       // waits 15ms for the servo to reach the position
                        myservo.write(0);              // tell servo to go to position in variable 'pos'
                      }
                    } else {
                    }

                  ldrValue = analogRead(A3)/4; 
                  Serial.println(ldrValue);  // Send the value
                  delay(100);  // Wait 100ms for next reading
                }

Thanks in advance!

Tagged:

Answers

    • Processing:

    Question: Why are you accessing the keyCode inside the serial event? Are you doing this for testing purposes? If so, the proper way to do this is to do it in a key event call back. For example, keyReleased(): https://processing.org/reference/keyReleased_.html

    Question: fill(0, 0, 0, height); This is not proper. Please check: https://processing.org/reference/fill_.html
    Also https://processing.org/reference/color_.html

    • Arduino:

    Question: Just to verify, are the values correct:

    for (int thisSensor = 0; thisSensor < 2; thisSensor++){
      int sensorValue = analogRead(thisSensor) / 3;
      ...
    

    but your input are sensorpin and pushpin. Do they have values of 0 and 1? From your code, it looks pushpin doesn't. Also, you have: digitalWrite(sensorPin, LOW); but this is an input. Why are you writing on it? Does the documentation states to do so?

    Kf

    • First image is TRUE

    • When the photoresistor value is between 2 value s: First image is FALSE
      Then a ball is displayed and moves

    • When the ball is on top of the screen:
      A second image is TRUE

    • When you click a push button:
      A servo turns and the second image is FALSE.

    • Servo done?:
      After that the first image becomes TRUE again

    Base on the layout of your code, you should consider using states in your program. Think of every state as a page. When you are done the state, you flip the page. This next post provides an example code using pages: https://forum.processing.org/two/discussion/558/creating-a-next-page-button

    This means you will need to drop the boolean values in processing and change to using a single int field to keep track of the current page.

    final int PAGE0=0;
    final int PAGE1=1;
    final int PAGE2=2;
    final int NPAGES=PAGE2+1;
    
    int currentPage;
    
    void setup(){
        currentPage=PAGE0;
    }
    
    void draw(){
    
        if(currentPage==PAGE0){
    
        } else if(currentPage==PAGE1){
    
        }else if(currentPage==PAGE2){
    
        }    
    }
    
    void mycustomEvent(){  //Serial,keyEvent, mouseEvent, etc...
        currentPage++;
        currentPage=(currentPage+1)%NPAGES;  //Ensures values stays in range 0 to n.
    }
    

    Kf

Sign In or Register to comment.