String inString = ""; // string to hold input
#include <Servo.h>
// Declare the Servo pin
int servoPin = 8 , servoPin2 = 9,servoPin3 = 10, servoPin4 = 11 ;
Servo Servo1, Servo2 , Servo3 , Servo4;
void setup() {
Servo1.attach(servoPin);
Servo2.attach(servoPin2);
Servo3.attach(servoPin3);
Servo4.attach(servoPin4);
Servo1.write(0);
Servo2.write(0);
Servo3.write(0);
Servo4.write(0);
// Open serial communications and wait for port to open:
Serial.begin(9600);
/*while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// send an intro:
Serial.println("\n\nString toInt():");
Serial.println();
*/
}
void loop() {
// Read serial input:
while (Serial.available() > 0) {
int inChar = Serial.read();
if (isDigit(inChar)) {
// convert the incoming byte to a char and add it to the string:
inString += (char)inChar;
}
// if you get a newline, print the string, then the string's value:
if (inChar == '\n') {
Serial.print("Value:");
Serial.println(inString.toInt());
int value = inString.toInt();
if(value>=0 && value<=90){
Serial.println(value);
int servo1_position = map(value, 0, 90, 0, 180);
Servo1.write(servo1_position);
}
if(value>90 && value<=181){
Serial.println(value);
int servo2_position = map(value, 91, 181, 0, 180);
Servo2.write(servo2_position);
}
if(value>181 && value<=272){
Serial.println(value);
int servo3_position = map(value, 182, 272, 0, 180);
Servo3.write(servo3_position);
}
if(value>272 && value<=363){
Serial.println(value);
int servo4_position = map(value, 273, 363, 0, 180);
Servo4.write(servo4_position);
}
inString = "";
//Serial.print("String: ");
//Serial.println(inString);
// clear the string for new input:
//
}
}
}
Comments