Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Servo.h>
- Servo myservo;
- int servo_output;
- int servo_pin = 3;
- int message;
- String temp;
- char ascii_table[] = {' ', '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*',
- '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5',
- '6', '7', '8', '9', ':', ';', '<', '=', '>', '?', '@',
- 'A', 'B', 'C', 'D', 'E', 'F', 'G','H', 'I', 'J', 'K', 'L',
- 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
- 'Y', 'Z', '[', '\\', ']', '^', '_', '`', 'a', 'b', 'c', 'd',
- 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
- 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{',
- '|', '}', '~'};
- char ascii_to_char(int ascii){
- return ascii_table[ascii - 32];
- }
- String capture_stream(){
- int input;
- String output;
- boolean flag = false;
- while((input = Serial.read()) != -1){
- output += String(ascii_to_char(input));
- flag = true;
- }
- if(flag){
- Serial.println(String(output));
- return output;
- }else{
- Serial.println(String("-1"));
- return "-1";
- }
- }
- void setup(){
- Serial.begin(9600);
- Serial.println("starting up");
- myservo.attach(servo_pin);
- }
- void loop(){
- temp = capture_stream();
- servo_output = temp.toInt();
- if(servo_output != -1){
- if(servo_output > 180){
- servo_output = 180;
- }else if(servo_output < 0){
- servo_output = 0;
- }
- myservo.write(servo_output);
- }
- delay(1000);
- }
Advertisement
Add Comment
Please, Sign In to add comment