Go Down

Topic: if no data do nothing (Read 4268 times) previous topic - next topic

PaulS

Quote
but why is it always getting stuck on last cammand recieved
What do your serial prints tell you is happening?

Can the motor driver actually go forward and turn at the same time?
The art of getting good answers lies in asking good questions.

merkzilla

with the motor shield im using it has lights that go on for each function so that made it much easier to test but yes all the commands wori just cant seem to tell it to do nothing or the motordriver.stop command i did notice that in serial monitor the RX side puts out the right camand number for data than goes back to 0 im assuming its supposed to anyway sense im subtract the same data number from the data. i try to set it to if theres no data to motordrive stop and thats supposed to stop all motors of course
this is what i have now with all four if statements
Code: [Select]
#include <Servo.h>
#include <MotorDriver.h>

Servo camera;
int data = 0;

const int rx_led = 42;

void setup()
{
  //camera.attach(9);//camera control servo

  motordriver.init();
  motordriver.setSpeed(200, MOTORB);
  motordriver.setSpeed(200, MOTORA); // stering motor

  pinMode(rx_led, OUTPUT);
  digitalWrite(rx_led, LOW);

  pinMode(3, INPUT);
  pinMode(4, INPUT);
  pinMode(5, INPUT);
  pinMode(6, INPUT);
  pinMode(7, INPUT);
  attachInterrupt(1, RF_VT, RISING);
  Serial.begin(9600);
}
void loop()
{
  bool needToGoForward = false;
bool needToGoBackward = false;
bool needToTurnLeft = false;
bool needToTurnRight = false;
bool needToBreak = true;
  //direction
  if(data >= 8)
{
   needToTurnRight = true;
   needToBreak = false;
   data -= 8;
}
if(data >= 4)
{
   needToTurnLeft = true;
   needToBreak = false;
   data -= 4;
}
if(data >= 2)
{
   needToGoForward = true;
   needToBreak = false;
   data -= 2;
}
if(data >= 1)
{
   needToGoBackward = true;
   needToBreak = false;
   data -= 1;
}


if(needToGoForward)
{
   if(needToTurnLeft)
   {
     motordriver.goForward();
     motordriver.goLeft();
     
   }
   else if(needToTurnRight)
   {
     motordriver.goForward();
      motordriver.goRight();
   }
   else
      motordriver.goForward();
}


else if(needToGoBackward)
{
   if(needToTurnLeft)
   {
     motordriver.goBackward();
     motordriver.goLeft();
     
   }
   else if(needToTurnRight)
   {
     motordriver.goBackward();
      motordriver.goRight();
   }
   else
      motordriver.goBackward();
}

if(needToTurnLeft)
{
   if(needToGoForward)
   {
     motordriver.goForward();
     motordriver.goLeft();
     
   }
   else if(needToGoBackward)
   {
     motordriver.goBackward();
      motordriver.goLeft();
   }
   else
      motordriver.goLeft();
}
if(needToTurnRight)
{
   if(needToGoForward)
   {
     motordriver.goForward();
     motordriver.goRight();
     
   }
   else if(needToGoBackward)
   {
     motordriver.goBackward();
      motordriver.goRight();
   }
   else
      motordriver.goRight();
}


  /*
  val = map(val, 0, 1023, 0, 180);
  camera.write();
  */
  delay(50);
  Serial.print("data=");
  Serial.println(data, DEC);
}

//=======================================
//=======================================
void RF_VT() // interrupt service function
{
  data = (digitalRead(4) << 3) + (digitalRead(5) << 2) + (digitalRead(6) << 1) + (digitalRead(7) << 0);

}

PaulS

Code: [Select]
if(needToTurnLeft)
{
   if(needToGoForward)

No. No. No! You have already handled the need to go forward, while turning or not. You've already handled the need to go backwards, while turning or not.

The art of getting good answers lies in asking good questions.

merkzilla

sorry my bad i ment to chage this i was just trying things to see how the motor shield would react this is now

Code: [Select]
if(needToTurnLeft)
{
      motordriver.goLeft();
}
if(needToTurnRight)
{
      motordriver.goRight();
}

Undermentioned

Im not really getting what you trying to do but if your problem is just stopping your motors when theres no data, none of your if statements will be true if there is no data but your motors will continue on the last command they got so you have to put a else statement right at the bottom of your main loop to tell them to stop .

M....   

merkzilla

put a else statement right at the bottom of your main loop to tell them to stop .

M....   
you would think it would be so easy but i tried to set it as a esle statement and also if data == 0 to stop but it just doesnt

Undermentioned

If I was you I would just say:

Code: [Select]
Data = 0;

and not

Code: [Select]
Data -= 8// or what ever number you on;

And then does this function even work
Code: [Select]
motordriver.stop(); I dont know what library you are using.

Have you tested that function just with a simple sketch ?

merkzilla

yes the library has a example sketch that simply runs through all the camands and they work in the example

PaulS

Quote
If I was you I would just say:
Well, you should go back and read the entire thread. There is good reason for subtracting 8, 4, 2, and 1 rather than arbitrarily setting data (not Data - Data is a character on Star Trek - The Next Generation) to 0.

Quote
And then does this function even work
And, is it even being called? Serial.print() is better than guesswork 8 days a week.
The art of getting good answers lies in asking good questions.

Undermentioned

Ah ok I get it sorry also see theres two threads on this.

You got a long week Paul ;)

