edit arduino variable with processing
in
Integration and Hardware
•
1 year ago
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.
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.
- #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);
}
}
- /* 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
}
}
}
1