Go Down

Topic: RC car with joystick (Read 657 times) previous topic - next topic

gshubham96

Jul 31, 2011, 03:08 pm Last Edit: Jul 31, 2011, 04:21 pm by gshubham96 Reason: 1
hello..
I was trying to built a  wireless rf r/c car controlled by a analog joystick.
it had 4 motors and motors were controlled by a l293d ic.
here's the code .
i dont know wat got wrong the circuit or the code.

transmitter side
Code: [Select]

#include <VirtualWire.h> //transmitter

 int joy1x = 1 ;
 int joy1y = 2;
 int button1 = 3 ;
 int joy2x = 4 ;
 int joy2y = 5 ;
 int button2 = 6 ;
 int ledPin = 7 ;      // LED 1
 int tx = 8 ;
 int val1 = 0 ;
 int val2 = 0 ;
 int val3 = 0 ;
 int val4 = 0 ;  
 int potPin = 9 ;
 int potValue = 0  ;
 
 
 void setup()
    {
   
 vw_set_tx_pin(tx);
 vw_setup(1600);
 
   pinMode(button1, INPUT) ;
   pinMode(button2, INPUT) ;

   pinMode(ledPin, OUTPUT);
     
  digitalWrite(ledPin, HIGH);   // set the LED on
  delay(100);              // wait for a second
  digitalWrite(ledPin, LOW);    // set the LED off
  delay(100);
  digitalWrite(ledPin, HIGH);   // set the LED on
  delay(100);              // wait for a second
  digitalWrite(ledPin, LOW);

}

void loop()
{
    val1 = analogRead(joy1x) ;
    delay(50) ;
    val2 = analogRead(joy1y) ;
    delay(50) ;
    val3 = analogRead(joy2x) ;
    delay(50) ;
    val4 = analogRead(joy2y) ;
 
   
     if(val1 >=510 && val1 <=520) {
   if(val2 >=510 && val2 <=520) {   //joystick 1
     
     digitalWrite(ledPin, HIGH) ;
     delay(val1 + val2 / 4 ) ;
     digitalWrite(ledPin, LOW) ;
     
   }
 }
   if(val1 >= 530 && val1 <= 1023)
  {    
    char *msg = "f" ;
    vw_send((uint8_t *)msg, strlen(msg));   //send the value
         vw_wait_tx() ;
     
     digitalWrite(ledPin, HIGH);   // set the LED on
     delay(100);              // wait for a second
     digitalWrite(ledPin, LOW);
 }

 
 if(val1 <=509 && val1 >= 0) {
   
  char *msg = "b" ;
   vw_send((uint8_t *)msg, strlen(msg));   //send the value
         vw_wait_tx() ;
     digitalWrite(ledPin, HIGH) ;
     delay(100) ;
     digitalWrite(ledPin, LOW) ;
   }
   
 if(val2 >= 530 && val2 <= 1023) {
   
   char *msg = "l" ;
   vw_send((uint8_t *)msg, strlen(msg));   //send the value
         vw_wait_tx() ;
       digitalWrite(ledPin, HIGH);   // set the LED on
      delay(100);              // wait for a second
      digitalWrite(ledPin, LOW);
 }
   if(val2 <=509 && val2 >= 0) {
   
  char *msg = "r" ;
    vw_send((uint8_t *)msg, strlen(msg));   //send the value
         vw_wait_tx() ;
       digitalWrite(ledPin, HIGH) ;
     delay(100) ;
     digitalWrite(ledPin, LOW) ;
   }
 
 if(val3 >= 530 && val3 <= 1023) {
   
   char *msg = "s" ;
   vw_send((uint8_t *)msg, strlen(msg));   //send the value
         vw_wait_tx() ;
       digitalWrite(ledPin, HIGH);   // set the LED on
      delay(100);              // wait for a second
      digitalWrite(ledPin, LOW);
 }
   if(val3 <=509 && val2 >= 0) {
   
  char *msg = "m" ;
    vw_send((uint8_t *)msg, strlen(msg));   //send the value
         vw_wait_tx() ;
       digitalWrite(ledPin, HIGH) ;
     delay(100) ;
     digitalWrite(ledPin, LOW) ;
 
 }
   if(button1 == HIGH && button2 == LOW)
   {
     potValue = 85 ;
     vw_send((uint8_t *)potValue, 3) ;
     vw_wait_tx() ;
    digitalWrite(ledPin, HIGH) ;
     delay(100) ;
     digitalWrite(ledPin, LOW) ;
 
   }
   
   if(button1 == LOW && button2 == HIGH)
   {
     potValue = 170 ;
     vw_send((uint8_t *)potValue, 3) ;
     vw_wait_tx() ;
     digitalWrite(ledPin, HIGH) ;
     delay(100) ;
     digitalWrite(ledPin, LOW) ;
 
 }
   
   if(button1 == HIGH && button2 == HIGH)
   {
     potValue = 255 ;
     vw_send((uint8_t *)potValue, 3) ;
     vw_wait_tx() ;
     digitalWrite(ledPin, HIGH) ;
     delay(100) ;
     digitalWrite(ledPin, LOW) ;
 
    }
}


receiver side
Code: [Select]

#include <VirtualWire.h>

const int ic2 = 0 ;
const int ic7 = 1 ;
const int ic10 = 2 ;
const int ic15 = 3 ;
const int rx = 13 ;
const int led = 12 ;
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated

long interval = 1000;           // interval at which to blink (milliseconds)


void setup() {

 vw_set_rx_pin(rx);
 vw_setup(1600);
 vw_rx_start() ;

pinMode(ic2, OUTPUT);
pinMode(ic7, OUTPUT);
pinMode(ic10, OUTPUT);
pinMode(ic15, OUTPUT);
pinMode(led, OUTPUT);
digitalWrite(led, HIGH);   // set the LED on
  delay(100);              // wait for a second
digitalWrite(led, LOW);    // set the LED off
delay(100);
 digitalWrite(led, HIGH);   // set the LED on
  delay(100);              // wait for a second
digitalWrite(led, LOW);    // set the LED off
}

void loop () {
 
 
uint8_t buf[VW_MAX_MESSAGE_LEN];
 uint8_t buflen = VW_MAX_MESSAGE_LEN;

if (vw_get_message(buf, &buflen))
 {
   if (buf[0] == 'f') {
     unsigned long currentMillis = millis();

 if(currentMillis - previousMillis > interval) {
   previousMillis = currentMillis;  

   if (ledState == LOW)
     ledState = HIGH;
   else
     ledState = LOW;

   digitalWrite(led, ledState);
 
     digitalWrite(ic2,   LOW);
     digitalWrite(ic7, HIGH);
     digitalWrite(ic10, HIGH);
     digitalWrite(ic15, LOW);
       
   }
     if (buf[0] == 'b') {
       unsigned long currentMillis = millis();

 if(currentMillis - previousMillis > interval) {
   previousMillis = currentMillis;  

   if (ledState == LOW)
     ledState = HIGH;
   else
     ledState = LOW;

   digitalWrite(led, ledState);
 
       digitalWrite(ic2, HIGH);
     digitalWrite(ic7, LOW);
     digitalWrite(ic10, LOW);
     digitalWrite(ic15, HIGH);
     }
 
     if (buf[0] == 'l') {
  unsigned long currentMillis = millis();

 if(currentMillis - previousMillis > interval) {
   previousMillis = currentMillis;  

   if (ledState == LOW)
     ledState = HIGH;
   else
     ledState = LOW;

   digitalWrite(led, ledState);
 
       digitalWrite(ic2, LOW);
     digitalWrite(ic7, HIGH);
     digitalWrite(ic10, LOW);
     digitalWrite(ic15, HIGH);
     }
     if (buf[0] == 'r') {
 unsigned long currentMillis = millis();

 if(currentMillis - previousMillis > interval) {
   previousMillis = currentMillis;  

   if (ledState == LOW)
     ledState = HIGH;
   else
     ledState = LOW;

   digitalWrite(led, ledState);
 
       digitalWrite(ic2, HIGH);
     digitalWrite(ic7, LOW);
     digitalWrite(ic10, HIGH);
     digitalWrite(ic15, LOW);
     }
     if (buf[0] == 's') {
 unsigned long currentMillis = millis();

 if(currentMillis - previousMillis > interval) {
   previousMillis = currentMillis;  

   if (ledState == LOW)
     ledState = HIGH;
   else
     ledState = LOW;

   digitalWrite(led, ledState);
 
       digitalWrite(ic2, LOW);
     digitalWrite(ic7, HIGH);
     digitalWrite(ic10, LOW);
     digitalWrite(ic15, LOW);
     }
 
     if (buf[0] == 'h') {
 unsigned long currentMillis = millis();

 if(currentMillis - previousMillis > interval) {
   previousMillis = currentMillis;  

   if (ledState == LOW)
     ledState = HIGH;
   else
     ledState = LOW;

   digitalWrite(led, ledState);
 
       digitalWrite(ic2, LOW);
     digitalWrite(ic7, LOW);
     digitalWrite(ic10, HIGH);
     digitalWrite(ic15, LOW);
     }
   }
  }
 
}
}
     }
     }
 }
}
Regards
Shubham Garg

