Go Down

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

merkzilla

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

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

}

Marmotjr

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. 

merkzilla

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

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

}

MarkT

Reread Marmotjr's reply, namely this bit:
Quote
Then in each if statement, reset the data value to your nodata value.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

merkzilla

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?

PaulS

Quote
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.
The art of getting good answers lies in asking good questions.

merkzilla

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 :)

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

vaj4088

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

Instead of this
Code: [Select]
int data = 0;

we should see this
Code: [Select]
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.

merkzilla

still seems to not be working properly

Code: [Select]
#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);

}

PaulS

#9
May 31, 2015, 01:21 am Last Edit: May 31, 2015, 01:30 am by PaulS
What happens if you send 9? You REALLY, REALLY need to do the reverse of the sending logic.

Code: [Select]
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.
Code: [Select]

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.
The art of getting good answers lies in asking good questions.

merkzilla

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

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

}

Marmotjr

Pin 42 for the LED?  Mega I assume?

merkzilla

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

michinyon

Quote
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 ?

merkzilla

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

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

}

Go Up