Go Down

Topic: RC-Car Control (Read 1 time) previous topic - next topic

elfriso

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?

AWOL

Can you post your code?
"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.

elfriso

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);
}

}

}

AWOL

"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.

elfriso

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);
               

            }
           
           
        }
}
}

AWOL

So you don't detect the change between pressing the key and releasing the key.
"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.

elfriso

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.

AWOL

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.
"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.

JasonK

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

PaulS

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.

elfriso

Thank you JasonK. That helped alot.

Go Up