Go Down

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

merkzilla

so the serial print is always 0 because i have it subtracting the equal value as it comes in as data

merkzilla

ok nevermind we are trying to see the initial value read from the data before subtracting but that still always comes to 0 im assuming because of this int data = 0; before my setup

merkzilla

alright so as an update both serial monitors match value and function for the value when sent only thing is on my reciever side say i tell it to go forward its sends the value 2 reciever serial outputs the 2 fallowed by eight 0s then another 2 unitil i stop sending the signal and gos back to all 0s but through all this when i release the forward button on transmitter side data gos back to 0 as shown in serial output and RX side outputs 0s it still continues to go forward heres the RX code
Code: [Select]
#include <Servo.h>
#include <MotorDriver.h>
Servo camera;
int data = 0;
const int rx_led = 42;

void setup()
{
  //MOTOR SHIELD SET MOTOR SPEEDS
  motordriver.init();
  motordriver.setSpeed(200, MOTORB);
  motordriver.setSpeed(200, MOTORA); // steering motor
  //DATA RECIEVE INDICATOR LIGHT
  pinMode(rx_led, OUTPUT);
  digitalWrite(rx_led, LOW);
  //RECIEVER PINS
  pinMode(3, INPUT);
  pinMode(4, INPUT);
  pinMode(5, INPUT);
  pinMode(6, INPUT);
  pinMode(7, INPUT);
  attachInterrupt(1, RF_VT, RISING);
  //BEGIN SERIAL COMMUNICATION
  Serial.begin(9600);
}
void loop()
{
  //BOOLS TO HOLD DEFAULT MOVE VALUES
  bool needToGoForward = false;
  bool needToGoBackward = false;
  bool needToTurnLeft = false;
  bool needToTurnRight = false;
  //DATA FROM TRANSMITTER TO DETERMINE MOVE FUNCTION
  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 FUNCTIONS
  if (needToGoForward)
  {
    if (needToTurnLeft)
    {
      motordriver.goForward();
      motordriver.goLeft();
    }
    else if (needToTurnRight)
    {
      motordriver.goForward();
      motordriver.goRight();
    }
    else
      motordriver.goForward();
  }
  // BACKWARD FUNCTIONS
  if (needToGoBackward)
  {
    if (needToTurnLeft)
    {
      motordriver.goBackward();
      motordriver.goLeft();
    }
    else if (needToTurnRight)
    {
      motordriver.goBackward();
      motordriver.goRight();
    }
    else
      motordriver.goBackward();
  }
  //SERIAL PRINT DATA RECIEVED
  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);
}

merkzilla

heres my TX code just in case
Code: [Select]
//BUTTON PINS
int SW1 = 8; //forward
int SW2 = 9; //backward
int SW3 = 3; //left
int SW4 = 2; //right

void setup()
{
  //SET BUTTON AS PULLUP
  pinMode(SW1, INPUT_PULLUP);
  pinMode(SW2, INPUT_PULLUP);
  pinMode(SW3, INPUT_PULLUP);
  pinMode(SW4, INPUT_PULLUP);
  //TRANSMITTER PINS
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  Serial.begin(9600);
}
void loop(void)
{
  unsigned char i = 0;
  //backward
  if ( digitalRead(SW1) == HIGH)
  {
    i += 1;
  }
  //forward
  if ( digitalRead(SW2) == HIGH)
  {
    i += 2;
  }
  //left
  if ( digitalRead(SW3) == HIGH)
  {
    i += 4;
  }
  //right
  if ( digitalRead(SW4) == HIGH)
  {
    i += 8;
  }
  send_data(i);
  Serial.print("i=");
  Serial.println(i, DEC);
  delay(500);
  send_data(0);
}
//====================================
void send_data(unsigned char data)
{
  digitalWrite(4, (data & 0x01));
  digitalWrite(5, (data & 0x02));
  digitalWrite(6, (data & 0x04));
  digitalWrite(7, (data & 0x08));
}

PaulS

Please describe EXACTLY when, and for EXACTLY how long, you expect the robot to move forward when you press a switch on the sending unit.
The art of getting good answers lies in asking good questions.

merkzilla

I would like it to move for as long as the button is pushed on the sending unit

PaulS

I would like it to move for as long as the button is pushed on the sending unit
Think about that statement. You are periodically sending data while the switch is pressed. How can that periodic data translate into continuous action on the receiver?

What I think you need to do is look at when a switch BECOMES pressed and when a switch BECOMES released.

Modify the value to be sent, by adding 1, 2, 4, or 8, when a switch becomes pressed. Then, send the value.

Modify the value to be sent, by subtracting 1, 2, 4, or 8, when a switch becomes released. Then, send the value.

This way, you will only send a value when the robot is to do something different.

I think it will make coding the receiver far easier.

(The state change detection example shows how to determine when a switch becomes pressed or released.)
The art of getting good answers lies in asking good questions.

GoForSmoke

How about "if no data for X milliseconds, do nothing"?

Because depending on baud rate, serial chars arrive with gaps between.

Consider that if your code does not block, 10 ms is a long time to Arduino.
1) http://gammon.com.au/blink  <-- tasking Arduino 1-2-3
2) http://gammon.com.au/serial <-- techniques howto
3) http://gammon.com.au/interrupts
Your sketch can sense ongoing process events in time.
Your sketch can make events to control it over time.

merkzilla

ok switched my TX code although the math is a liittle off data prints out oer 240 except for my right button and if i go backward and right its 1 and 3 so if i use the if data is >= on the rx side wont it always be going forward and right but the other way wouldnt work ether

merkzilla

Code: [Select]
#include <Servo.h>
#include <MotorDriver.h>
Servo camera;
int data = 0;
const int rx_led = 42;