wildbill

What seems to be the problem?

PaulS

Quote
I was trying to built a  wireless rf r/c car controlled by a analog joystick.

Using what as the transmitter/receiver?

Code: [Select]
    if(val1 >= 530 && val1 <= 1023)
val1 can not be greater than 1023, by definition, so there is no reason to test it.
Your code would be a lot cleaner with proper if/else usage:
Code: [Select]
if(val1 >= 530)
{
}
else if(val1 <= 509)
{
}

Flashing an LED involves turning it on, waiting some time, turning it off, waiting some time, and repeating. You are turning it on, waiting, turning it off, and almost immediately turning it back on.

Code: [Select]
      vw_send((uint8_t *)potValue, 3) ;
There are no native Arduino data types with odd numbers of bytes. Three?

Code: [Select]
const int ic2 = 0 ;
const int ic7 = 1 ;
const int ic10 = 2 ;
const int ic15 = 3 ;

These names don't mean anything to me. I hope they do to you.

Code: [Select]
      }
    }
   }
 
}
}
      }
      }
  }
}

This doesn't even begin to look right to me. Far too many consecutive closing braces. It appears that you only check for buf[0] to be 'b' if it is 'f'. Therefore, the only check that has any possibility of succeeding is the first one, where buf[0] is compared to 'f'.

Finally, you haven't said what problem you are having. It's hard to help without knowing what problem you are asking for help with.

gshubham96

Quote
Using what as the transmitter/receiver?

rf transmitter and receiver 434 mhz .

Quote
There are no native Arduino data types with odd numbers of bytes. Three?


i wrote three coz when sending the value 100+, the size will be three, isnt'it ?
please do tell me wat to write in size if i am wrong.

Quote
These names don't mean anything to me. I hope they do to you.


ic2 --> pin no. 2 of ic l293d
ic7 --> pin no. 7 of ic l293d
ic10 --> pin no. 10 of ic l293d
ic15 --> pin no. 15 of ic l293d

Quote
This doesn't even begin to look right to me. Far too many consecutive closing braces. It appears that you only check for buf[0] to be 'b' if it is 'f'. Therefore, the only check that has any possibility of succeeding is the first one, where buf[0] is compared to 'f'.

i didn't get you, neither how to check the received msg, so just wrote what seems to be right.

Quote
Finally, you haven't said what problem you are having. It's hard to help without knowing what problem you are asking for help with.


sorry for that

the bot starts moving right as soon as the power starts and doesnt seems to respond to the remote.
Regards
Shubham Garg

PaulS

Quote
i wrote three coz when sending the value 100+, the size will be three, isnt'it ?
please do tell me wat to write in size if i am wrong.

You are not sending the data as a string. You are sending in as binary data. An int is 2 bytes, so the correct second argument is 2 (or sizeof(int)).

Quote
i didn't get you

Your code to check the received value(s) looks like:
Code: [Select]
if(buf[0] == 'f')
{
   if(buf[0] == 'b')
   {
      if(buf[0] == 'l')
      {
         if(buf[0] == 'r')
         {
            if(buf[0] == 's')
            {
               if(buf[0] == 'h')
               {
               }
            }
         }
      }
   }
}

So, you never check for the value of 'b', 'l', 'r', 's', or 'h' unless the value IS 'f'. Clearly, nested if's are not what you want. You want this structure:
Code: [Select]
if(buf[0] == 'f')
{
}
else if(buf[0] == 'b')
{
}
else if(buf[0] == 'l')
{
}
else if(buf[0] == 'r')
{
}
else if(buf[0] == 's')
{
}
else if(buf[0] == 'h')
{
}


The whole action based on time is unclear, too. It seems to me that if you get an 'f', you want to do something regardless of how long it has been since you last got an 'f'.

Quote
the bot starts moving right as soon as the power starts and doesnt seems to respond to the remote.

I see nothing that should make the bot start moving to the right on startup, so that seems to be a separate issue - likely hardware related.

You need to strip out all the code for making the bot do something until the code for it to understand WHAT to do is working.

Connect the receiver to the PC and open the Serial Monitor, after adding Serial.print() statements to the sketch like so:
Code: [Select]
if(buf[0] == 'f')
{
   Serial.println("Told to go forward...");
}
else if(buf[0] == 'b')
{
   Serial.println("Told to go back...");
}


When the Serial Monitor output corresponds to the data sent by the remote, then you can make the bot actually do something. Until then, there are far too many possible problems  - hardware and software - to systematically sort out.

gshubham96

thanks.. for  the advice paulS. :D..:D.
i'll check it out
Regards
Shubham Garg

Go Up