if no data do nothing

how can i tell this if there is no incoming data to do nothing at the moment it just gets stuck on the last camand recieved

#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(50, 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()
{
  //direction
  

 if (data == 8)
    {
      digitalWrite(rx_led, HIGH);
      motordriver.goRight();
    }
    if (data == 4)
    {
      digitalWrite(rx_led, HIGH);
      motordriver.goLeft();
    }
    if (data == 2)
    {
      digitalWrite(rx_led, HIGH);
      motordriver.goForward();
    }
    if (data == 1)
    {
      digitalWrite(rx_led, HIGH);
      motordriver.goBackward();
    }
    if (data == 6)
    {
      digitalWrite(rx_led, HIGH);
      motordriver.goForward();
      motordriver.goLeft();
    }
    if (data == 10)
    {
      digitalWrite(rx_led, HIGH);
      motordriver.goForward();
      motordriver.goRight();
    }
    if (data == 5)
    {
      digitalWrite(rx_led, HIGH);
      motordriver.goBackward();
      motordriver.goLeft();
    }
    if (data == 9)
    {
      digitalWrite(rx_led, HIGH);
      motordriver.goBackward();
      motordriver.goRight();
    }
    if (data)
    {
     motordriver.stop();
    Serial.println("stop");
    }
  /*
  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);

}

Change all but the first If to Elseif 's, then the last should be just an else for no data. Then in each if statement, reset the data value to your nodata value. Case/Switch would also work here.

updated code to this but still just loops the last signal recieved its starts out doing nothing or the "stop" function i have but as soon as i send it a cammand it just loops it

#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(50, 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()
{
  //direction
  

 if (data == 8)
    {
      digitalWrite(rx_led, HIGH);
      motordriver.goRight();
    }
   else if (data == 4)
    {
      digitalWrite(rx_led, HIGH);
      motordriver.goLeft();
    }
   else if (data == 2)
    {
      digitalWrite(rx_led, HIGH);
      motordriver.goForward();
    }
    else if (data == 1)
    {
      digitalWrite(rx_led, HIGH);
      motordriver.goBackward();
    }
   else if (data == 6)
    {
      digitalWrite(rx_led, HIGH);
      motordriver.goForward();
      motordriver.goLeft();
    }
   else if (data == 10)
    {
      digitalWrite(rx_led, HIGH);
      motordriver.goForward();
      motordriver.goRight();
    }
   else if (data == 5)
    {
      digitalWrite(rx_led, HIGH);
      motordriver.goBackward();
      motordriver.goLeft();
    }
   else if (data == 9)
    {
      digitalWrite(rx_led, HIGH);
      motordriver.goBackward();
      motordriver.goRight();
    }
    else
    {
     motordriver.stop();
    Serial.println("stop");
    }
  /*
  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);

}

Reread Marmotjr's reply, namely this bit:

Then in each if statement, reset the data value to your nodata value.

i tried it and it worked allthough the signal was jumpy (lights on motor shield would blink) that leads me to beleave my motors would be jumpy as well would a while statement maybe work better?

would a while statement maybe work better?

No. You need to figure out how to make your robot move forward AND turn at the same time.

In the same way that you read all 4 switches and then constructed the value, you need to deconstruct the value, to figure out all the things you need to do, and then do them.

You also need to stop opening new threads for the same problem.

well i have it working now..... i had a differant problem though with the same code also it seems that the help stops on old post that i have so after a while i start a new one i guess i need to be more patient sorry and paul you have been a big help i tried the -= on my RX side i couldnt get it right i hade two that where putting out the correct number but the other where not idk if youd be willing to take a look at it for me :slight_smile:

http://forum.arduino.cc/index.php?topic=326685.msg2255264#msg2255264

The variable data is shared between the main program and an ISR (Interrupt Service Routine). It should be declared volatile.

Instead of this

int data = 0;

we should see this

volatile int data = 0;

I also don't understand why some pins are called out by name, and others by number. It will work but it is a style and maintenance issue.

still seems to not be working properly

#include <Servo.h>
#include <MotorDriver.h>

Servo camera;
volatile int data = 0;

const int rx_led = 42;

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

  motordriver.init();
  motordriver.setSpeed(200, MOTORB);
  motordriver.setSpeed(50, 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()
{
  //direction
  

 if (data == 8)
    {
      data -= 8;
      digitalWrite(rx_led, HIGH);
      motordriver.goBackward();
      Serial.println("B");
    }
   else if (data == 2)
    {
      data -= 4;
      digitalWrite(rx_led, HIGH);
      motordriver.goForward();
      Serial.println("F");
    }
    else if (data == 3)
    {
      data -= 2;
      digitalWrite(rx_led, HIGH);
      motordriver.goLeft();
      Serial.println("L");
    }
  else if (data == 1)
    {
      data -= 1;
      digitalWrite(rx_led, HIGH);
      motordriver.goRight();
      Serial.println("R");
    }
    else 
    {
     motordriver.stop();
    }
  /*
  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);

}

What happens if you send 9? You REALLY, REALLY need to do the reverse of the sending logic.

bool needToGoForward = false;
bool needToGoBackward = false;
bool needToTurnLeft = false;
bool needToTurnRight = false;

if(data >= 8)
{
   needToGoBackward = true;
   data -= 8;
}

if(data >= 4)
{
   needToGoForward = true;
   data -= 4;
}

// Other two cases...

You need to make sure that you set the correct boolean as you subtract 8, 4, 2, and 1.

Then, you need to look at the 4 booleans to decide what to do.

if(needToGoForward)
{
   if(needToTurnLeft)
   {
      // turn left while going forward
   }
   else if(needToTurnRight)
   {
      // turn right while going forward
   }
   else
      // Go straight forward
}

Add similar code for going backwards.

ive decided to go with this code as for the life of me i can not figure out the deconstruction of the code on the RX side but with both methods i have the problem of when the transmitter isnt sending data the RX still just loops whatever data was sent last

#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(50, 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()
{
  //direction
  if (data == 8)
  {
    digitalWrite(rx_led, HIGH);
    motordriver.goRight();
  }
 else if (data == 4 )
  {
    digitalWrite(rx_led, HIGH);
    motordriver.goLeft();
  }
 else if (data == 2)
  {
    digitalWrite(rx_led, HIGH);
    motordriver.goForward();
  }
else if (data == 1)
  {
    digitalWrite(rx_led, HIGH);
    motordriver.goBackward();
  }
 else if (data == 10)
  {
    digitalWrite(rx_led, HIGH);
    motordriver.goForward();
    motordriver.goRight();
  }
 else if (data == 6 )
  {
    digitalWrite(rx_led, HIGH);
    motordriver.goForward();
    motordriver.goLeft();
  }
 else if (data == 5)
  {
    digitalWrite(rx_led, HIGH);
    motordriver.goBackward();
    motordriver.goLeft();
  }
 else if (data == 9)
  {
    digitalWrite(rx_led, HIGH);
    motordriver.goBackward();
    motordriver.goRight();
  }
  else if
  {
    motordriver.stop();
  }
  /*
  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);

}

Pin 42 for the LED? Mega I assume?

Yes All my lights I'm putting on higher number pins for now as to not interfer with any shields and such but once I get the controls down I'll move them around

i have the problem of when the transmitter isnt sending data the RX still just loops whatever data was sent last

Isn't that what you would expect to happen ?

ok updated my code to how paul was explaining but it still not working properly it gos forward reverse but doesnt turn at all and still gets stuck on the last camand given....i beleave i need a if statement for turning left then right but why is it always getting stuck on last cammand recieved

#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(50, 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;
  //direction
  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;
}

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


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

  /*
  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);

}

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?

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

#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);

}
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.

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

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

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....