void setup()
{
  //MOTOR SHIELD SET MOTOR SPEEDS
  motordriver.init();
  motordriver.setSpeed(200, MOTORB);
  motordriver.setSpeed(200, MOTORA); // steering motor
  //DATA RECIEVE INDICATOR LIGHT
  pinMode(rx_led, OUTPUT);
  digitalWrite(rx_led, LOW);
  //RECIEVER PINS
  pinMode(3, INPUT);
  pinMode(4, INPUT);
  pinMode(5, INPUT);
  pinMode(6, INPUT);
  pinMode(7, INPUT);
  attachInterrupt(1, RF_VT, RISING);
  //BEGIN SERIAL COMMUNICATION
  Serial.begin(9600);
}
void loop()
{
  //BOOLS TO HOLD DEFAULT MOVE VALUES
  bool needToGoForward = false;
  bool needToGoBackward = false;
  bool needToTurnLeft = false;
  bool needToTurnRight = false;
  //DATA FROM TRANSMITTER TO DETERMINE MOVE FUNCTION
  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 FUNCTIONS
  if (needToGoForward)
  {
    if (needToTurnLeft)
    {
      motordriver.goForward();
      motordriver.goLeft();
    }
    else if (needToTurnRight)
    {
      motordriver.goForward();
      motordriver.goRight();
    }
    else
      motordriver.goForward();
  }
  // BACKWARD FUNCTIONS
  if (needToGoBackward)
  {
    if (needToTurnLeft)
    {
      motordriver.goBackward();
      motordriver.goLeft();
    }
    else if (needToTurnRight)
    {
      motordriver.goBackward();
      motordriver.goRight();
    }
    else
      motordriver.goBackward();
  }
  //SERIAL PRINT DATA RECIEVED
  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);
}

merkzilla

ive tried if (data == 0) do nothing is there another way to say if theres no data

PaulS

Quote
is there another way to say if theres no data
What is your definition of "no data"? In my mind, that means data == 0. If doing something when data is 0 doesn't work, then you are doing something wrong.

Write some code that starts the motors running in setup(). In loop(), execute whatever code you have for the case where data is 0, and see if they stop. If they do, then something in the current sketch is wrong. If not, it's a hardware problem.
The art of getting good answers lies in asking good questions.

merkzilla

yes thats what i mean as well but when i put in

if (data >= 1)
  {
    data -= 1;
    needToGoBackward = true;
  }
  if (data == 0)
  {
    motordriver.stop();
  }

the serial  puts out this

data=1
data=0
data=0
data=0
data=0
data=0
data=0
data=0
data=0
data=0
data=1
data=0
data=0

so it trys to move but because it gos back to 0 it just doesnt

PaulS

Quote
so it trys to move but because it gos back to 0 it just doesnt
After all the diddling with data, of course the value is going to be 0. You need to structure the code like so:
Code: [Select]
void loop()
{
   if(data == 0)
   {
       // stop the motors
   }
   else
   {
      // Determine what to do, based on data

      // Do what needs doing
   }
}
The art of getting good answers lies in asking good questions.

merkzilla

so i restructured it but then i run into the same problem where it starts at 0 but as soon as i send a cammand say 2 for going forward even if i stop sending 2 from my TX it just constantly serial prints 2 and gos forward on my RX side idk why id doesnt reset back to 0 unless i subtract the same amount of data coming in
Code: [Select]
#include <Servo.h>
#include <MotorDriver.h>
Servo camera;
int data = 0;
const int rx_led = 42;
void setup()
{
  //MOTOR SHIELD SET MOTOR SPEEDS
  motordriver.init();
  motordriver.setSpeed(200, MOTORB);
  motordriver.setSpeed(200, MOTORA); // steering motor
  //DATA RECIEVE INDICATOR LIGHT
  pinMode(rx_led, OUTPUT);
  digitalWrite(rx_led, LOW);
  //RECIEVER PINS
  pinMode(3, INPUT);
  pinMode(4, INPUT);
  pinMode(5, INPUT);
  pinMode(6, INPUT);
  pinMode(7, INPUT);
  attachInterrupt(1, RF_VT, RISING);
  //BEGIN SERIAL COMMUNICATION
  Serial.begin(9600);
}
void loop()
{
  //BOOLS TO HOLD DEFAULT MOVE VALUES
  bool needToGoForward = false;
  bool needToGoBackward = false;
  bool needToTurnLeft = false;
  bool needToTurnRight = false;
  bool needToBreak = false;
  //DATA FROM TRANSMITTER TO DETERMINE MOVE FUNCTION
  if(data == 0)
  {
    motordriver.stop();
  }
  else
  {
    if (data >= 8)
  {
   
  needToTurnRight = true;
  }
  if (data >= 4)
  {
   
    needToTurnLeft = true;
  }
  if (data >= 2)
  {
   
    needToGoForward = true;
  }
  if (data >= 1)
  {
   
    needToGoBackward = true;
  }
  }
 
  //FORWARD FUNCTIONS
  if (needToGoForward)
  {
    if (needToTurnLeft)
    {
      motordriver.goForward();
      motordriver.goLeft();
    }
    else if (needToTurnRight)
    {
      motordriver.goForward();
      motordriver.goRight();
    }
    else
      motordriver.goForward();
  }
  // BACKWARD FUNCTIONS
  if (needToGoBackward)
  {
    if (needToTurnLeft)
    {
      motordriver.goBackward();
      motordriver.goLeft();
    }
    else if (needToTurnRight)
    {
      motordriver.goBackward();
      motordriver.goRight();
    }
    else
      motordriver.goBackward();
  }

  //SERIAL PRINT DATA RECIEVED
  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