Go Down

Topic: servo control issue (Read 591 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

Go Up