Go Down

Topic: How to control servo motor via user serial monitor analog input? (Read 1 time) previous topic - next topic

gnozahs

Could someone link me a good write up or tutorial on controlling a servo motor and making it move to a certain degree based on user input on the serial monitor? For example, if I wanted it to move to 180 degrees, I would type in 180 and the servo motor would move to 180 degrees.

AWOL

Wait a few minutes, and zoomkat will be along to post some code.   :smiley-yell:
Or you could save time and search for it.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

zoomkat

First time in a couple of days!

Code: [Select]

// zoomkat 10-4-10 serial servo test
// type servo position 0 to 180 in serial monitor
// for writeMicroseconds, use a value like 1500
// for IDE 0019 and later
// Powering a servo from the arduino usually DOES NOT WORK.

String readString;
#include <Servo.h>
Servo myservo;  // create servo object to control a servo

void setup() {
  Serial.begin(9600);
  myservo.writeMicroseconds(2000); //set initial servo position if desired
  myservo.attach(7);  //the pin for the servo control
  Serial.println("servo-test-21"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    delay(1); 
    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      readString += c; //makes the string readString
    }
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured string
    int n;
    char carray[6]; //converting string to number
    readString.toCharArray(carray, sizeof(carray));
    n = atoi(carray);
    myservo.writeMicroseconds(n); // for microseconds
    //myservo.write(n); //for degees 0-180
    readString="";
  }
}
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

gnozahs


First time in a couple of days!

Code: [Select]

// zoomkat 10-4-10 serial servo test
// type servo position 0 to 180 in serial monitor
// for writeMicroseconds, use a value like 1500
// for IDE 0019 and later
// Powering a servo from the arduino usually DOES NOT WORK.

String readString;
#include <Servo.h>
Servo myservo;  // create servo object to control a servo

void setup() {
  Serial.begin(9600);
  myservo.writeMicroseconds(2000); //set initial servo position if desired
  myservo.attach(7);  //the pin for the servo control
  Serial.println("servo-test-21"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    delay(1); 
    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      readString += c; //makes the string readString
    }
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured string
    int n;
    char carray[6]; //converting string to number
    readString.toCharArray(carray, sizeof(carray));
    n = atoi(carray);
    myservo.writeMicroseconds(n); // for microseconds
    //myservo.write(n); //for degees 0-180
    readString="";
  }
}



I put this code inside my already exisiting code and when I enter in a number, ie. 0, it automatically goes to 180 degrees. It does this for any number I input. And also, it only reads an input once, I can't enter another number in and make it move again.

zoomkat

Quote
I put this code inside my already exisiting code and when I enter in a number, ie. 0, it automatically goes to 180 degrees. It does this for any number I input. And also, it only reads an input once, I can't enter another number in and make it move again.


Then I would suspect something incompatable with your existing code.
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

gnozahs

Here is part of my code so far.

How do I make it so that when the "System deactived" pops up on the serial monitor after hitting my push button, it deletes the "System active Enter degree(s) 0-180." line and just displays "System deactived". Right now it's showing:
System active
Enter degree(s) 0-180.
System deactivated



Code: [Select]
#include <Servo.h>                         

int buttonPin = 2;              // pushbutton switch is connected to pin 2
int ButtonMode = 0;
int pushbutton;                        // variable for reading the pin status
int doublecheck;                       // variable for reading the delayed status
int buttonState;                // variable to hold the button state; initial

Servo servomotor;

int Hundreds;
int Tens;
int Ones;
int Good_value;
int Degrees =0;
int lol=0;

int var = 0;

int outputValue = 0;



void setup() {
  pinMode(buttonPin, INPUT);    // Declare pushbutton as input


servomotor.attach(9); // servo is on pin 9


 
  Serial.begin(9600);           
  buttonState = digitalRead(buttonPin);   // read the initial state
}

void loop(){
 
  pushbutton = digitalRead(buttonPin);      // read input value and store it in pushbutton
  delay(10);                                // 10 milliseconds
  doublecheck = digitalRead(buttonPin);     // read the input again to check for bounces
  if (pushbutton == doublecheck) {          // make sure we got 2 consistant readings
    if (pushbutton != buttonState) {        // the button state has changed
      if (pushbutton == LOW) {             // check if the button is pressed
        if (ButtonMode == 0) {               // if its off
          ButtonMode = 1;           // First time push
          Serial.println("System active");
  Serial.println("Enter degree(s) 0-180.");
     
    } else {
          if (ButtonMode == 1) {             // After first push
            ButtonMode = 0; // Go onto the 2nd case
            Serial.println("System deactived");
           
          }
        }
      }
    }
    buttonState = pushbutton;                 // save the new state in our variable
  }




  // Different motor modes for each push of the button
  if (ButtonMode == 0) {                // initial mode
 
  servomotor.write(90); // set servo to 90 degree position
  }

PaulS

Quote
How do I make it so that when the "System deactived" pops up on the serial monitor after hitting my push button, it deletes the "System active Enter degree(s) 0-180." line and just displays "System deactived".

To do this, you need to use something other than the Serial Monitor that comes with the Arduino. What you use needs to understand something like VT100, and you need to include appropriate VT100 codes in what you send to make it clear the screen.

In a former job, I knew all about how to make VT100 screens do all kinds of cool stuff, but that was 25 years ago.

Go Up