1
votes

My project is to create a robot Arduino flower with two sensors: light and pressure, that opens and closes according to the values in those two sensors, and at the same time plays different videos through Processing according to those values.

I've been having a really hard time passing on the value from the analog sensors in Arduino too processing. This is what I have so far:

Arduino

#include <Servo.h> 

Servo myservo;
int pressure = 0;
int light = 1;
int pre;
int val;

void setup()
{
    Serial.begin(9600);
    myservo.attach(2);
}

void loop() {
    pre = analogRead(pressure);
    pre = map(pre, 918, 1023, 255, 0);

    val = analogRead(light);
    val = map(val, 0, 255, 0, 127);
    Serial.print(val, DEC);
    /*if (val>50) {
        Serial.print(1);
    }
    else {
        Serial.print(0);
    }*/

    myservo.write(val);
}

Processing

import processing.video.*;
import processing.serial.*; //arduino
Serial myPort;  // Create object from Serial class
int val;


Movie movie;

void setup() {
  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);
  size(640, 360);
  background(0);
  movie = new Movie(this, "transit.mov");
  movie.loop();

}

void movieEvent(Movie m) {
  m.read();
}

void draw() {
  val = myPort.read();
  println(val);
  //if (movie.available() == true) {
  //  movie.read(); 
  //}
  image(movie, 0, 0, width, height);
  if (val==49) {
    movie.jump(1);
  }
}

For now I'm just trying to make the video react to the light sensor, but haven't been able. All I get from the Processing readings is 48. The motor reacts fine to the sensors.

1

1 Answers

0
votes

What output do you get from a simpler Processing sketch like this? What output would you expect?

import processing.serial.*;

Serial myPort;  // The serial port

void setup() {
  // List all the available serial ports
  println(Serial.list());
  // Open the port you are using at the rate you want:
  myPort = new Serial(this, Serial.list()[0], 9600);
}

void draw() {
  while (myPort.available() > 0) {
    int inByte = myPort.read();
    println(inByte);
  }
}

Also, why is your map function in your Arduino code pre = map(pre, 918, 1023, 255, 0); using 250 and 0 as your lower bound and upper bound respectively? That seems backwards. Should it not read pre = map(pre, 918, 1023, 0, 255);?