But Its true what Paul said, setup your else statement or == 0 statement and add a serial print and see if that line of code even gets executed.

If it does then you know theres something wrong with the stop function.

merkzilla

it has always worked for me i ont see why not in this scetch maybe if i set the bools back to orig settings t the end

if (needToBreak)
  {
    motordriver.stop();
    needToGoForward = false;
    needToGoBackward = false;
    needToTurnLeft = false;
    needToTurnRight = false;
  }

PaulS

Quote
maybe if i set the bools back to orig settings t the end
Not necessary. They are local to loop(), so at the top, they will be recreated/reinitialized.

Start putting Serial.print() statements in the code. You need to print data at the start of loop(), where you use it, and you need to print the conditions that are true as you manipulate data.

Then, you need to print the conditions that are true as you use the booleans.

Are you sure that the 8, 4, 2, and 1 actions match what the sender is actually sending?
The art of getting good answers lies in asking good questions.

merkzilla

Are you sure that the 8, 4, 2, and 1 actions match what the sender is actually sending?
yes i monitored both serial monitors and both data matched as the buttons were pressed

merkzilla

update i put a serial print in each
and this is the data im getting back now

forward button
going forwarddata=0

backward button- puts out nothing

left button- puts out nothing

right button-puts out this series of data

ForLeftBackleftgoing backwardTurn leftdata=1
ForLeftBackleftgoing backwardTurn leftdata=0
ForLeftBackleftgoing backwardTurn leftdata=1
going forwardgoing backwarddata=5
going forwardgoing backwarddata=5
ForLeftBackleftgoing backwardTurn leftdata=0
going forwardgoing backwarddata=5
ForLeftBackleftgoing backwardTurn leftdata=1
going forwardgoing backwarddata=5

Code: [Select]


#include <Servo.h>
#include <MotorDriver.h>
Servo camera;
int data = 0;

const int rx_led = 42;

void setup()
{
  //camera.attach(9);//camera control servo

  motordriver.init();
  motordriver.setSpeed(200, MOTORB);
  motordriver.setSpeed(200, MOTORA); // stering motor

  pinMode(rx_led, OUTPUT);
  digitalWrite(rx_led, LOW);

  pinMode(3, INPUT);
  pinMode(4, INPUT);
  pinMode(5, INPUT);
  pinMode(6, INPUT);
  pinMode(7, INPUT);
  attachInterrupt(1, RF_VT, RISING);
  Serial.begin(9600);
}
void loop()
{
  bool needToGoForward = false;
  bool needToGoBackward = false;
  bool needToTurnLeft = false;
  bool needToTurnRight = false;
  //data recieved for control
  if (data >= 8)
  {
    needToTurnRight = true;
    data -= 8;
  }
  if (data >= 4)
  {
    needToTurnLeft = true;
    data -= 4;
  }
  if (data >= 2)
  {
    needToGoForward = true;
    data -= 2;
  }
  if (data >= 1)
  {
    needToGoBackward = true;
    data -= 1;
  }

//forward commands
  if (needToGoForward)
  {
    if (needToTurnLeft)
    {
      Serial.print("ForLeft");
      motordriver.goForward();
      motordriver.goLeft();

    }
    else if (needToTurnRight)
    { Serial.print("ForRight");
      motordriver.goForward();
      motordriver.goRight();
    }
    else
    {
      motordriver.goForward();
      Serial.print("going forward");
    }
 
//backward commands
    if (needToGoBackward)
    {
      if (needToTurnLeft)
      {
        motordriver.goBackward();
        motordriver.goLeft();
        Serial.print("Backleft");
      }
      else if (needToTurnRight)
      {
        motordriver.goBackward();
        motordriver.goRight();
        Serial.print("BackRight");
      }
      else
        motordriver.goBackward();
      Serial.print("going backward");
    }

    if (needToTurnLeft)
    {
      motordriver.goLeft();
      Serial.print("Turn left");
    }
    if (needToTurnRight)
    {
      motordriver.goRight();
      Serial.print("Turn right");
    }


    /*
    val = map(val, 0, 1023, 0, 180);
    camera.write();
    */
    delay(50);
    Serial.print("data=");
    Serial.println(data, DEC);
  }
}
  //=======================================
  //=======================================
  void RF_VT() // interrupt service function
  {
    data = (digitalRead(4) << 3) + (digitalRead(5) << 2) + (digitalRead(6) << 1) + (digitalRead(7) << 0);

  }

PaulS

#29
May 31, 2015, 10:49 pm Last Edit: May 31, 2015, 10:51 pm by PaulS
You are trying to parse unknown data, and having problems. That is not surprising. So, the first issue to address is to stop trying to parse unknown data. In your case, that is incredibly simple. Simply print data (with identifier) at the top of loop().

Until you know that that value is correct, you are wasting your time (and ours).

Code: [Select]
    { Serial.print("ForRight");
NOTHING follows the {.

Code: [Select]
    if (needToTurnLeft)
    {
      motordriver.goLeft();
      Serial.print("Turn left");
    }
    if (needToTurnRight)
    {
      motordriver.goRight();
      Serial.print("Turn right");
    }

This is STILL useless crap. You have ALREADY dealt with all possible cases. GET RID OF THIS SHIT!

These should be else if's, to handle the case where you are trying to turn without moving. Though that hardly makes sense to me.
The art of getting good answers lies in asking good questions.

Go Up