for some reason my serial.available is acting really funny, it wont print the line Serial.print("serial avail passed val: "); but it seems to be receiving data...in some way because I can see the serial monitor reacting to processing program when I run it. It seems that serial.available() is just not passing and returning 0 or null. any idea?
//SERVO stuff
#include <Servo.h>
Servo myservo; // create servo object, max 8 servo objects
// incoming serial data from processing
String serialDataIn;
String data[3];
int counter;
int inbyte;
void setup() {
Serial.begin(9600);
myservo.attach(3); // server is on pin 3
counter = 0;
int pos = 0;
}
void loop() {
//sensor 1
float distance = analogRead(1);
//map float to an int
int x = (int) distance;
Serial.print("initial value: "); Serial.println(x);
// SERVO stuffs
int incomingByte = 0;
if( x > 400 && x < 750){
Serial.write("length");
if (Serial.available()) {
Serial.print("serial avail passed val: ");
delay(1);
inbyte = Serial.read();
Serial.println(inbyte);
if(inbyte >= '0' & inbyte <= '9')
serialDataIn += inbyte;
if (inbyte == ','){ // Handle delimiter
data[counter] = String(serialDataIn);
serialDataIn = String("");
counter = counter + 1;
}
if(inbyte == '\r'){ // end of line
//handle end of line a do something with data
}
// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte, DEC);
}
myservo.write(x); // tell servo to go to position in variable 'pos'
delay(25);
}
}