Go Down

Topic: servo control issue (Read 597 times) previous topic - next topic

mustang493

Can anyone help me with this code.  The serial print at the end of the code does not print.  Which makes me assume the program is sticking before the end.  this would make sense as the motor and servo do not operate.

Code: [Select]
#include <Servo.h>
#define midlimit 1700
Servo myservoA;

int Abrake = 9;//brake signal for motor A
int motorspdA = 3;//PWM speed signal for motor A
int ch5;
int motorA = 12;
int sensorPinA = A0;    // select the input pin for the current
int switchpin = 22;
int sensorValueA = 0;  // variable to store the value coming from the sensor

void setup () {

  pinMode (44, INPUT);//signal from the RC reciever channel 5
  pinMode (12, OUTPUT);//motor A enable and direction HIGH or LOW
  pinMode (9, OUTPUT);//Brake motor A
  pinMode (4, OUTPUT);//servo A pin
  pinMode (switchpin, OUTPUT);
  myservoA.attach(4);

}

void loop () {
  Serial.begin(9600);
  int limitA = 300; //this is the current limit for motor A
  sensorValueA = analogRead(sensorPinA); //this reads the value of current for motorA and sets
  // the variable sensorValueA
  int iA = 0; //this creates and sets a variable named iA which we will use to make sure the
  //motors stop once they reach overcurrent condition
  myservoA.writeMicroseconds(900);
  ch5 = pulseIn(44, HIGH);
  Serial.println(ch5); 

  if (ch5 < midlimit) {
    digitalWrite(switchpin, HIGH);
  }
  else {
    digitalWrite(switchpin, LOW);

  }
  int updwn = digitalRead(switchpin); //this sets the value of the variable updwn which is
    // read from transmitter switch
  int updwn2 = digitalRead(switchpin); //this sets the comparison value of updwn which is
    // read from transmitter switch
  Serial.println(updwn);
  delay(300);
  while (updwn == updwn2) { //the while loop here knows the current state of the switch
    // updwn. during the while loop it will continually check
    //the state of the switch by setting updwn2 therefore once the state of the switch
    //changes it will exit the while loop


    if (updwn==1 && sensorValueA < limitA && iA==0) {
      analogWrite  (motorspdA,255);
      digitalWrite (motorA,HIGH);
      digitalWrite (Abrake,LOW);
    }
    else if (updwn==0 && sensorValueA < limitA && iA==0) {
      analogWrite  (motorspdA,255);
      digitalWrite (motorA,LOW);
      digitalWrite (Abrake,LOW);
    }
    else {
      analogWrite  (motorspdA,0);
      digitalWrite (Abrake,LOW);
      myservoA.writeMicroseconds(2125);
      iA++;
    }
  }
  delay (200);
  sensorValueA = analogRead(sensorPinA);
  int ch5 = pulseIn(44, HIGH);
 
  if (ch5 < midlimit) {
    digitalWrite(switchpin, HIGH);
  }
  else {
    digitalWrite(switchpin, LOW);
  }
  updwn2 = digitalRead(switchpin);
  Serial.println(updwn2);
}

billroy

How are the motors connected?

It's unusual for Serial.begin() to be in loop().  You probably want it in setup().

Sprinkle a few more Serial.print() statements, one in setup, one at the start of loop(), one in the middle.  They may turn up more data.

-br


mustang493

The motor is connected to an arduino motor shield mounted on the mega.

mustang493

The servo signal is taken from the arduino pin 4, with the power supply coming from the rc receiver power.  The gnd from the rc power is also connected to the Arduino gnd.

johnwasser

Code: [Select]

  int updwn = digitalRead(switchpin);
  int updwn2 = digitalRead(switchpin);
  while (updwn == updwn2) {
  }


Since the contents of the while() loop does not change 'updwn' or 'updwn2' the loop will never end.  Any code after the loop will never be executed.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

mustang493

Johnwasser, thats why i set the code at the end of the program within the while loop, it looks at the switchpin i.e. the position of the transmitter switch and if its changed it makes updwn 2 a different value to updwn.  thus it exists the loop.

billroy

Sorry, mustang, I believe Jon nailed it.  Your update of updwn2 is outside the while loop.

-br

mustang493

yes i see that now.  thanks guys

mustang493

John, Guys if you're still looking at this thread i still have a problem.  motor now operates according to the loop but the servo is not operating.

PaulS

Quote
but the servo is not operating.

So, we'll ask the same questions about the servo. How is it (all three wires) connected?

zoomkat

Simple code for testing a servo.

Code: [Select]

// zoomkat 10-22-11 serial servo test
// type servo position 0 to 180 in serial monitor
// or for writeMicroseconds, use a value like 1500
// for IDE 0022 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(1500); //set initial servo position if desired
  myservo.attach(7);  //the pin for the servo control
  Serial.println("servo-test-22-dual-input"); // so I can keep track of what is loaded
}

void loop() {
  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the string readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured string
    int n = readString.toInt();  //convert readString into a number

    // auto select appropriate value, copied from someone elses code.
    if(n >= 500)
    {
      Serial.print("writing Microseconds: ");
      Serial.println(n);
      myservo.writeMicroseconds(n);
    }
    else
    {   
      Serial.print("writing Angle: ");
      Serial.println(n);
      myservo.write(n);
    }

    readString=""; //empty for next input
  }
}

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   8)

mustang493

yes all three wires are connected.  i'm going to check some of the connectors again as i can't see why this code will not work. will also try the servo test code.

mustang493

Paul, guys found the problem,  bad gnd connection on the rc receiver battery to the Arduino mega.

Go Up