element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Arduino Sketch Question???
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Not Answered
  • Replies 2 replies
  • Subscribers 401 subscribers
  • Views 328 views
  • Users 0 members are here
  • multiple
  • byte
  • virtual
  • loop
  • rc
  • sketch
  • car
  • char
  • digits
  • receiver
  • transmitter
  • wire
  • and
  • for
  • rf
  • array
  • arduino
Related

Arduino Sketch Question???

fechrxy
fechrxy over 12 years ago

Hey, so i am writing a sketch for an rc car. I am using one of those cheap five dollar rf transmitter and receiver modules and am using virtual wire library in my sketch. I had gotten it to when i hit one of the four buttons, the remote control would wirelessly tell one of the two motors which direction to turn. However i cannot seem to figure out how to allow the car to multi task, for example turn right while going forward. I found that i needed to send two bytes at a time the first telling motor A what to do and the second telling motor B what to do. The wireless data is handled in a for loop when received thus meaning it is in an array. So i geuss i am stuck the two sketches below (transmitter and receiver) will happily control the direction of the first motor but just doesn't seem to work with the second. I know i did a terrible job of explaining so if needed i can provide additional information. Thanks so much.    (I attatched the files below)

 

 

 

TRANSMITTER

 

// you must download and install the VirtualWire.h

// to your hardware/libraries folder

#include <VirtualWire.h>

 

 

const int Enable_Motor_A = 9;

const int P1_Motor_A = 11;

const int P2_Motor_A = 13; 

 

 

const int Enable_Motor_B = 10;

const int P1_Motor_B = 5;

const int P2_Motor_B = 6;

 

 

byte msg[1];

 

 

void setup()

{

   Serial.begin(9600);

  pinMode(6, INPUT);

    pinMode(13, INPUT);

    pinMode(8, INPUT);

    pinMode(9, INPUT);

   

  

    vw_setup(2000);

    vw_set_tx_pin(7);

}

 

 

void loop()

{

    

 

 

  msg[0] = '0';

      msg[0] = '0';

    if(digitalRead(13) == HIGH) {

      msg[0] = '1';

    }

    

   

    else if(digitalRead(6) == HIGH) {

      msg[0] = '2';

    } 

   

    if(digitalRead(8) == HIGH) {

      msg[1] = '3';

    }

    

   

    else if(digitalRead(9) == HIGH) {

      msg[1] = '4';

    }

  

   

 

 

 

   vw_send(msg, 1);

  

   vw_wait_tx();

 

   

}

 

 

 

RECEIVER

 

#include <VirtualWire.h>

 

 

const int Enable_Motor_A = 9;

const int P1_Motor_A = 12;

const int P2_Motor_A = 13; 

 

 

const int Enable_Motor_B = 10;

const int P1_Motor_B = 5;

const int P2_Motor_B = 6;

 

 

 

 

byte buf[1];

byte buflen;

void setup()

{

  buflen = 1;

 

 

  pinMode(Enable_Motor_A, OUTPUT);  //Declares the three 'Motor A' pins as outputs.

  pinMode(P1_Motor_A, OUTPUT);

  pinMode(P2_Motor_A, OUTPUT);

  

  pinMode(Enable_Motor_B, OUTPUT);  //Declares the three 'Motor B' pins as outputs.

  pinMode(P1_Motor_B, OUTPUT);

  pinMode(P2_Motor_B, OUTPUT);

 

  digitalWrite(Enable_Motor_A, LOW); 

  digitalWrite(P1_Motor_A, LOW);

  digitalWrite(P2_Motor_A, LOW);

  

  digitalWrite(Enable_Motor_B, LOW); 

  digitalWrite(P1_Motor_B, LOW);

  digitalWrite(P2_Motor_B, LOW);

 

  vw_setup(2000);

  vw_set_rx_pin(7);

  vw_rx_start();

 

  

 

}

 

 

 

 

void loop()

{

  uint8_t buflen = VW_MAX_MESSAGE_LEN;

  uint8_t buf[buflen];

 

  if (vw_get_message(buf, &buflen))          

  {

    for(int i = 0;i < buflen;i++)           

  { 

  if (buf[0] == '1')                                //FORWARDS BUTTON

   {

    digitalWrite(P2_Motor_A, LOW);

    digitalWrite(P1_Motor_A, HIGH);

    analogWrite(Enable_Motor_A, 255);}

 

 

  else if (buf[0] == '2')                         //BACKWARDS BUTTON

  {

    digitalWrite(P1_Motor_A, LOW);

    digitalWrite(P2_Motor_A, HIGH);

    analogWrite(Enable_Motor_A, 255);}

 

 

  

 

 

  else                                            //NIETHER BUTTONS

   { 

    digitalWrite(P1_Motor_A, LOW);

    digitalWrite(P2_Motor_A, LOW);

    digitalWrite(Enable_Motor_A, LOW);}

    

 

 

 

 

  if (buf[1] == '3')                              //RIGHT BUTTON

  {

    digitalWrite(P2_Motor_B, LOW);

    digitalWrite(P1_Motor_B, HIGH);

    analogWrite(Enable_Motor_B, 255);}

    

  else if (buf[1] == '4')                         //LEFT BUTTON

  {

    digitalWrite(P1_Motor_B, LOW);

    digitalWrite(P2_Motor_B, HIGH);

    analogWrite(Enable_Motor_B, 255);}

   

  else                                            //NIETHER BUTTONS

   {                      

    digitalWrite(P1_Motor_B, LOW);

    digitalWrite(P2_Motor_B, LOW);

    digitalWrite(Enable_Motor_B, LOW);}

  }}

 

 

 

 

}

  • Sign in to reply
  • Cancel

Top Replies

  • Former Member
    Former Member over 12 years ago in reply to mcb1 +1
    In TRANSMITTER: msg[0] = '0'; msg[0] = '0'; one of these should set msg[1]. Edited to add: In RECEIVER, you have: for(int i = 0;i < buflen;i++) but then you don't reference 'i'. Instead you say for example…
  • mcb1
    0 mcb1 over 12 years ago

    Tony

    I'd be slightly more inclined to help, if you answered or acknowledged the other questions you pose.

     

    Good luck.

    Mark

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 12 years ago in reply to mcb1

    In TRANSMITTER:

    msg[0] = '0';

          msg[0] = '0';

     

    one of these should set msg[1].

     

     

    Edited to add:

    In RECEIVER, you have:

                    for(int i = 0;i < buflen;i++)

     

    but then you don't reference 'i'.  Instead you say for example:

          if (buf[0] == '1')   

    referencing buf[0] rather than buf[i], which I don't think is what you want.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube