Pages: [1]   Go Down
Author Topic: edit arduino variable with processing  (Read 314 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm working on a infrared radar project. I've found this website as a guideline: http://luckylarry.co.uk/arduino-projects/arduino-processing-make-a-radar-screen-part-3-visualising-the-data-from-sharp-infrared-range-finder/

I've succesfully managed to display the IR values in the radarscreen. However I need to adjust the speed of the servo motor when I press a button on my keyboard ('1' to slow the servo down and '2' to increase the rotating speed. I hope I'm explaining everything correctly. If not then please let me know.

This piece of code allows me to increase and decrease the servo speed by using the serial monitor. However when I use processing I won't be able to use the serial monitor cause processing is using the comm port.

Code:
#include <Servo.h>

Servo servo;
int IRpin = 1;
int positie = 0;
int snelheid = 5;
int readInput = 0;

void setup()
{
  servo.attach(9);
  Serial.begin(9600);
  Serial.println("Test");
}

void loop()
{
  if (Serial.available() > 0)
  {
    readInput = Serial.read();
   
    if(readInput == '1')
    {
      if(snelheid < 10) snelheid++;
    }
   
    if(readInput == '2')
    {
      if(snelheid > 1) snelheid--;
    }
  }
 
  for(positie = 0; positie < 180; positie++)
  {
    float volts = analogRead(IRpin) * 0.0048828125;
    float distance = 28 * pow(volts, -1.8);
    //Serial.println(distance);
    delay(snelheid);
    servo.write(positie);
  }
 
  for(positie = 180; positie >= 1; positie--)
  {
    float volts = analogRead(IRpin) * 0.0048828125;
    float distance = 28 * pow(volts, -1.8);
    //Serial.println(distance);
    delay(snelheid);
    servo.write(positie);
  }
}

I'm using this processing example from the website listed above. This allows the display of the IR readings in a radarscreen. Is it possible to write something within the serialEvent which allows me to increase/decrease a variable which I've defined in the Arduino part of the software. Thanks in advance.

Code:
/* get values from serial port */
void serialEvent (Serial myPort)

  String xString = myPort.readStringUntil('\n');  // read the serial port until a new line
    if(xString != null)                                // if theres data in between the new lines
    { 
        xString = trim(xString); // get rid of any whitespace just in case
    String getX = xString.substring(1, xString.indexOf("V")); // get the value of the servo position
    String getV = xString.substring(xString.indexOf("V")+1, xString.length()); // get the value of the sensor reading
    degree = Integer.parseInt(getX); // set the values to variables
    value = Integer.parseInt(getV);
        /*
        If our values are outside either end of the sensors range then convert them to the max/min for a better display without the spikes
        */
        if(value > 150)
        {
            value = 150;
        }
        if(value < 20)
        {
            value = 20;
        }
    oldValue[degree] = newValue[degree]; // store the values in the arrays.
    newValue[degree] = value; 
        /* sets a counter to allow for the first 2 sweeps of the servo */
    firstRun++;
    if(firstRun > 360)
        {
        firstRun = 360; // keep the value at 360
    }
    }
}
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 548
Posts: 46029
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Is it possible to write something within the serialEvent which allows me to increase/decrease a variable which I've defined in the Arduino part of the software.
In serialEvent(), or in draw() (or any other method), you can use myPort.write() to send data TO the Arduino, just like the Serial Monitor does.
Logged

Pages: [1]   Go Up
Jump to: