sweeping a servo back & forth only ONCE

hey folks, so i came across some similar posts on this forum, but none that really help me.
So i have this project going, wherein, when i type in a particular word in processing it will - 1) play an appropriate video on processing, 2) send a serial value to arduino which changes the light of a rgb led strip & 3) triggers off the motor to turn from 0 to 180 and back to 0 and then STOP. now the problem is im not able to code this desired sweep action on the servo motor only ONCE. any ideas how to do that?? im quite a arduino / processing noob btw. here is the code. please help!
#include <Servo.h>

Servo myservo;

 char val; // Data received from the serial port
 int greenPin = 11;
 int redPin = 10;
 int bluePin = 9;
 int posTarget = 0;
 int posLastSet = 180;
 // Set the pin to digital I/O 13
 void setup() {
   pinMode(greenPin, OUTPUT); // Set pin as OUTPUT
   pinMode(redPin, OUTPUT);
   pinMode(bluePin, OUTPUT);
   myservo.attach(8);        // attaches the servo on pin 8 to the ser
   Serial.begin(9600); // Start serial communication at 9600 bps
 }
 void loop() {
   if (Serial.available()) 
   { // If data is available to read,
     val = Serial.read(); // read it and store it in val
   }
   
   
   
   //myservo.write(0);
   if (val == '0') 
   { // If 1 was received
    analogWrite(greenPin, 0);
    analogWrite(redPin, 0);
    analogWrite(bluePin, 255);
    //myservo.attach(8);
    myservo.write(posLastSet);
    /*if(posTarget != posLastSet){
    myservo.write(posTarget);
    posLastSet = posTarget;
    }*/
    
    
     // turn the LED on
   } else if ( val == '1') {
    analogWrite(greenPin, 0);
    analogWrite(redPin, 255);
    analogWrite(bluePin, 0);
    //myservo.write(0);
     // otherwise turn it off
   } else if ( val == '2'){
    analogWrite(greenPin, 255);
    analogWrite(redPin, 0);
    analogWrite(bluePin, 0);
   } else // leave the light at a dim white 
   {
    analogWrite(greenPin, 25);
    analogWrite(redPin, 25);
    analogWrite(bluePin, 25);
   }
   delay(10); // Wait 10 milliseconds for next reading
  /*if (val == 'a'){
    myservo.write(180);
    } else if (val == 'b'){
    myservo.write(0);
    } else {
    myservo.write(0);
    }*/
}

Why not make everything that comes after the Serial.read part of the same condition?
That way, you only do something when you receive new data.

Otherwise, just put a "while (1);" after the last thing you want to do.

make it a part of the same condition
by this do you mean, that i make an array of the values 0,1,2??

also the while(1); worked perfectly, but because of that, the next time arduino receives the value '0', the motor does nothing. thats not how i want it. i need it to sweep once every time a '0' value comes in...
thanks

I don't believe I mentioned arrays.

You have a condition based on whether there is serial data available to read.
If there is, you do only one thing, which is to read that data.
I'm saying, why not make everything conditional upon that one thing

aaa.i think i get what you mean. okay so im going to try that out and post again, cuz im pretty likely to mess something up.. :slightly_frowning_face:

alright worked! thanks AWOL!

Glad you stuck at it.
Thanks for getting back . . . Maybe post what you've got?

so here is the code. both processing & arduino. again, not the most well written, but seems to be doing the job pretty well!

PROCESSING CODE

import processing.serial.*;
import processing.video.Movie;
import ddf.minim.spi.*;
import ddf.minim.signals.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.ugens.*;
import ddf.minim.effects.*;

Minim minim;

static final int QTY = 3;
static final int QTY2 = 3;

final Movie[] movies = new Movie[QTY];
final AudioPlayer[] players = new AudioPlayer[QTY2];

int[][] myArray = new int[QTY][QTY2];

int idx;
int index;

String typing = "";
String saved = "";

Serial myPort;



