Quantcast
Channel: Arduino - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 747

Reading multiple values in processing > Help needed!

$
0
0

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!


Viewing all articles
Browse latest Browse all 747

Latest Images

Trending Articles



Latest Images