Go Down

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

merkzilla

#45
Jun 02, 2015, 03:33 pm Last Edit: Jun 02, 2015, 03:35 pm by merkzilla
so i have a code that all functions work now including stopping when nothing is pressed. not sure if this is from the transmitter recever pair im using but it seems theres sometimes as highs as a 2 sec delay between when i send value and when the rx receves the value anyway heres what i have for code working so far tx side first

Code: [Select]
//BUTTON PINS
const int SW1 = 8; //forward
const int SW2 = 9; //backward
const int SW3 = 3; //left
const 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)
{
  //sends 15 as stop command if no button pressed
  unsigned char i = 15;
  //if button pressed - from 15 and send new value
  //backwards
  if (digitalRead(SW1) == HIGH)
  {
    i -= 8;
  }
  //forward
  if (digitalRead(SW2) == HIGH)
  {
    i -= 4;
  }
  //left
  if (digitalRead(SW3) == HIGH)
  {
    i -= 2;
  }
  //right
  if (digitalRead(SW4) == HIGH)
  {
    i -= 1;
  }
  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));
}

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(400, 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()
{
  //DATA FROM TRANSMITTER TO DETERMINE MOVE FUNCTION
  //stop
  if (data == 15)
  {
    motordriver.stop();
  }
  //turn wheels right
  else if (data == 14)
  {
    motordriver.goRight();
  }
  //turn wheels left
  else if (data == 13)
  {
    motordriver.goLeft();
  }
  // move forward
  else if (data == 11)
  {
    motordriver.goForward();
  }
  //move backward
  else if (data == 7)
  {
    motordriver.goBackward();
  }
  //straight left turn
  else if (data == 9)
  {
    motordriver.goForward();
    motordriver.goLeft();
  }
  //straight right turn
  else if (data == 10)
  {
    motordriver.goForward();
    motordriver.goRight();
  }
  //back left turn
  else if (data == 5)
  {
    motordriver.goBackward();
    motordriver.goLeft();
  }
  //back right turn
  else if (data == 6)
  {
    motordriver.goBackward();
    motordriver.goRight();
  }
  //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