void setup() { 
  size(200, 200, JAVA2D);
  frameRate(30);
  noSmooth();
  
  String portName = Serial.list()[2]; //change the 0 to a 1 or 2 etc. to match your port
  myPort = new Serial(this, portName, 9600);
  
  movies[0] = new Movie(this, "sin_city.mp4");
  movies[1] = new Movie(this, "blue_velvet.mp4");
  movies[2] = new Movie(this, "finale.mp4");
  
  minim = new Minim(this); 
 
  players[0] = minim.loadFile("one.mp3");
  players[1] = minim.loadFile("two.mp3");
  players[2] = minim.loadFile("three.mp3");
  
}



void draw() { 
  background(0);
  set(0,0,movies[idx] ); 
}



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



void keyPressed() {
  
  if (key == '\n' ) {
    saved = typing;
    typing = ""; // A String can be cleared by setting it equal to ""
    
    int k = keyCode, n = getMovieIndex(k) ;  
    
    if ( n == 0){
      myPort.write('0');
    } 
    
    else if (n == 1){
      myPort.write('1');
    }
    
    else if (n == 2){
      myPort.write('2');
    }
    
    if ( n>=0 & n != idx){// if string typed is NOT the same as previous string then...
      index = int(random(2));
    } 
    
    else if( n>=0 & n == idx){// if string typed is the same as previous string...
    
      if(index == 0){
        index = 1;
      } 
      
      else if (index == 1){
        index = 0;
      }  
    }  
    //println(index);
    
  } 
  
  else {
    // Otherwise, concatenate the String
    // Each character typed by the user is added to the end of the String variable.
    typing = typing + key; 
  }

  int k = keyCode, n = getMovieIndex(k) ;  
  
  if (n >= 0 & n!= idx){// if string typed is NOT the same as previous string then... 
    println(n);
    
    if (index == 0){
      movies[idx].pause();
      players[idx].pause();  
      movies[idx = n].play();
    } 
    
    else if (index == 1){
      movies[idx].pause();  
      players[idx].pause();  
      players[idx = n].play();
    }
    
  } 
  
  else if ( n >= 0 & n == idx ){// if string typed is the same as previous string...
    println(n); 
    
    if (index == 0){
      players[idx].pause();
      movies[idx = n].play();
    } 
    
    else if (index == 1){
      movies[idx].pause();
      players[idx = n].play();
    } 
    
  }
  
  else{
    println(n);  
  }

}

int getMovieIndex(int k){
  
  if ( saved.equals("blue")){
  return 0;
  }
  
  else if(saved.equals("red")){
  return 1;
  }
  
  else if(saved.equals("green")){
  return 2;
  }
  
  else {
  return -1;
  }

}

ARDUINO CODE

#include <Servo.h>

Servo myservo;

char val; // Data received from the serial port
int greenPin = 11;
int redPin = 10;
int bluePin = 9;


 
 void setup() {
   
   Serial.begin(9600); // Start serial communication at 9600 bps
   pinMode(greenPin, OUTPUT); // Set pin as OUTPUT
   pinMode(redPin, OUTPUT);
   pinMode(bluePin, OUTPUT);
   myservo.attach(8);  // attaches the servo on pin 8 to the ser
   myservo.write(0);
   analogWrite(redPin, 255);
   analogWrite(greenPin, 220);
   analogWrite(bluePin, 0);
 }
 
 
 
 void loop() {
       
   if (Serial.available() > 0){ // If data is available to read,
     val = Serial.read(); // read it and store it in val
     
     if (val == '0') { // change color to BLUE
       analogWrite(redPin, 0);  
       analogWrite(greenPin, 0);  
       analogWrite(bluePin, 255);  
    
       myservo.write(180); // 
       delay(1000); // turn servo back and forth 180 degrees, ONCE
       myservo.write(0); // 
     } 
     
     else if ( val == '1') { // change color to RED 
       analogWrite(redPin, 255);
       analogWrite(greenPin, 220);
       analogWrite(bluePin, 0);
     } 
    
     else if ( val == '2'){ // change color to GREEN
       analogWrite(redPin, 0);
       analogWrite(greenPin, 255);
       analogWrite(bluePin, 0);
     } 
   
   }
   
 }