Pages: [1]   Go Down
Author Topic: RC-Car Control  (Read 1265 times)
0 Members and 1 Guest are viewing this topic.
Austria
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi

I want to control a rc car with an arduino. To control it i use C#. When i want to drive forward i press the UP-key on my Computer and a Character will be sendet out over XBEE. The arduino recive it and activate the Motor. That works fine but i have a problem to put the output of the arduino LOW when i release the button on my computer. I don´t want to send out another char. when I release the Button.

Please can anybody help me?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26293
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can you post your code?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Austria
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

int vor = 9, rueck=10, links=11, rechts=12;
int buffer;
int temp=206;

void setup()
{
pinMode(vor, OUTPUT);
pinMode(rueck, OUTPUT);
pinMode(links, OUTPUT);
pinMode(rechts, OUTPUT);
Serial.begin(9600);

}

void loop()
{

  
if (Serial.available() > 0)
{
  buffer=Serial.read();
  
 
  
if (buffer == 'w')
{
  analogWrite(vor, temp);
}

if (buffer == 's')
{
  analogWrite(rueck, temp);
}

if (buffer == 'l')
{
  analogWrite(links, temp);
}

if (buffer == 'r')
{
  analogWrite(rechts, temp);
}

}
 
}
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26293
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

And the C# code?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Austria
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;


namespace RC_car_1
{
    public partial class Form1 : Form
    {
        SerialPort port;
        String w = "w", s = "s", l = "l", r = "r";

        public Form1()
        {
            InitializeComponent();
            port = new SerialPort("COM4", 9600, Parity.None, 8, StopBits.One);
            port.Open();
           
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {

           
           
            if (e.KeyCode == Keys.Up)
            {
               
                port.Write(w);
               
               
            }
            if (e.KeyCode == Keys.Down)
            {

                port.Write(s);
               

            }
            if (e.KeyCode == Keys.Left)
            {

                port.Write(l);
               
            }
            if (e.KeyCode == Keys.Right)
            {

                port.Write(r);
               

            }
           
           
        }
}
}
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26293
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So you don't detect the change between pressing the key and releasing the key.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Austria
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I had implementet that before. When i released the button another char was sendet out and set the output LOW. That worked fine. The problem was when i came out of the Range from the Xbee the char from releasing the button didn´t arrive and the car never stopped. So what i want is that the arduino code set the output LOW when i release the butten, that means when no chars arrive to arduino.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26293
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How does the Arduino code know when you have released the key?
Please use the # icon on the editors toolbar when posting code.
You can go back and edit your previous posts to achieve the same effect.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 1
Posts: 48
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you want the car to stop if it doesn't receive any input, you should add some code on the Arduino to detect the lack of input -> something like this:

// global variable declarations
unsigned long previousMillis = 0;
unsigned long interval = 100;  // 1/10 second

at the begining of your loop add
unsigned long currentMillis = Millis();

after
if (Serial.available() > 0)
{
  buffer=Serial.read()
add this
  previousMillis = currentMillis;


then after the if statement closes

if(currentMillis - previousMillis > interval) {
   //code to stop the car
}

this will cause the car to stop if it doesn't receive input for interval ms
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49343
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I don´t want to send out another char. when I release the Button.
Please explain why not. This would be the most reasonable thing to do.
Logged

Austria
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you JasonK. That helped alot.
Logged

Pages: [1]   Go Up
Jump to: