"save" variable read from serial for the next loop.

Hello all ! :wink:
i got another question...
here´s a short version of my code :

...
int size1;
const int valve = 12; 
...
void setup() { 
  pinMode(valve, OUTPUT); 
  Serial.begin(9600);  
}

void loop() {
...
// here I´m reading stuff from the Serial port and convert it etc.
...
size1 = Data[1].toInt() ;
...
/////////////////////////////// ** I´ll describe this part 
digitalWrite(valve, HIGH);
delay(size1);
digitalWrite(valve, LOW);
delay(100);
}
////////////////////////////// **

** Here is my Problem :

I´ve written a programm that lets me control falling water-drops through a GUI I made with Visual Basic. The Serial Reading, converting etc. works pretty good.
In the **-part , the final dropping does happen.
The valve opens as long as the value of the variable size1.Then the loop is over. In the second loop, i want the valve to drop again.
So the variable size1 should keep its value for the next loops. (Until i send another value).
But at the moment the valve just makes a drop whenever i press my Drop-Button in visual basic...
I hope you guys understand :smiley:

You'll need to post more of your code. It appears as though size1 is a global variable, so it should retain it's value from one iteration of loop to the next. That is seems to not do that means that you are overwriting it somewhere - unintentionally, perhaps. But, we can't see that...

You´ve already solved my question.. or helped me finding the mistake.
I´d been overwriting it because the line
size1 = Data[1].toInt() ;
did also happen when i didnt send something to the serial port. So Data[] was empty and the size1,too, of course. :smiley:
i´ve now moved it into the
if(Serial.available()){ part.. and it works ..
thanks :slight_smile: